Skip to content
Browse files

Moved dependency merge logic into `Dependency`

  • Loading branch information...
1 parent a5f339e commit 18c67c225403713e86973f76cc9d508d11c459e8 Ian Lesperance and Matthew Kane Parker committed Sep 10, 2012
View
25 lib/license_finder/dependency.rb
@@ -21,6 +21,31 @@ def initialize(attributes = {})
@readme_files = attributes['readme_files'] || []
end
+ def merge(other)
+ raise "Cannot merge dependencies with different names. Expected #{name}, was #{other.name}." unless other.name == name
+
+ merged = self.class.new(
+ 'name' => name,
+ 'version' => other.version,
+ 'license_files' => other.license_files,
+ 'readme_files' => other.readme_files,
+ 'license_url' => other.license_url,
+ 'notes' => notes,
+ 'source' => other.source
+ )
+
+ case other.license
+ when license, 'other'
+ merged.license = license
+ merged.approved = approved
+ else
+ merged.license = other.license
+ merged.approved = other.approved
+ end
+
+ merged
+ end
+
def to_yaml_entry
attrs = "- name: \"#{name}\"\n version: \"#{version}\"\n license: \"#{license}\"\n approved: #{approved}\n source: \"#{source}\"\n license_url: \"#{license_url}\"\n notes: \"#{notes}\"\n"
attrs << " license_files:\n"
View
27 lib/license_finder/dependency_list.rb
@@ -25,37 +25,16 @@ def self.from_yaml(yml)
def merge(new_list)
deps = new_list.dependencies.map do |new_dep|
- old_dep = self.dependencies.detect { |d| d.name == new_dep.name }
+ old_dep = dependencies.detect { |d| d.name == new_dep.name }
if old_dep
- dep = Dependency.new(
- 'name' => new_dep.name,
- 'version' => new_dep.version,
- 'license_files' => new_dep.license_files,
- 'readme_files' => new_dep.readme_files,
- 'license_url' => old_dep.license_url,
- 'notes' => old_dep.notes,
- 'source' => 'bundle'
- )
-
- case new_dep.license
- when old_dep.license
- dep.license = new_dep.license
- dep.approved = old_dep.approved || new_dep.approved
- when 'other'
- dep.license = old_dep.license
- dep.approved = old_dep.approved
- else
- dep = new_dep
- end
-
- dep
+ old_dep.merge(new_dep)
else
new_dep
end
end
- deps += self.dependencies.select {|d| d.source != "bundle" }
+ deps += dependencies.select { |d| d.source != "bundle" }
self.class.new(deps)
end
View
138 spec/lib/license_finder/dependency_list_spec.rb
@@ -105,96 +105,54 @@ def license
end
describe '#merge' do
- before(:each) do
- @yml_same = LicenseFinder::Dependency.new('name' => 'same_gem', 'version' => '1.2.3', 'license' => 'MIT', 'approved' => true, 'license_url' => 'a', 'notes' => 'b')
- @yml_updated = LicenseFinder::Dependency.new('name' => 'updated_gem', 'version' => '1.0.1', 'license' => 'MIT', 'approved' => true, 'license_url' => 'a', 'notes' => 'b')
- @yml_new_license = LicenseFinder::Dependency.new('name' => 'new_license_gem', 'version' => '1.0.1', 'license' => 'MIT', 'approved' => true, 'license_url' => 'a', 'notes' => 'b')
- @yml_manual_license = LicenseFinder::Dependency.new('name' => 'manual_license_gem', 'version' => '1.0.1', 'license' => 'Ruby', 'approved' => true, 'license_url' => 'a', 'notes' => 'b')
- @yml_removed_gem = LicenseFinder::Dependency.new('name' => 'removed_gem', 'version' => '1.0.1', 'license' => 'MIT', 'approved' => true, 'license_url' => 'a', 'notes' => 'b', 'source' => 'bundle')
- @yml_new_whitelist = LicenseFinder::Dependency.new('name' => 'new_whitelist_gem', 'version' => '1.0.1', 'license' => 'MIT', 'approved' => false, 'license_url' => 'a', 'notes' => 'b')
- @yml_non_bundled_dependency = LicenseFinder::Dependency.new('name' => 'non_bundled_dependency', 'version' => '1.0.1', 'license' => 'GPL', 'approved' => false, 'license_url' => 'a', 'notes' => 'b')
-
- @gemspec_same = LicenseFinder::Dependency.new('name' => 'same_gem', 'version' => '1.2.3', 'license' => 'MIT', 'approved' => false)
- @gemspec_new = LicenseFinder::Dependency.new('name' => 'brand_new_gem', 'version' => '0.9', 'license' => 'MIT', 'approved' => false)
- @gemspec_updated = LicenseFinder::Dependency.new('name' => 'updated_gem', 'version' => '1.1.2', 'license' => 'MIT', 'approved' => false)
- @gemspec_new_license = LicenseFinder::Dependency.new('name' => 'new_license_gem', 'version' => '2.0.1', 'license' => 'Apache 2.0', 'approved' => false)
- @gemspec_new_whitelist = LicenseFinder::Dependency.new('name' => 'new_whitelist_gem', 'version' => '1.0.1', 'license' => 'MIT', 'approved' => true)
- @gemspec_manual_license = LicenseFinder::Dependency.new('name' => 'manual_license_gem', 'version' => '1.2.1', 'license' => 'other', 'approved' => false)
-
- @list_from_yml = LicenseFinder::DependencyList.new([@yml_same, @yml_non_bundled_dependency, @yml_updated, @yml_new_license, @yml_removed_gem, @yml_new_whitelist, @yml_manual_license])
- @list_from_gemspec = LicenseFinder::DependencyList.new([@gemspec_same, @gemspec_new, @gemspec_updated, @gemspec_new_license, @gemspec_new_whitelist, @gemspec_manual_license])
- end
-
- it "should ignore existing gems with the same version" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'same_gem' }
- dep.approved.should == @yml_same.approved
- dep.license_url.should == 'a'
- dep.notes.should == 'b'
- end
-
- it "should keep old license value if gemspec license is other" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'manual_license_gem' }
- dep.license.should == @yml_manual_license.license
- dep.version.should == @gemspec_manual_license.version
- dep.approved.should == @yml_manual_license.approved
- dep.license_url.should == 'a'
- dep.notes.should == 'b'
- end
-
- it "should use the new source" do
- old_deps = [LicenseFinder::Dependency.new('name' => 'foo', 'license' => 'MIT')]
- new_deps = [LicenseFinder::Dependency.new('name' => 'foo', 'license' => 'other', 'source' => 'bundle')]
-
- list = LicenseFinder::DependencyList.new(old_deps).merge(LicenseFinder::DependencyList.new(new_deps))
- dep = list.dependencies.first
- dep.source.should == 'bundle'
- end
-
- it "should add new gem" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'brand_new_gem' }
- dep.should_not be_nil
- dep.version.should == @gemspec_new.version
- dep.approved.should == @gemspec_new.approved
- dep.license.should == @gemspec_new.license
- dep.license_url.should == ''
- dep.notes.should == ''
- end
-
- it "should update version if gem exists and license is the same" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'updated_gem' }
- dep.name.should == @gemspec_updated.name
- dep.approved.should == @yml_updated.approved
- dep.license_url.should == 'a'
- dep.notes.should == 'b'
- end
-
- it "should replace gem if version and license are different" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'new_license_gem' }
- dep.name.should == @gemspec_new_license.name
- dep.version.should == @gemspec_new_license.version
- dep.approved.should == @gemspec_new_license.approved
- dep.license_url.should == ''
- dep.notes.should == ''
- end
-
- it "should update approved if gemspec gem is approved" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'new_whitelist_gem' }
- dep.name.should == @gemspec_new_whitelist.name
- dep.version.should == @gemspec_new_whitelist.version
- dep.approved.should == @gemspec_new_whitelist.approved
- dep.license_url.should == 'a'
- dep.notes.should == 'b'
- end
-
- it "should remove gem if new list doesn't contain it" do
- dep = @list_from_yml.merge(@list_from_gemspec).dependencies.detect { |d| d.name == 'removed_gem' }
- dep.should be_nil
- end
-
- it "should keep any manually added dependencies that aren't part of the bundle" do
- dependencies = @list_from_yml.merge(@list_from_gemspec).dependencies
- dep = dependencies.detect { |d| d.name == 'non_bundled_dependency' }
- dep.should_not be_nil
+ let(:old_dep) do
+ LicenseFinder::Dependency.new(
+ 'name' => 'foo',
+ 'version' => '0.0.1',
+ 'source' => 'bundle'
+ )
+ end
+ let(:old_list) { LicenseFinder::DependencyList.new([old_dep]) }
+
+ let(:new_dep) do
+ LicenseFinder::Dependency.new(
+ 'name' => 'foo',
+ 'version' => '0.0.2',
+ 'source' => 'bundle'
+ )
+ end
+ let(:new_list) { LicenseFinder::DependencyList.new([new_dep]) }
+
+ it 'should merge dependencies with the same name' do
+ merged_list = old_list.merge(new_list)
+
+ merged_deps = merged_list.dependencies.select { |d| d.name == 'foo' }
+ merged_deps.should have(1).item
+
+ merged_dep = merged_deps.first
+ merged_dep.name.should == 'foo'
+ merged_dep.version.should == '0.0.2'
+ end
+
+ it 'should add new dependencies' do
+ new_dep.name = 'bar'
+
+ merged_list = old_list.merge(new_list)
+ merged_list.dependencies.should include(new_dep)
+ end
+
+ it 'should keep dependencies not originating from the bundle' do
+ old_dep.source = ''
+
+ merged_list = old_list.merge(new_list)
+ merged_list.dependencies.should include(old_dep)
+ end
+
+ it 'should remove dependencies missing from the bundle' do
+ old_dep.source = 'bundle'
+
+ merged_list = old_list.merge(new_list)
+ merged_list.dependencies.should_not include(old_dep)
end
end
View
75 spec/lib/license_finder/dependency_spec.rb
@@ -61,6 +61,81 @@
LicenseFinder::Dependency.new("source" => "foo").source.should == "foo"
end
end
+
+ describe '#merge' do
+ subject do
+ LicenseFinder::Dependency.new(
+ 'name' => 'foo',
+ 'license' => 'MIT',
+ 'version' => '0.0.1',
+ 'license_url' => 'http://www.example.com/license1.htm',
+ 'license_files' => "old license files",
+ 'readme_files' => "old readme files",
+ )
+ end
+
+ let(:new_dep) do
+ LicenseFinder::Dependency.new(
+ 'name' => 'foo',
+ 'license' => 'MIT',
+ 'version' => '0.0.2',
+ 'license_url' => 'http://www.example.com/license2.htm',
+ 'license_files' => "new license files",
+ 'readme_files' => "new readme files"
+ )
+ end
+
+ it 'should raise an error if the names do not match' do
+ new_dep.name = 'bar'
+
+ expect {
+ subject.merge(new_dep)
+ }.to raise_error
+ end
+
+ it 'should return the new version, license url, license files, readme files, and source' do
+ merged = subject.merge(new_dep)
+
+ merged.version.should == '0.0.2'
+ merged.license_url.should == 'http://www.example.com/license2.htm'
+ merged.license_files.should == new_dep.license_files
+ merged.readme_files.should == new_dep.readme_files
+ merged.source.should == new_dep.source
+ end
+
+ it 'should return the old notes' do
+ subject.notes = 'old notes'
+ new_dep.notes = 'new notes'
+
+ merged = subject.merge(new_dep)
+
+ merged.notes.should == 'old notes'
+ end
+
+ it 'should return the new license and approval if the license is different' do
+ subject.license = "MIT"
+ subject.approved = true
+
+ new_dep.license = "GPLv2"
+ new_dep.approved = false
+
+ merged = subject.merge(new_dep)
+
+ merged.license.should == "GPLv2"
+ merged.approved.should == false
+ end
+
+ it 'should return the old license and approval if the new license is the same or "other"' do
+ subject.approved = false
+ new_dep.approved = true
+
+ subject.merge(new_dep).approved.should == false
+
+ new_dep.license = 'other'
+
+ subject.merge(new_dep).approved.should == false
+ end
+ end
end

0 comments on commit 18c67c2

Please sign in to comment.
Something went wrong with that request. Please try again.