Skip to content

Commit

Permalink
Merge pull request #15020 from NickLaMuro/miq_helper
Browse files Browse the repository at this point in the history
Adds MiqHelper
  • Loading branch information
jrafanie authored Jun 5, 2017
2 parents 35f17a4 + 50208b1 commit 037d27e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
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

0 comments on commit 037d27e

Please sign in to comment.