Skip to content

Commit 76f1cdb

Browse files
committed
updated test-cases to avoid hardcoded paths
Signed-off-by: nikhil2611 <[email protected]>
1 parent 3e95a0c commit 76f1cdb

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

spec/unit/command/env_spec.rb

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,31 @@
8484
end
8585

8686
describe "when running in a Chef-cli Habitat Standalone package" do
87+
let(:standalone_pkg_base) { "/hab/pkgs/chef/chef-cli" }
88+
let(:standalone_pkg_version) { "1.0.0" }
89+
let(:standalone_pkg_build) { "20240210120000" }
90+
let(:standalone_pkg_path) { "#{standalone_pkg_base}/#{standalone_pkg_version}/#{standalone_pkg_build}" }
91+
92+
let(:ruby_version) { "3.1.0" }
93+
let(:ruby_base) { "/hab/pkgs/core/ruby/#{ruby_version}/20240101000000/lib/ruby/gems" }
94+
let(:cli_gem_home) { "/hab/pkgs/chef/chef-cli/#{standalone_pkg_version}/20240210121000/vendor/bundle/ruby/#{ruby_version}" }
95+
8796
before do
8897
allow(command_instance).to receive(:habitat_install?).and_return(true)
8998
allow(command_instance).to receive(:habitat_standalone?).and_return(true)
9099
allow(command_instance).to receive(:habitat_chef_dke?).and_return(false)
91-
allow(command_instance).to receive(:omnibus_install?).and_return(false) # Ensure Omnibus is NOT detected
100+
allow(command_instance).to receive(:omnibus_install?).and_return(false)
92101
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_CLI_PACKAGE)
93102

94-
# Habitat package paths
95-
hab_pkg_path = "/hab/pkgs/chef/chef-cli/1.0.0/20240210120000"
96-
allow(command_instance).to receive(:get_chef_cli_path).and_return("#{hab_pkg_path}")
103+
allow(command_instance).to receive(:get_chef_cli_path).and_return(standalone_pkg_path)
97104

98-
# Mock habitat_env to reflect correct GEM paths
99105
allow(command_instance).to receive(:habitat_env).and_return({
100-
"GEM_ROOT" => "/hab/pkgs/core/ruby/3.1.0/20240101000000/lib/ruby/gems",
101-
"GEM_HOME" => "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0",
102-
"GEM_PATH" => "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0",
103-
"PATH" => "/hab/pkgs/chef/chef-cli/1.0.0/20240210120000/bin:/usr/local/bin:/usr/bin"
106+
"GEM_ROOT" => ruby_base,
107+
"GEM_HOME" => cli_gem_home,
108+
"GEM_PATH" => cli_gem_home,
109+
"PATH" => "#{standalone_pkg_path}/bin:/usr/local/bin:/usr/bin"
104110
})
105-
111+
106112
command_instance.ui = ui
107113
end
108114

@@ -122,24 +128,33 @@
122128
end
123129

124130
it "should include correct Habitat installation path" do
125-
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Install Directory"]).to eql "/hab/pkgs/chef/chef-cli/1.0.0/20240210120000"
131+
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Install Directory"]).to eql standalone_pkg_path
126132
end
127133

128134
it "should include correct GEM_ROOT path" do
129-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql "/hab/pkgs/core/ruby/3.1.0/20240101000000/lib/ruby/gems"
135+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql ruby_base
130136
end
131137

132138
it "should include correct GEM_HOME path" do
133-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0"
139+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql cli_gem_home
134140
end
135141

136142
it "should include correct GEM_PATH paths" do
137-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql ["/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0"]
143+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql [cli_gem_home]
138144
end
139145
end
140146
end
141-
142-
describe "when running chef-cli coming with Habitat Chef-DKE package" do
147+
148+
describe "when running chef-cli coming with Chef-DKE Habitat package" do
149+
let(:hab_pkg_base) { "/hab/pkgs/chef/chef-development-kit-enterprise" }
150+
let(:hab_pkg_version) { "1.0.0" }
151+
let(:hab_pkg_build) { "20240210120000" }
152+
let(:hab_pkg_path) { "#{hab_pkg_base}/#{hab_pkg_version}/#{hab_pkg_build}" }
153+
154+
let(:ruby_version) { "3.1.0" }
155+
let(:ruby_base) { "/hab/pkgs/core/ruby/#{ruby_version}/20240101000000/lib/ruby/gems" }
156+
let(:cli_gem_home) { "/hab/pkgs/chef/chef-cli/#{hab_pkg_version}/20240210121000/vendor/bundle/ruby/#{ruby_version}" }
157+
143158
before do
144159
# Mock all Habitat-related methods
145160
allow(command_instance).to receive(:habitat_install?).and_return true
@@ -148,52 +163,50 @@
148163
allow(command_instance).to receive(:omnibus_install?).and_return false
149164
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_DK_CLI_PACKAGE)
150165

151-
# Habitat package paths
152-
hab_pkg_path = "/hab/pkgs/chef/chef-development-kit-enterprise/1.0.0/20240210120000"
153-
allow(command_instance).to receive(:get_chef_cli_path).and_return("#{hab_pkg_path}")
166+
# Mock Habitat package paths
167+
allow(command_instance).to receive(:get_chef_cli_path).and_return(hab_pkg_path)
154168

155169
# Mock habitat_env to reflect correct GEM paths
156170
allow(command_instance).to receive(:habitat_env).and_return({
157-
"GEM_ROOT" => "/hab/pkgs/core/ruby/3.1.0/20240101000000/lib/ruby/gems",
158-
"GEM_HOME" => "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0",
159-
"GEM_PATH" => "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0",
160-
"PATH" => "/hab/pkgs/chef/chef-development-kit-enterprise/1.0.0/20240210120000/bin:/usr/local/bin:/usr/bin"
171+
"GEM_ROOT" => ruby_base,
172+
"GEM_HOME" => cli_gem_home,
173+
"GEM_PATH" => cli_gem_home,
174+
"PATH" => "#{hab_pkg_path}/bin:/usr/local/bin:/usr/bin"
161175
})
162-
176+
163177
command_instance.ui = ui
164-
end
165-
178+
end
179+
166180
describe "and the env command is run" do
167181
let(:yaml) { YAML.load(ui.output) }
168182

169183
before :each do
170184
run_command
171185
end
172-
186+
173187
it "should include correct product name for Chef-DKE Habitat package" do
174188
expect(yaml).to have_key(ChefCLI::Dist::CHEF_DK_CLI_PACKAGE)
175189
end
176190

177191
it "should include correct version" do
178192
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Version"]).to eql ChefCLI::VERSION
179193
end
180-
194+
181195
it "should include correct Habitat installation path" do
182-
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Install Directory"]).to eql "/hab/pkgs/chef/chef-development-kit-enterprise/1.0.0/20240210120000"
196+
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Install Directory"]).to eql hab_pkg_path
183197
end
184198

185199
it "should include correct GEM_ROOT path" do
186-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql "/hab/pkgs/core/ruby/3.1.0/20240101000000/lib/ruby/gems"
200+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql ruby_base
187201
end
188202

189203
it "should include correct GEM_HOME path" do
190-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql "/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0"
204+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql cli_gem_home
191205
end
192206

193207
it "should include correct GEM_PATH paths" do
194-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql ["/hab/pkgs/chef/chef-cli/1.0.0/20240210121000/vendor/bundle/ruby/3.1.0"]
208+
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql [cli_gem_home]
195209
end
196-
197210
end
198211
end
199212

0 commit comments

Comments
 (0)