Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds MiqHelper #15020

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/manageiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'pathname'
require 'active_support/core_ext/object/blank'
require 'active_support/string_inquirer'

module ManageIQ
# Defined in the same fasion
def self.env
@_env ||= begin
if defined?(Rails)
Rails.env
else
ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
end
end
end

def self.root
@_root ||= begin
if defined?(Rails)
Rails.root
else
Pathname.new File.expand_path("../..", __FILE__)
end
end
end
end
68 changes: 68 additions & 0 deletions spec/lib/manageiq_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This tests are done in a way that assume the Rails environment for ManageIQ are
# loaded, and that Rails is present to do the comparisons. Tests that confirm
# that this works when rails is not present us a subprocess to run the
# `lib/miq.rb` file in isolation.
#
# If tests are not passing, check to see if the spec/spec_helper.rb is being
# loaded properly and initailizing the Vmdb::Application.

require 'manageiq'

describe ManageIQ do
def without_rails(rb_cmd)
miq_lib_file = Rails.root.join("lib", "manageiq.rb")
`#{Gem.ruby} -e 'require "#{miq_lib_file}"; print #{rb_cmd}'`
end

describe ".env" do
before(:each) do
ManageIQ.instance_variable_set(:@_env, nil)
end

it "equivalent to Rails.root when Rails is present" do
expect(ManageIQ.env.to_s).to eq(Rails.env.to_s)
end

it "equivalent to Rails.root even when Rails is not present" do
result = without_rails('ManageIQ.env.to_s')
expect(result).to eq(Rails.env.to_s)
end

it "responds to .test?" do
expect(ManageIQ.env.test?).to be true
expect(without_rails('ManageIQ.env.test?.inspect')).to eq("true")
end

it "responds to .development?" do
expect(ManageIQ.env.development?).to be false
expect(without_rails('ManageIQ.env.development?.inspect')).to eq("false")
end

it "responds to .production?" do
expect(ManageIQ.env.production?).to be false
expect(without_rails('ManageIQ.env.production?.inspect')).to eq("false")
end
end

describe ".root" do
before(:each) do
ManageIQ.instance_variable_set(:@_root, nil)
end

it "equivalent to Rails.root when Rails is present" do
expect(ManageIQ.root.to_s).to eq(Rails.root.to_s)
end

it "equivalent to Rails.root even when Rails is not present" do
result = without_rails('ManageIQ.root.to_s')
expect(result).to eq(Rails.root.to_s)
end

it "responds to .join" do
expected = Rails.root.join('config')
expect(ManageIQ.root.join('config')).to eq(expected)
# doing an .inspect here to confirm it is a Pathname
expect(without_rails('ManageIQ.root.join("config").inspect')).to eq(expected.inspect)
end
end
end