From a8e6141cd7ac7ed2aa10b35c55954a48bacf3523 Mon Sep 17 00:00:00 2001 From: Daniil Kouznetsov Date: Tue, 16 Jan 2018 11:47:14 -0500 Subject: [PATCH] [Fixed] Duplicate approvals in decisions file Signed-off-by: Shane Lattanzio --- lib/license_finder/decisions.rb | 12 +++++------- spec/lib/license_finder/decisions_spec.rb | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/license_finder/decisions.rb b/lib/license_finder/decisions.rb index 5973ca57a..29a069c89 100644 --- a/lib/license_finder/decisions.rb +++ b/lib/license_finder/decisions.rb @@ -51,9 +51,9 @@ def ignored_group?(name) ####### TXN = Struct.new(:who, :why, :safe_when, :safe_versions) do - def self.from_hash(txn) - new(txn[:who], txn[:why], txn[:when], txn[:versions].nil? ? [] : txn[:versions]) - end + def self.from_hash(txn, versions) + new(txn[:who], txn[:why], txn[:when], versions || []) + end end def initialize @@ -96,10 +96,8 @@ def approve(name, txn = {}) versions = [] versions = @approvals[name][:safe_versions] if @approvals.key?(name) - - @approvals[name] = TXN.from_hash(txn) - - @approvals[name][:safe_versions].concat(versions) + @approvals[name] = TXN.from_hash(txn, versions) + @approvals[name][:safe_versions].concat(txn[:versions]) unless txn[:versions].nil? self end diff --git a/spec/lib/license_finder/decisions_spec.rb b/spec/lib/license_finder/decisions_spec.rb index 231f36d04..646a52621 100644 --- a/spec/lib/license_finder/decisions_spec.rb +++ b/spec/lib/license_finder/decisions_spec.rb @@ -300,14 +300,29 @@ def roundtrip(decisions) expect(licenses).to eq [License.find_by_name('GPL')].to_set end - it 'can restore approvals' do + + it 'can restore approvals without versions' do time = Time.now.getutc - decisions = roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time)) - expect(decisions).to be_approved('dep') - approval = decisions.approval_of('dep') + roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time)) + + approval = subject.approval_of('dep') + expect(approval.who).to eq 'Somebody' + expect(approval.why).to eq 'Some reason' + expect(approval.safe_when).to eq time + expect(approval.safe_versions).to eq [] + end + + it 'can restore approvals with versions' do + time = Time.now.getutc + roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['1.0'])) + roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['2.0'])) + roundtrip(subject.approve('dep', who: 'Somebody', why: 'Some reason', when: time, versions: ['3.0'])) + + approval = subject.approval_of('dep', '1.0') expect(approval.who).to eq 'Somebody' expect(approval.why).to eq 'Some reason' expect(approval.safe_when).to eq time + expect(approval.safe_versions).to eq ['1.0', '2.0', '3.0'] end it 'can restore unapprovals' do