diff options
author | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2015-03-16 16:06:00 -0400 |
---|---|---|
committer | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2015-03-20 11:22:30 -0400 |
commit | d2dd332132fef834a55b7ad21eba6b2312cc5a27 (patch) | |
tree | 07ed62f2501980f5dbf841f662f2c2f48633178e /spec | |
parent | 8adb5e38a31f47d84fcac6c825b5bd9279ba6aac (diff) | |
download | chef-d2dd332132fef834a55b7ad21eba6b2312cc5a27.tar.gz |
Fixup all specs to reference PathHelper.home.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/config_spec.rb | 10 | ||||
-rw-r--r-- | spec/unit/knife/bootstrap_spec.rb | 28 | ||||
-rw-r--r-- | spec/unit/knife/core/subcommand_loader_spec.rb | 58 | ||||
-rw-r--r-- | spec/unit/provider/service/macosx_spec.rb | 6 | ||||
-rw-r--r-- | spec/unit/shell_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/workstation_config_loader_spec.rb | 2 |
6 files changed, 56 insertions, 50 deletions
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb index 06178f7733..6ea67246b5 100644 --- a/spec/unit/config_spec.rb +++ b/spec/unit/config_spec.rb @@ -360,18 +360,12 @@ describe Chef::Config do describe "Chef::Config[:user_home]" do it "should set when HOME is provided" do expected = to_platform("/home/kitten") - allow(Chef::Config).to receive(:env).and_return({ 'HOME' => expected }) - expect(Chef::Config[:user_home]).to eq(expected) - end - - it "should be set when only USERPROFILE is provided" do - expected = to_platform("/users/kitten") - allow(Chef::Config).to receive(:env).and_return({ 'USERPROFILE' => expected }) + allow(Chef::Util::PathHelper).to receive(:home).and_return(expected) expect(Chef::Config[:user_home]).to eq(expected) end it "falls back to the current working directory when HOME and USERPROFILE is not set" do - allow(Chef::Config).to receive(:env).and_return({}) + allow(Chef::Util::PathHelper).to receive(:home).and_return(nil) expect(Chef::Config[:user_home]).to eq(Dir.pwd) end end diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb index 848af11db5..f1ca510ed3 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -115,7 +115,7 @@ describe Chef::Knife::Bootstrap do end def configure_env_home - ENV['HOME'] = "/env/home" + allow(Chef::Util::PathHelper).to receive(:home).with(".chef", "bootstrap", "example.erb").and_yield(env_home_template_path) end def configure_gem_files @@ -123,15 +123,9 @@ describe Chef::Knife::Bootstrap do end before(:each) do - @original_home = ENV['HOME'] - ENV['HOME'] = nil expect(File).to receive(:exists?).with(bootstrap_template).and_return(false) end - after(:each) do - ENV['HOME'] = @original_home - end - context "when file is available everywhere" do before do configure_chef_config_dir @@ -161,7 +155,7 @@ describe Chef::Knife::Bootstrap do end end - context "when file is available in ENV['HOME']" do + context "when file is available in home directory" do before do configure_chef_config_dir configure_env_home @@ -180,7 +174,25 @@ describe Chef::Knife::Bootstrap do context "when file is available in Gem files" do before do configure_chef_config_dir + configure_env_home + configure_gem_files + + expect(File).to receive(:exists?).with(builtin_template_path).and_return(false) + expect(File).to receive(:exists?).with(chef_config_dir_template_path).and_return(false) + expect(File).to receive(:exists?).with(env_home_template_path).and_return(false) + expect(File).to receive(:exists?).with(gem_files_template_path).and_return(true) + end + + it "should load the template from Gem files" do + expect(knife.find_template).to eq(gem_files_template_path) + end + end + + context "when file is available in Gem files and home dir doesn't exist" do + before do + configure_chef_config_dir configure_gem_files + allow(Chef::Util::PathHelper).to receive(:home).with(".chef", "bootstrap", "example.erb").and_return(nil) expect(File).to receive(:exists?).with(builtin_template_path).and_return(false) expect(File).to receive(:exists?).with(chef_config_dir_template_path).and_return(false) diff --git a/spec/unit/knife/core/subcommand_loader_spec.rb b/spec/unit/knife/core/subcommand_loader_spec.rb index df42cff2ea..7f9308b28a 100644 --- a/spec/unit/knife/core/subcommand_loader_spec.rb +++ b/spec/unit/knife/core/subcommand_loader_spec.rb @@ -19,23 +19,29 @@ require 'spec_helper' describe Chef::Knife::SubcommandLoader do + let(:loader) { Chef::Knife::SubcommandLoader.new(File.join(CHEF_SPEC_DATA, 'knife-site-subcommands')) } + let(:home) { File.join(CHEF_SPEC_DATA, 'knife-home') } + let(:plugin_dir) { File.join(home, '.chef', 'plugins', 'knife') } + before do allow(Chef::Platform).to receive(:windows?) { false } - @home = File.join(CHEF_SPEC_DATA, 'knife-home') - @env = {'HOME' => @home} - @loader = Chef::Knife::SubcommandLoader.new(File.join(CHEF_SPEC_DATA, 'knife-site-subcommands'), @env) + Chef::Util::PathHelper.class_variable_set(:@@home_dir, home) + end + + after do + Chef::Util::PathHelper.class_variable_set(:@@home_dir, nil) end it "builds a list of the core subcommand file require paths" do - expect(@loader.subcommand_files).not_to be_empty - @loader.subcommand_files.each do |require_path| + expect(loader.subcommand_files).not_to be_empty + loader.subcommand_files.each do |require_path| expect(require_path).to match(/chef\/knife\/.*|plugins\/knife\/.*/) end end it "finds files installed via rubygems" do - expect(@loader.find_subcommands_via_rubygems).to include('chef/knife/node_create') - @loader.find_subcommands_via_rubygems.each {|rel_path, abs_path| expect(abs_path).to match(%r[chef/knife/.+])} + expect(loader.find_subcommands_via_rubygems).to include('chef/knife/node_create') + loader.find_subcommands_via_rubygems.each {|rel_path, abs_path| expect(abs_path).to match(%r[chef/knife/.+])} end it "finds files from latest version of installed gems" do @@ -54,23 +60,23 @@ describe Chef::Knife::SubcommandLoader do expect(gems[0]).to receive(:full_gem_path).and_return('/usr/lib/ruby/gems/knife-ec2-0.5.12') expect(Dir).to receive(:[]).with('/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/*.rb').and_return(gem_files) end - expect(@loader).to receive(:find_subcommands_via_dirglob).and_return({}) - expect(@loader.find_subcommands_via_rubygems.values.select { |file| file =~ /knife-ec2/ }.sort).to eq(gem_files) + expect(loader).to receive(:find_subcommands_via_dirglob).and_return({}) + expect(loader.find_subcommands_via_rubygems.values.select { |file| file =~ /knife-ec2/ }.sort).to eq(gem_files) end it "finds files using a dirglob when rubygems is not available" do - expect(@loader.find_subcommands_via_dirglob).to include('chef/knife/node_create') - @loader.find_subcommands_via_dirglob.each {|rel_path, abs_path| expect(abs_path).to match(%r[chef/knife/.+])} + expect(loader.find_subcommands_via_dirglob).to include('chef/knife/node_create') + loader.find_subcommands_via_dirglob.each {|rel_path, abs_path| expect(abs_path).to match(%r[chef/knife/.+])} end it "finds user-specific subcommands in the user's ~/.chef directory" do - expected_command = File.join(@home, '.chef', 'plugins', 'knife', 'example_home_subcommand.rb') - expect(@loader.site_subcommands).to include(expected_command) + expected_command = File.join(home, '.chef', 'plugins', 'knife', 'example_home_subcommand.rb') + expect(loader.site_subcommands).to include(expected_command) end it "finds repo specific subcommands by searching for a .chef directory" do expected_command = File.join(CHEF_SPEC_DATA, 'knife-site-subcommands', 'plugins', 'knife', 'example_subcommand.rb') - expect(@loader.site_subcommands).to include(expected_command) + expect(loader.site_subcommands).to include(expected_command) end # https://github.com/opscode/chef-dk/issues/227 @@ -137,25 +143,19 @@ describe Chef::Knife::SubcommandLoader do end before do - expect(@loader).to receive(:find_files_latest_gems).with("chef/knife/*.rb").and_return(all_found_commands) - expect(@loader).to receive(:find_subcommands_via_dirglob).and_return({}) + expect(loader).to receive(:find_files_latest_gems).with("chef/knife/*.rb").and_return(all_found_commands) + expect(loader).to receive(:find_subcommands_via_dirglob).and_return({}) end it "ignores commands from the non-matching gem install" do - expect(@loader.find_subcommands_via_rubygems.values).to eq(expected_valid_commands) + expect(loader.find_subcommands_via_rubygems.values).to eq(expected_valid_commands) end end describe "finding 3rd party plugins" do - let(:env_home) { "/home/alice" } - let(:manifest_path) { env_home + "/.chef/plugin_manifest.json" } - - before do - env_dup = ENV.to_hash - allow(ENV).to receive(:[]) { |key| env_dup[key] } - allow(ENV).to receive(:[]).with("HOME").and_return(env_home) - end + let(:home) { "/home/alice" } + let(:manifest_path) { home + "/.chef/plugin_manifest.json" } context "when there is not a ~/.chef/plugin_manifest.json file" do before do @@ -168,14 +168,14 @@ describe Chef::Knife::SubcommandLoader do else expect(Gem.source_index).to receive(:latest_specs).and_call_original end - @loader.subcommand_files.each do |require_path| + loader.subcommand_files.each do |require_path| expect(require_path).to match(/chef\/knife\/.*|plugins\/knife\/.*/) end end context "and HOME environment variable is not set" do before do - allow(ENV).to receive(:[]).with("HOME").and_return(nil) + allow(Chef::Util::PathHelper).to receive(:home).and_return(nil) end it "searches rubygems for plugins" do @@ -184,7 +184,7 @@ describe Chef::Knife::SubcommandLoader do else expect(Gem.source_index).to receive(:latest_specs).and_call_original end - @loader.subcommand_files.each do |require_path| + loader.subcommand_files.each do |require_path| expect(require_path).to match(/chef\/knife\/.*|plugins\/knife\/.*/) end end @@ -215,7 +215,7 @@ describe Chef::Knife::SubcommandLoader do it "uses paths from the manifest instead of searching gems" do expect(Gem::Specification).not_to receive(:latest_specs).and_call_original - expect(@loader.subcommand_files).to include(ec2_server_create_plugin) + expect(loader.subcommand_files).to include(ec2_server_create_plugin) end end diff --git a/spec/unit/provider/service/macosx_spec.rb b/spec/unit/provider/service/macosx_spec.rb index fb751592df..9905a6e4ae 100644 --- a/spec/unit/provider/service/macosx_spec.rb +++ b/spec/unit/provider/service/macosx_spec.rb @@ -22,17 +22,17 @@ describe Chef::Provider::Service::Macosx do describe ".gather_plist_dirs" do context "when HOME directory is set" do before do - allow(ENV).to receive(:[]).with('HOME').and_return("/User/someuser") + allow(Chef::Util::PathHelper).to receive(:home).with('Library', 'LaunchAgents').and_yield('/Users/someuser/Library/LaunchAgents') end it "includes users's LaunchAgents folder" do - expect(described_class.gather_plist_dirs).to include("#{ENV['HOME']}/Library/LaunchAgents") + expect(described_class.gather_plist_dirs).to include("/Users/someuser/Library/LaunchAgents") end end context "when HOME directory is not set" do before do - allow(ENV).to receive(:[]).with('HOME').and_return(nil) + allow(Chef::Util::PathHelper).to receive(:home).with('Library', 'LaunchAgents').and_return(nil) end it "doesn't include user's LaunchAgents folder" do diff --git a/spec/unit/shell_spec.rb b/spec/unit/shell_spec.rb index 0e028f4359..617abcfde2 100644 --- a/spec/unit/shell_spec.rb +++ b/spec/unit/shell_spec.rb @@ -56,7 +56,7 @@ describe Shell do describe "configuring IRB" do it "configures irb history" do Shell.configure_irb - expect(Shell.irb_conf[:HISTORY_FILE]).to eq("~/.chef/chef_shell_history") + expect(Shell.irb_conf[:HISTORY_FILE]).to eq("#{ENV['HOME']}/.chef/chef_shell_history") expect(Shell.irb_conf[:SAVE_HISTORY]).to eq(1000) end diff --git a/spec/unit/workstation_config_loader_spec.rb b/spec/unit/workstation_config_loader_spec.rb index a865103188..72631f3dfa 100644 --- a/spec/unit/workstation_config_loader_spec.rb +++ b/spec/unit/workstation_config_loader_spec.rb @@ -65,7 +65,7 @@ describe Chef::WorkstationConfigLoader do let(:home) { "/Users/example.user" } before do - env["HOME"] = home + allow(Chef::Util::PathHelper).to receive(:home).with('.chef').and_yield(File.join(home, '.chef')) allow(config_loader).to receive(:path_exists?).with("#{home}/.chef/knife.rb").and_return(true) end |