<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::CookbookLoader do
  before do
    allow(ChefConfig).to receive(:windows?) { false }
  end
  let(:repo_paths) do
    [
      File.expand_path(File.join(CHEF_SPEC_DATA, "kitchen")),
      File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")),
    ]
  end

  let(:cookbook_loader) { Chef::CookbookLoader.new(repo_paths) }

  def full_paths_for_part(cb, part)
    cookbook_loader[cb].files_for(part).inject([]) { |memo, f| memo << f[:full_path]; memo }
  end

  it "checks each directory only once when loading (CHEF-3487)" do
    cookbook_paths = []
    repo_paths.each do |repo_path|
      cookbook_paths |= Dir[File.join(repo_path, "*")]
    end

    cookbook_paths.delete_if { |path| File.basename(path) == "chefignore" }

    cookbook_paths.each do |cookbook_path|
      expect(Chef::Cookbook::CookbookVersionLoader).to receive(:new).
        with(cookbook_path, anything).
        once.
        and_call_original
    end
    expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
    cookbook_loader.load_cookbooks
  end

  context "after loading all cookbooks" do
    before(:each) do
      expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
      cookbook_loader.load_cookbooks
    end

    it "should be possible to reload all the cookbooks without triggering deprecation warnings on all of them" do
      start_merged_cookbooks = cookbook_loader.merged_cookbooks
      expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
      cookbook_loader.load_cookbooks
      expect(cookbook_loader.merged_cookbooks).to eql(start_merged_cookbooks)
    end

    describe "[]" do
      it "should return cookbook objects with []" do
        expect(cookbook_loader[:openldap]).to be_a_kind_of(Chef::CookbookVersion)
      end

      it "should raise an exception if it cannot find a cookbook with []" do
        expect { cookbook_loader[:monkeypoop] }.to raise_error(Chef::Exceptions::CookbookNotFoundInRepo)
      end

      it "should allow you to look up available cookbooks with [] and a symbol" do
        expect(cookbook_loader[:openldap].name).to eql(:openldap)
      end

      it "should allow you to look up available cookbooks with [] and a string" do
        expect(cookbook_loader["openldap"].name).to eql(:openldap)
      end
    end

    describe "each" do
      it "should allow you to iterate over cookbooks with each" do
        seen = Hash.new
        cookbook_loader.each do |cookbook_name, cookbook|
          seen[cookbook_name] = true
        end
        expect(seen).to have_key("openldap")
        expect(seen).to have_key("apache2")
      end

      it "should iterate in alphabetical order" do
        seen = Array.new
        cookbook_loader.each do |cookbook_name, cookbook|
          seen << cookbook_name
        end
        expect(seen[0]).to eq("angrybash")
        expect(seen[1]).to eq("apache2")
        expect(seen[2]).to eq("borken")
        expect(seen[3]).to eq("ignorken")
        expect(seen[4]).to eq("java")
        expect(seen[5]).to eq("name-mismatch")
        expect(seen[6]).to eq("openldap")
      end
    end

    describe "referencing cookbook files" do
      it "should find all the cookbooks in the cookbook path" do
        expect(cookbook_loader).to have_key(:openldap)
        expect(cookbook_loader).to have_key(:apache2)
      end

      it "should allow you to override an attribute file via cookbook_path" do
        expect(full_paths_for_part(:openldap, "attributes").detect do |f|
          f =~ /cookbooks\/openldap\/attributes\/default.rb/
        end).not_to eql(nil)
        expect(full_paths_for_part(:openldap, "attributes").detect do |f|
          f =~ /kitchen\/openldap\/attributes\/default.rb/
        end).to eql(nil)
      end

      it "should load different attribute files from deeper paths" do<commit_msg>Fix a test I broke by adding a new fixture cookbook.<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::CookbookLoader do
  before do
    allow(ChefConfig).to receive(:windows?) { false }
  end
  let(:repo_paths) do
    [
      File.expand_path(File.join(CHEF_SPEC_DATA, "kitchen")),
      File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks")),
    ]
  end

  let(:cookbook_loader) { Chef::CookbookLoader.new(repo_paths) }

  def full_paths_for_part(cb, part)
    cookbook_loader[cb].files_for(part).inject([]) { |memo, f| memo << f[:full_path]; memo }
  end

  it "checks each directory only once when loading (CHEF-3487)" do
    cookbook_paths = []
    repo_paths.each do |repo_path|
      cookbook_paths |= Dir[File.join(repo_path, "*")]
    end

    cookbook_paths.delete_if { |path| File.basename(path) == "chefignore" }

    cookbook_paths.each do |cookbook_path|
      expect(Chef::Cookbook::CookbookVersionLoader).to receive(:new).
        with(cookbook_path, anything).
        once.
        and_call_original
    end
    expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
    cookbook_loader.load_cookbooks
  end

  context "after loading all cookbooks" do
    before(:each) do
      expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
      cookbook_loader.load_cookbooks
    end

    it "should be possible to reload all the cookbooks without triggering deprecation warnings on all of them" do
      start_merged_cookbooks = cookbook_loader.merged_cookbooks
      expect(Chef::Log).to receive(:deprecation).with(/The cookbook\(s\): openldap exist in multiple places in your cookbook_path./)
      cookbook_loader.load_cookbooks
      expect(cookbook_loader.merged_cookbooks).to eql(start_merged_cookbooks)
    end

    describe "[]" do
      it "should return cookbook objects with []" do
        expect(cookbook_loader[:openldap]).to be_a_kind_of(Chef::CookbookVersion)
      end

      it "should raise an exception if it cannot find a cookbook with []" do
        expect { cookbook_loader[:monkeypoop] }.to raise_error(Chef::Exceptions::CookbookNotFoundInRepo)
      end

      it "should allow you to look up available cookbooks with [] and a symbol" do
        expect(cookbook_loader[:openldap].name).to eql(:openldap)
      end

      it "should allow you to look up available cookbooks with [] and a string" do
        expect(cookbook_loader["openldap"].name).to eql(:openldap)
      end
    end

    describe "each" do
      it "should allow you to iterate over cookbooks with each" do
        seen = Hash.new
        cookbook_loader.each do |cookbook_name, cookbook|
          seen[cookbook_name] = true
        end
        expect(seen).to have_key("openldap")
        expect(seen).to have_key("apache2")
      end

      it "should iterate in alphabetical order" do
        seen = Array.new
        cookbook_loader.each do |cookbook_name, cookbook|
          seen << cookbook_name
        end
        expect(seen).to eq %w{aliased angrybash apache2 borken ignorken java name-mismatch openldap preseed supports-platform-constraints}
      end
    end

    describe "referencing cookbook files" do
      it "should find all the cookbooks in the cookbook path" do
        expect(cookbook_loader).to have_key(:openldap)
        expect(cookbook_loader).to have_key(:apache2)
      end

      it "should allow you to override an attribute file via cookbook_path" do
        expect(full_paths_for_part(:openldap, "attributes").detect do |f|
          f =~ /cookbooks\/openldap\/attributes\/default.rb/
        end).not_to eql(nil)
        expect(full_paths_for_part(:openldap, "attributes").detect do |f|
          f =~ /kitchen\/openldap\/attributes\/default.rb/
        end).to eql(nil)
      end

      it "should load different attribute files from deeper paths" do
{'fraction_non_alphanumeric': 0.10063162402312387, 'fraction_numerical': 0.0052456910395032655, 'mean_word_length': 3.297148114075437, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3497844', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2794, 'n_words': 825}