From 95338950ee96d7cb5750c0a0098a034a6f7002de Mon Sep 17 00:00:00 2001 From: tariq1890 Date: Mon, 30 Jul 2018 00:12:37 -0700 Subject: [PATCH] Fixing an edge case in lockdiff where all the projects may be removed from the lock file. --- gps/verify/lockdiff.go | 41 +++++++++++++++++++++---------------- gps/verify/lockdiff_test.go | 4 ++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/gps/verify/lockdiff.go b/gps/verify/lockdiff.go index 525a46db0e..3804540cf8 100644 --- a/gps/verify/lockdiff.go +++ b/gps/verify/lockdiff.go @@ -95,26 +95,31 @@ func DiffLocks(l1, l2 gps.Lock) LockDelta { Name: pr1, } - for i2 := i2next; i2 < len(p2); i2++ { - lp2 := p2[i2] - pr2 := lp2.Ident().ProjectRoot - - switch strings.Compare(string(pr1), string(pr2)) { - case 0: // Found a matching project - lpd.LockedProjectPropertiesDelta = DiffLockedProjectProperties(lp1, lp2) - i2next = i2 + 1 // Don't visit this project again - case +1: // Found a new project - diff.ProjectDeltas[pr2] = LockedProjectDelta{ - Name: pr2, - ProjectAdded: true, + // Edge case: If there are no lockedProjects on the RHS, they have most probably been removed from the lock file. + if len(p2) == 0 { + lpd.ProjectRemoved = true + } else { + for i2 := i2next; i2 < len(p2); i2++ { + lp2 := p2[i2] + pr2 := lp2.Ident().ProjectRoot + + switch strings.Compare(string(pr1), string(pr2)) { + case 0: // Found a matching project + lpd.LockedProjectPropertiesDelta = DiffLockedProjectProperties(lp1, lp2) + i2next = i2 + 1 // Don't visit this project again + case +1: // Found a new project + diff.ProjectDeltas[pr2] = LockedProjectDelta{ + Name: pr2, + ProjectAdded: true, + } + i2next = i2 + 1 // Don't visit this project again + continue // Keep looking for a matching project + case -1: // Project has been removed, handled below + lpd.ProjectRemoved = true } - i2next = i2 + 1 // Don't visit this project again - continue // Keep looking for a matching project - case -1: // Project has been removed, handled below - lpd.ProjectRemoved = true - } - break // Done evaluating this project, move onto the next + break // Done evaluating this project, move onto the next + } } diff.ProjectDeltas[pr1] = lpd diff --git a/gps/verify/lockdiff_test.go b/gps/verify/lockdiff_test.go index 52955a392d..a368f8c76c 100644 --- a/gps/verify/lockdiff_test.go +++ b/gps/verify/lockdiff_test.go @@ -118,6 +118,10 @@ func TestLockDelta(t *testing.T) { lt: dup.addII("other.org").rmII("baz.com/qux").addDumbProject("zebrafun.org").rmProject("foo.com/bar"), delta: InputImportsChanged | ProjectRemoved | ProjectAdded, }, + "remove all projects and imports": { + lt: dup.rmII("baz.com/qux").rmII("foo.com/bar").rmProject("baz.com/qux").rmProject("foo.com/bar").rmProject("transitive.com/dependency"), + delta: InputImportsChanged | ProjectRemoved, + }, } for name, fix := range tt {