<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2010 Opscode, 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'
require 'chef/cookbook/metadata'

describe Chef::Cookbook::Metadata do
  before(:each) do
    @cookbook = Chef::CookbookVersion.new('test_cookbook')
    @meta = Chef::Cookbook::Metadata.new(@cookbook)
  end

  describe "when comparing for equality" do
    before do
      @fields = [ :name, :description, :long_description, :maintainer,
                  :maintainer_email, :license, :platforms, :dependencies,
                  :recommendations, :suggestions, :conflicting, :providing,
                  :replacing, :attributes, :groupings, :recipes, :version]
    end

    it "does not depend on object identity for equality" do
      @meta.should == @meta.dup
    end

    it "is not equal to another object if it isn't have all of the metadata fields" do
      @fields.each_index do |field_to_remove|
        fields_to_include = @fields.dup
        fields_to_include.delete_at(field_to_remove)
        almost_duck_type = Struct.new(*fields_to_include).new
        @fields.each do |field|
          setter = "#{field}="
          metadata_value = @meta.send(field)
          almost_duck_type.send(setter, metadata_value) if almost_duck_type.respond_to?(setter)
          @mets.should_not == almost_duck_type
        end
      end
    end

    it "is equal to another object if it has equal values for all metadata fields" do
      duck_type = Struct.new(*@fields).new
      @fields.each do |field|
        setter = "#{field}="
        metadata_value = @meta.send(field)
        duck_type.send(setter, metadata_value)
      end
      @meta.should == duck_type
    end

    it "is not equal if any values are different" do
      duck_type_class = Struct.new(*@fields)
      @fields.each do |field_to_change|
        duck_type = duck_type_class.new

        @fields.each do |field|
          setter = "#{field}="
          metadata_value = @meta.send(field)
          duck_type.send(setter, metadata_value)
        end

        field_to_change

        duck_type.send("#{field_to_change}=".to_sym, :epic_fail)
        @meta.should_not == duck_type
      end
    end

  end

  describe "when first created" do
    it "should return a Chef::Cookbook::Metadata object" do
      @meta.should be_a_kind_of(Chef::Cookbook::Metadata)
    end

    it "should allow a cookbook as the first argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook) }.should_not raise_error
    end

    it "should allow an maintainer name for the second argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>') }.should_not raise_error
    end

    it "should set the maintainer name from the second argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>')
      md.maintainer.should == '<NAME>'
    end

    it "should allow an maintainer email for the third argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>') }.should_not raise_error
    end

    it "should set the maintainer email from the third argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>')
      md.maintainer_email.should == '<EMAIL>'
    end

    it "should allow a license for the fourth argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>', 'Clown License v1') }.should_not raise_error
    end

    it "should set the license from the fourth argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>', 'Clown License v1')
      md.license.should == 'Clown License v1'
    end
  end

  describe "cookbook" do
    it "should return the cookbook we were initialized with" do
      @meta.cookbook.should eql(@cookbook)
    end
  end

  describe "name" do
    it "should return the name of the cookbook" do
      @meta.name.should eql(@cookbook.name)
    end
  end

  describe "platforms" do
    it "should return the current platform hash" do
      @meta.platforms.should be_a_kind_of(Hash)
    end
  end

  describe "adding a supported platform" do
    it "should support adding a supported platform with a single expression" do
      @meta.supports("ubuntu", ">= 8.04")
      @meta.platforms["ubuntu"].should == '>= 8.4.0'
    end
  end

  describe "meta-data attributes" do
    params = {
      :maintainer => "<NAME>",
      :maintainer_email => "<EMAIL>",
      :license => "Apache v2.0",
      :description => "Foobar!",
      :long_description => "Much Longer\nSeriously",
      :version => "0.6.0"
    }
    params.sort { |a,b| a.to_s <=> b.to_s }.each do |field, field_value|
      describe field do
        it "should be set-able via #{field}" do
          @meta.send(field, field_value).should eql(field_value)
        end
        it "should be get-able via #{field}" do
          @meta.send(field, field_value)
          @meta.send(field).should eql(field_value)
        end
      end
    end

    describe "version transformation" do
      it "should transform an '0.6' version to '0.6.0'" do
        @meta.send(:version, "0.6").should eql("0.6.0")
      end

      it "should spit out '0.6.0' after transforming '0.6'" do
        @meta.send(:version, "0.6")
        @meta.send(:version).should eql("0.6.0")
      end
    end
  end

  describe "describing dependencies (includes version trasnformation)" do
    dep_types = {
      :depends     => [ :dependencies, "foo::bar", "> 0.2", "> 0.2.0" ],
      :recommends  => [ :recommendations, "foo::bar", ">= 0.2", ">= 0.2.0" ],
      :suggests    => [ :suggestions, "foo::bar", "> 0.2", "> 0.2.0" ],
      :conflicts   => [ :conflicting, "foo::bar", "~> 0.2", "~> 0.2.0" ],<commit_msg>[CHEF-4298] "trasnformation" typo in tests<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright 2008-2010 Opscode, 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'
require 'chef/cookbook/metadata'

describe Chef::Cookbook::Metadata do
  before(:each) do
    @cookbook = Chef::CookbookVersion.new('test_cookbook')
    @meta = Chef::Cookbook::Metadata.new(@cookbook)
  end

  describe "when comparing for equality" do
    before do
      @fields = [ :name, :description, :long_description, :maintainer,
                  :maintainer_email, :license, :platforms, :dependencies,
                  :recommendations, :suggestions, :conflicting, :providing,
                  :replacing, :attributes, :groupings, :recipes, :version]
    end

    it "does not depend on object identity for equality" do
      @meta.should == @meta.dup
    end

    it "is not equal to another object if it isn't have all of the metadata fields" do
      @fields.each_index do |field_to_remove|
        fields_to_include = @fields.dup
        fields_to_include.delete_at(field_to_remove)
        almost_duck_type = Struct.new(*fields_to_include).new
        @fields.each do |field|
          setter = "#{field}="
          metadata_value = @meta.send(field)
          almost_duck_type.send(setter, metadata_value) if almost_duck_type.respond_to?(setter)
          @mets.should_not == almost_duck_type
        end
      end
    end

    it "is equal to another object if it has equal values for all metadata fields" do
      duck_type = Struct.new(*@fields).new
      @fields.each do |field|
        setter = "#{field}="
        metadata_value = @meta.send(field)
        duck_type.send(setter, metadata_value)
      end
      @meta.should == duck_type
    end

    it "is not equal if any values are different" do
      duck_type_class = Struct.new(*@fields)
      @fields.each do |field_to_change|
        duck_type = duck_type_class.new

        @fields.each do |field|
          setter = "#{field}="
          metadata_value = @meta.send(field)
          duck_type.send(setter, metadata_value)
        end

        field_to_change

        duck_type.send("#{field_to_change}=".to_sym, :epic_fail)
        @meta.should_not == duck_type
      end
    end

  end

  describe "when first created" do
    it "should return a Chef::Cookbook::Metadata object" do
      @meta.should be_a_kind_of(Chef::Cookbook::Metadata)
    end

    it "should allow a cookbook as the first argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook) }.should_not raise_error
    end

    it "should allow an maintainer name for the second argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>') }.should_not raise_error
    end

    it "should set the maintainer name from the second argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>')
      md.maintainer.should == '<NAME>'
    end

    it "should allow an maintainer email for the third argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>') }.should_not raise_error
    end

    it "should set the maintainer email from the third argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>')
      md.maintainer_email.should == '<EMAIL>'
    end

    it "should allow a license for the fourth argument" do
      lambda { Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>', 'Clown License v1') }.should_not raise_error
    end

    it "should set the license from the fourth argument" do
      md = Chef::Cookbook::Metadata.new(@cookbook, '<NAME>', '<EMAIL>', 'Clown License v1')
      md.license.should == 'Clown License v1'
    end
  end

  describe "cookbook" do
    it "should return the cookbook we were initialized with" do
      @meta.cookbook.should eql(@cookbook)
    end
  end

  describe "name" do
    it "should return the name of the cookbook" do
      @meta.name.should eql(@cookbook.name)
    end
  end

  describe "platforms" do
    it "should return the current platform hash" do
      @meta.platforms.should be_a_kind_of(Hash)
    end
  end

  describe "adding a supported platform" do
    it "should support adding a supported platform with a single expression" do
      @meta.supports("ubuntu", ">= 8.04")
      @meta.platforms["ubuntu"].should == '>= 8.4.0'
    end
  end

  describe "meta-data attributes" do
    params = {
      :maintainer => "<NAME>",
      :maintainer_email => "<EMAIL>",
      :license => "Apache v2.0",
      :description => "Foobar!",
      :long_description => "Much Longer\nSeriously",
      :version => "0.6.0"
    }
    params.sort { |a,b| a.to_s <=> b.to_s }.each do |field, field_value|
      describe field do
        it "should be set-able via #{field}" do
          @meta.send(field, field_value).should eql(field_value)
        end
        it "should be get-able via #{field}" do
          @meta.send(field, field_value)
          @meta.send(field).should eql(field_value)
        end
      end
    end

    describe "version transformation" do
      it "should transform an '0.6' version to '0.6.0'" do
        @meta.send(:version, "0.6").should eql("0.6.0")
      end

      it "should spit out '0.6.0' after transforming '0.6'" do
        @meta.send(:version, "0.6")
        @meta.send(:version).should eql("0.6.0")
      end
    end
  end

  describe "describing dependencies (includes version transformation)" do
    dep_types = {
      :depends     => [ :dependencies, "foo::bar", "> 0.2", "> 0.2.0" ],
      :recommends  => [ :recommendations, "foo::bar", ">= 0.2", ">= 0.2.0" ],
      :suggests    => [ :suggestions, "foo::bar", "> 0.2", "> 0.2.0" ],
      :conflicts   => [ :conflicting, "foo::bar", "~> 0.2", "~> 0.2.0" ],
{'fraction_non_alphanumeric': 0.12382159550027727, 'fraction_numerical': 0.011090865879743325, 'mean_word_length': 2.8962962962962964, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 85, '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': '2869917', 'n_tokens_mistral': 4103, 'n_tokens_neox': 3914, 'n_words': 1178}