Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit fd4fa35

Browse files
committed
Fixes #623. LockDiff does not compare hash/solvemeta any more.
LockDiff now uses only project dependencies to identify changes, thus ignoring any changes to the SolveMeta iformation. If required changes in the SolveMeta can still be seen using source diffs. Modified and added tests to check new behaviour.
1 parent 9f3d2ec commit fd4fa35

File tree

5 files changed

+78
-63
lines changed

5 files changed

+78
-63
lines changed

internal/gps/lockdiff.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package gps
66

77
import (
8-
"encoding/hex"
98
"fmt"
109
"sort"
1110
"strings"
@@ -44,10 +43,9 @@ func (diff *StringDiff) String() string {
4443
// LockDiff is the set of differences between an existing lock file and an updated lock file.
4544
// Fields are only populated when there is a difference, otherwise they are empty.
4645
type LockDiff struct {
47-
HashDiff *StringDiff
48-
Add []LockedProjectDiff
49-
Remove []LockedProjectDiff
50-
Modify []LockedProjectDiff
46+
Add []LockedProjectDiff
47+
Remove []LockedProjectDiff
48+
Modify []LockedProjectDiff
5149
}
5250

5351
// LockedProjectDiff contains the before and after snapshot of a project reference.
@@ -91,12 +89,6 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
9189

9290
diff := LockDiff{}
9391

94-
h1 := hex.EncodeToString(l1.InputHash())
95-
h2 := hex.EncodeToString(l2.InputHash())
96-
if h1 != h2 {
97-
diff.HashDiff = &StringDiff{Previous: h1, Current: h2}
98-
}
99-
10092
var i2next int
10193
for i1 := 0; i1 < len(p1); i1++ {
10294
lp1 := p1[i1]
@@ -140,7 +132,7 @@ func DiffLocks(l1 Lock, l2 Lock) *LockDiff {
140132
diff.Add = append(diff.Add, add)
141133
}
142134

143-
if diff.HashDiff == nil && len(diff.Add) == 0 && len(diff.Remove) == 0 && len(diff.Modify) == 0 {
135+
if len(diff.Add) == 0 && len(diff.Remove) == 0 && len(diff.Modify) == 0 {
144136
return nil // The locks are the equivalent
145137
}
146138
return &diff

internal/gps/lockdiff_test.go

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,35 @@ func TestDiffLocks_NoChange(t *testing.T) {
212212
}
213213
}
214214

215-
func TestDiffLocks_AddProjects(t *testing.T) {
215+
func TestDiffLocks_IgnoreHashChange(t *testing.T) {
216216
l1 := safeLock{
217217
h: []byte("abc123"),
218218
p: []LockedProject{
219219
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
220220
},
221221
}
222222
l2 := safeLock{
223+
h: []byte("def456"),
224+
p: []LockedProject{
225+
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
226+
},
227+
}
228+
229+
diff := DiffLocks(l1, l2)
230+
if diff != nil {
231+
t.Fatal("Expected the diff to be nil")
232+
}
233+
}
234+
235+
func TestDiffLocks_AddProjects(t *testing.T) {
236+
l1 := safeLock{
223237
h: []byte("abc123"),
238+
p: []LockedProject{
239+
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
240+
},
241+
}
242+
l2 := safeLock{
243+
h: []byte("def456"),
224244
p: []LockedProject{
225245
{
226246
pi: ProjectIdentifier{ProjectRoot: "github.com/baz/qux", Source: "https://github.com/mcfork/bazqux.git"},
@@ -310,7 +330,7 @@ func TestDiffLocks_RemoveProjects(t *testing.T) {
310330
},
311331
}
312332
l2 := safeLock{
313-
h: []byte("abc123"),
333+
h: []byte("def456"),
314334
p: []LockedProject{
315335
{pi: ProjectIdentifier{ProjectRoot: "github.com/baz/qux"}, v: NewVersion("v1.0.0")},
316336
},
@@ -389,7 +409,7 @@ func TestDiffLocks_ModifyProjects(t *testing.T) {
389409
},
390410
}
391411
l2 := safeLock{
392-
h: []byte("abc123"),
412+
h: []byte("def456"),
393413
p: []LockedProject{
394414
{pi: ProjectIdentifier{ProjectRoot: "github.com/baz/qux"}, v: NewVersion("v1.0.0")},
395415
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v2.0.0")},
@@ -420,35 +440,6 @@ func TestDiffLocks_ModifyProjects(t *testing.T) {
420440
}
421441
}
422442

423-
func TestDiffLocks_ModifyHash(t *testing.T) {
424-
h1, _ := hex.DecodeString("abc123")
425-
l1 := safeLock{
426-
h: h1,
427-
p: []LockedProject{
428-
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
429-
},
430-
}
431-
432-
h2, _ := hex.DecodeString("def456")
433-
l2 := safeLock{
434-
h: h2,
435-
p: []LockedProject{
436-
{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
437-
},
438-
}
439-
440-
diff := DiffLocks(l1, l2)
441-
if diff == nil {
442-
t.Fatal("Expected the diff to be populated")
443-
}
444-
445-
want := "abc123 -> def456"
446-
got := diff.HashDiff.String()
447-
if got != want {
448-
t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", want, got)
449-
}
450-
}
451-
452443
func TestDiffLocks_EmptyInitialLock(t *testing.T) {
453444
h2, _ := hex.DecodeString("abc123")
454445
l2 := safeLock{
@@ -460,12 +451,6 @@ func TestDiffLocks_EmptyInitialLock(t *testing.T) {
460451

461452
diff := DiffLocks(nil, l2)
462453

463-
wantHash := "+ abc123"
464-
gotHash := diff.HashDiff.String()
465-
if gotHash != wantHash {
466-
t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", wantHash, gotHash)
467-
}
468-
469454
if len(diff.Add) != 1 {
470455
t.Fatalf("Expected diff.Add to contain 1 project, got %d", len(diff.Add))
471456
}
@@ -482,12 +467,6 @@ func TestDiffLocks_EmptyFinalLock(t *testing.T) {
482467

483468
diff := DiffLocks(l1, nil)
484469

485-
wantHash := "- abc123"
486-
gotHash := diff.HashDiff.String()
487-
if gotHash != wantHash {
488-
t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", wantHash, gotHash)
489-
}
490-
491470
if len(diff.Remove) != 1 {
492471
t.Fatalf("Expected diff.Remove to contain 1 project, got %d", len(diff.Remove))
493472
}

testdata/txn_writer/expected_diff_output.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Memo: 595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c -> 2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e
2-
31
Add:
42
[[projects]]
53
name = "github.com/sdboyer/deptest"

txn_writer.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package dep
66

77
import (
88
"bytes"
9-
"fmt"
109
"io/ioutil"
1110
"log"
1211
"os"
@@ -176,10 +175,6 @@ func toRawLockedProjectDiffs(diffs []gps.LockedProjectDiff) rawLockedProjectDiff
176175
func formatLockDiff(diff gps.LockDiff) (string, error) {
177176
var buf bytes.Buffer
178177

179-
if diff.HashDiff != nil {
180-
buf.WriteString(fmt.Sprintf("Memo: %s\n\n", diff.HashDiff))
181-
}
182-
183178
writeDiffs := func(diffs []gps.LockedProjectDiff) error {
184179
raw := toRawLockedProjectDiffs(diffs)
185180
chunk, err := toml.Marshal(raw)

txn_writer_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/golang/dep/internal/gps"
1415
"github.com/golang/dep/internal/test"
1516
"github.com/pkg/errors"
1617
)
@@ -247,6 +248,50 @@ func TestSafeWriter_ManifestAndUnmodifiedLockWithForceVendor(t *testing.T) {
247248
}
248249
}
249250

251+
func TestSafeWriter_ModifiedLockChangedHash(t *testing.T) {
252+
test.NeedsExternalNetwork(t)
253+
test.NeedsGit(t)
254+
255+
h := test.NewHelper(t)
256+
defer h.Cleanup()
257+
258+
pc := NewTestProjectContext(h, safeWriterProject)
259+
defer pc.Release()
260+
pc.CopyFile(LockName, safeWriterGoldenLock)
261+
pc.Load()
262+
263+
originalLock := new(Lock)
264+
*originalLock = *pc.Project.Lock
265+
originalLock.SolveMeta.InputsDigest = []byte{} // zero out the input hash to ensure non-equivalency
266+
sw, _ := NewSafeWriter(nil, originalLock, pc.Project.Lock, VendorOnChanged)
267+
268+
// Verify prepared actions
269+
if sw.HasManifest() {
270+
t.Fatal("Did not expect the manifest to be written")
271+
}
272+
if !sw.HasLock() {
273+
t.Fatal("Expected that the writer should plan to write the lock")
274+
}
275+
if sw.writeVendor {
276+
t.Fatal("Did not expect vendor to be written to")
277+
}
278+
279+
// Write changes
280+
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true)
281+
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
282+
283+
// Verify file system changes
284+
if err := pc.ManifestShouldNotExist(); err != nil {
285+
t.Fatal(err)
286+
}
287+
if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
288+
t.Fatal(err)
289+
}
290+
if err := pc.VendorShouldExist(); err == nil {
291+
t.Fatal(err)
292+
}
293+
}
294+
250295
func TestSafeWriter_ModifiedLock(t *testing.T) {
251296
test.NeedsExternalNetwork(t)
252297
test.NeedsGit(t)
@@ -262,6 +307,12 @@ func TestSafeWriter_ModifiedLock(t *testing.T) {
262307
originalLock := new(Lock)
263308
*originalLock = *pc.Project.Lock
264309
originalLock.SolveMeta.InputsDigest = []byte{} // zero out the input hash to ensure non-equivalency
310+
originalLock.P = append(originalLock.P, gps.NewLockedProject(
311+
gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/golang/dep")},
312+
gps.NewBranch("master").Pair(gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb")),
313+
[]string{"."},
314+
))
315+
265316
sw, _ := NewSafeWriter(nil, originalLock, pc.Project.Lock, VendorOnChanged)
266317

267318
// Verify prepared actions

0 commit comments

Comments
 (0)