|
| 1 | +# |
| 2 | +# Copyright:: (c) 2019-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | +# License:: Apache License, Version 2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +require "spec_helper" |
| 19 | +require "chef-cli/command/gem" |
| 20 | + |
| 21 | +describe ChefCLI::Command::GemForwarder do |
| 22 | + let(:command_instance) { described_class.new } |
| 23 | + let(:gem_runner) { instance_double(Gem::GemRunner) } |
| 24 | + let(:ruby_version) { RbConfig::CONFIG["ruby_version"] } |
| 25 | + let(:expected_gem_dir) { File.expand_path("~/.chef/ruby/#{ruby_version}/gems") } |
| 26 | + |
| 27 | + before do |
| 28 | + allow(Gem::GemRunner).to receive(:new).and_return(gem_runner) |
| 29 | + end |
| 30 | + |
| 31 | + it "has a usage banner" do |
| 32 | + expect(command_instance.banner).to eq("Usage: chef gem GEM_COMMANDS_AND_OPTIONS") |
| 33 | + end |
| 34 | + |
| 35 | + describe "#needs_version?" do |
| 36 | + it "returns false to let GemRunner handle version flag" do |
| 37 | + expect(command_instance.needs_version?([])).to be(false) |
| 38 | + end |
| 39 | + |
| 40 | + it "returns false even with -v parameter" do |
| 41 | + expect(command_instance.needs_version?(["-v"])).to be(false) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe "#run" do |
| 46 | + context "when NOT in a Habitat environment" do |
| 47 | + before do |
| 48 | + allow(command_instance).to receive(:habitat_install?).and_return(false) |
| 49 | + end |
| 50 | + |
| 51 | + it "forwards params to Gem::GemRunner" do |
| 52 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 53 | + expect(command_instance.run(%w(install knife))).to eq(true) |
| 54 | + end |
| 55 | + |
| 56 | + it "does not modify GEM_HOME" do |
| 57 | + expect(gem_runner).to receive(:run).with(%w(list)).and_return(true) |
| 58 | + expect(ENV).not_to receive(:[]=).with("GEM_HOME", anything) |
| 59 | + command_instance.run(%w(list)) |
| 60 | + end |
| 61 | + |
| 62 | + it "returns true when GemRunner returns nil" do |
| 63 | + expect(gem_runner).to receive(:run).with(%w(list)).and_return(nil) |
| 64 | + expect(command_instance.run(%w(list))).to eq(true) |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + context "when in a Habitat environment" do |
| 69 | + let(:vendor_dir) { "/hab/pkgs/chef/chef-cli/1.0.0/123/vendor" } |
| 70 | + let(:habitat_env_hash) do |
| 71 | + { |
| 72 | + "PATH" => "/hab/pkgs/chef/chef-cli/1.0.0/123/bin", |
| 73 | + "GEM_ROOT" => Gem.default_dir, |
| 74 | + "GEM_HOME" => expected_gem_dir, |
| 75 | + "GEM_PATH" => "#{expected_gem_dir}:#{vendor_dir}", |
| 76 | + } |
| 77 | + end |
| 78 | + |
| 79 | + before do |
| 80 | + allow(command_instance).to receive(:habitat_install?).and_return(true) |
| 81 | + allow(command_instance).to receive(:habitat_user_gem_dir).and_return(expected_gem_dir) |
| 82 | + allow(command_instance).to receive(:habitat_env).and_return(habitat_env_hash) |
| 83 | + allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(true) |
| 84 | + allow(Gem).to receive(:clear_paths) |
| 85 | + end |
| 86 | + |
| 87 | + it "sets GEM_HOME to user gem directory" do |
| 88 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 89 | + expect(ENV).to receive(:[]=).with("GEM_HOME", expected_gem_dir) |
| 90 | + allow(ENV).to receive(:[]=).with("GEM_PATH", anything) |
| 91 | + command_instance.run(%w(install knife)) |
| 92 | + end |
| 93 | + |
| 94 | + it "sets GEM_PATH to include both user gem dir and habitat vendor dir" do |
| 95 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 96 | + allow(ENV).to receive(:[]=).with("GEM_HOME", expected_gem_dir) |
| 97 | + expected_gem_path = "#{expected_gem_dir}:#{expected_gem_dir}:#{vendor_dir}" |
| 98 | + expect(ENV).to receive(:[]=).with("GEM_PATH", expected_gem_path) |
| 99 | + command_instance.run(%w(install knife)) |
| 100 | + end |
| 101 | + |
| 102 | + it "clears Gem paths after setting environment" do |
| 103 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 104 | + allow(ENV).to receive(:[]=) |
| 105 | + expect(Gem).to receive(:clear_paths) |
| 106 | + command_instance.run(%w(install knife)) |
| 107 | + end |
| 108 | + |
| 109 | + it "creates the gem directory if it doesn't exist" do |
| 110 | + allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(false) |
| 111 | + expect(FileUtils).to receive(:mkdir_p).with(expected_gem_dir) |
| 112 | + allow(ENV).to receive(:[]=) |
| 113 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 114 | + command_instance.run(%w(install knife)) |
| 115 | + end |
| 116 | + |
| 117 | + it "does not create the gem directory if it already exists" do |
| 118 | + allow(Dir).to receive(:exist?).with(expected_gem_dir).and_return(true) |
| 119 | + expect(FileUtils).not_to receive(:mkdir_p) |
| 120 | + allow(ENV).to receive(:[]=) |
| 121 | + expect(gem_runner).to receive(:run).with(%w(install knife)).and_return(true) |
| 122 | + command_instance.run(%w(install knife)) |
| 123 | + end |
| 124 | + |
| 125 | + it "forwards all gem subcommands correctly" do |
| 126 | + allow(ENV).to receive(:[]=) |
| 127 | + %w(install list uninstall source search update).each do |subcmd| |
| 128 | + expect(gem_runner).to receive(:run).with([subcmd]).and_return(true) |
| 129 | + expect(command_instance.run([subcmd])).to eq(true) |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context "when GemRunner raises Gem::SystemExitException" do |
| 135 | + before do |
| 136 | + allow(command_instance).to receive(:habitat_install?).and_return(false) |
| 137 | + end |
| 138 | + |
| 139 | + it "exits with the exception's exit code" do |
| 140 | + exception = Gem::SystemExitException.new(1) |
| 141 | + allow(gem_runner).to receive(:run).and_raise(exception) |
| 142 | + expect { command_instance.run(%w(install bad_gem)) }.to raise_error(SystemExit) { |e| |
| 143 | + expect(e.status).to eq(1) |
| 144 | + } |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + describe "#habitat_user_gem_dir" do |
| 150 | + it "returns ~/.chef/ruby/<version>/gems path" do |
| 151 | + expect(command_instance.send(:habitat_user_gem_dir)).to eq(expected_gem_dir) |
| 152 | + end |
| 153 | + |
| 154 | + it "uses the ruby_version from RbConfig" do |
| 155 | + allow(RbConfig::CONFIG).to receive(:[]).with("ruby_version").and_return("3.3.0") |
| 156 | + expect(command_instance.send(:habitat_user_gem_dir)).to eq( |
| 157 | + File.expand_path("~/.chef/ruby/3.3.0/gems") |
| 158 | + ) |
| 159 | + end |
| 160 | + end |
| 161 | +end |
0 commit comments