summaryrefslogtreecommitdiff
path: root/spec/unit/knife/core/gem_glob_loader_spec.rb
diff options
context:
space:
mode:
authorChef Expeditor <chef-ci@chef.io>2021-03-11 17:45:48 +0000
committerMarc A. Paradise <marc.paradise@gmail.com>2021-03-31 13:52:49 -0400
commit01c6a9e1753bd4b2166efe747151ba8700ebc0f5 (patch)
tree55f16085d89859139211bf6d2b8fd2dbcf4633b0 /spec/unit/knife/core/gem_glob_loader_spec.rb
parent497dd4af19ea7dff9a72d704a1de8e9be36b2996 (diff)
downloadchef-01c6a9e1753bd4b2166efe747151ba8700ebc0f5.tar.gz
Move knife to its own gem
This moves knife into /knife, in the same way that chef-utils and chef-config are separated. NOTES: == File History == If you see this message as the first message in the history of an knife file, you can see the complete history by using 'git log --follow', 'git config log.follow true' to make it the default behavior in this repository, or 'git config --globa log.follow true' to make it the global default. == API Changes == At the API level, there is one breaking change: CookbookSiteStreamingUploader has been moved out of chef and into knife/core. There were a combination of reasons we chose this path: - CookbookSiteStreamingUploader (CSSU) is only used within Knife. - CookbookSiteStreamingUploader (CSSU) references the command Chef::Knife::CookbookMetadata in order to generate a metadata file for the cookbook to upload - Chef::Knife::CookbookMetadata is no longer available to Chef:: because Knife has been moved to its own gem. Knife gem depends on the Chef gem, so Chef can't depend on something in Knife. A search for usage in related projects (berks, chef-cli) and the Internet at large shows that there are no known external consumers of CSSU. For now, we'll move this class into Knife::Core, as it's going to be faster than splitting off the metadata generation and time is a concern. If we find that we need the metadata generation in chef/ proper, we should evaluate decoupling that functionality from Knife::CookbookMetadata and exposing it via something like `Chef::Cookbook::Metadata#from_cookbook_files` == spec changes == The specs are kept in their existing locations, though we have separated out a `knife_spec_helper` so that we can ensure knife is not directly accessing chef requires; and chef is relying on knife's at all. We also now clear gem paths with each test, to force gem state to reset. This works around a problem where a combination of tests is corrupting the internal Gem state, causing failures in rubygems_spec. See branch `mp/broken-gems` for many more details around findings so far. Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
Diffstat (limited to 'spec/unit/knife/core/gem_glob_loader_spec.rb')
-rw-r--r--spec/unit/knife/core/gem_glob_loader_spec.rb39
1 files changed, 36 insertions, 3 deletions
diff --git a/spec/unit/knife/core/gem_glob_loader_spec.rb b/spec/unit/knife/core/gem_glob_loader_spec.rb
index f0c29b86a0..a6a94cc57a 100644
--- a/spec/unit/knife/core/gem_glob_loader_spec.rb
+++ b/spec/unit/knife/core/gem_glob_loader_spec.rb
@@ -15,7 +15,7 @@
# limitations under the License.
#
-require "spec_helper"
+require "knife_spec_helper"
describe Chef::Knife::SubcommandLoader::GemGlobLoader do
let(:loader) { Chef::Knife::SubcommandLoader::GemGlobLoader.new(File.join(CHEF_SPEC_DATA, "knife-site-subcommands")) }
@@ -24,11 +24,11 @@ describe Chef::Knife::SubcommandLoader::GemGlobLoader do
before do
allow(ChefUtils).to receive(:windows?) { false }
- Chef::Util::PathHelper.class_variable_set(:@@home_dir, home)
+ ChefConfig::PathHelper.class_variable_set(:@@home_dir, home)
end
after do
- Chef::Util::PathHelper.class_variable_set(:@@home_dir, nil)
+ ChefConfig::PathHelper.class_variable_set(:@@home_dir, nil)
end
it "builds a list of the core subcommand file require paths" do
@@ -62,12 +62,42 @@ describe Chef::Knife::SubcommandLoader::GemGlobLoader do
expect(loader).to receive(:find_subcommands_via_dirglob).and_return({})
expect(loader.subcommand_files.select { |file| file.include?("knife-ec2") }.sort).to eq(gem_files)
end
+ it "excludes knife version file if loaded from a gem" do
+ gems = [ double("knife-ec2-0.5.12") ]
+ gem_files = [
+ "/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/ec2_base.rb",
+ "/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/ec2_otherstuff.rb",
+ "/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/version.rb",
+ ]
+ expected_files = [
+ "/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/ec2_base.rb",
+ "/usr/lib/ruby/gems/knife-ec2-0.5.12/lib/chef/knife/ec2_otherstuff.rb"
+ ]
+
+ expect($LOAD_PATH).to receive(:map).and_return([])
+ if Gem::Specification.respond_to? :latest_specs
+ expect(Gem::Specification).to receive(:latest_specs).with(true).and_return(gems)
+ expect(gems[0]).to receive(:matches_for_glob).with(%r{chef/knife/\*\.rb\{(.*),\.rb,(.*)\}}).and_return(gem_files)
+ else
+ expect(Gem.source_index).to receive(:latest_specs).with(true).and_return(gems)
+ expect(gems[0]).to receive(:require_paths).twice.and_return(["lib"])
+ 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.subcommand_files.select { |file| file.include?("knife-ec2") }.sort).to eq(expected_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_value { |abs_path| expect(abs_path).to match(%r{chef/knife/.+}) }
end
+ it "excludes chef/knife/version.rb using a dirglob when rubygems is not available" do
+ expect(loader.find_subcommands_via_dirglob).to_not include("chef/knife/version")
+ loader.find_subcommands_via_dirglob.each_value { |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)
@@ -86,6 +116,9 @@ describe Chef::Knife::SubcommandLoader::GemGlobLoader do
# source tree of the "primary" chef install, it can be loaded and cause an
# error. We also want to ensure that we only load builtin commands from the
# "primary" chef install.
+ #
+ # NOTE - we need to revisit coverage now that we're moving knife to its own gem;
+ # or remove this test if it's no longer a supported scenario.
context "when a different version of chef is also installed as a gem" do
let(:all_found_commands) do