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

Commit 354108c

Browse files
committedJul 21, 2018
dep: Make SafeWriter use status map for OnChanged
If vendor was empty, nonexistent, or just missing a few targeted items, the SafeWriter would miss them.
1 parent 2c0659e commit 354108c

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed
 

‎cmd/dep/ensure.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Proj
316316

317317
// Pass the same lock as old and new so that the writer will observe no
318318
// difference, and write out only ncessary vendor/ changes.
319-
dw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions)
319+
dw, err := dep.NewSafeWriter(nil, p.Lock, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions, nil)
320320
//dw, err := dep.NewDeltaWriter(p.Lock, p.Lock, p.Manifest.PruneOptions, filepath.Join(p.AbsRoot, "vendor"), dep.VendorAlways)
321321
if err != nil {
322322
return err

‎cmd/dep/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
170170
ctx.Err.Printf("Old vendor backed up to %v", vendorbak)
171171
}
172172

173-
sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions)
173+
sw, err := dep.NewSafeWriter(p.Manifest, nil, p.Lock, dep.VendorAlways, p.Manifest.PruneOptions, nil)
174174
if err != nil {
175175
return errors.Wrap(err, "init failed: unable to create a SafeWriter")
176176
}

‎txn_writer.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type SafeWriter struct {
9090
// - If oldLock is provided without newLock, error.
9191
//
9292
// - If vendor is VendorAlways without a newLock, error.
93-
func NewSafeWriter(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior, prune gps.CascadingPruneOptions) (*SafeWriter, error) {
93+
func NewSafeWriter(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior, prune gps.CascadingPruneOptions, status map[string]verify.VendorStatus) (*SafeWriter, error) {
9494
sw := &SafeWriter{
9595
Manifest: manifest,
9696
lock: newLock,
@@ -114,7 +114,18 @@ func NewSafeWriter(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBeha
114114
case VendorAlways:
115115
sw.writeVendor = true
116116
case VendorOnChanged:
117-
sw.writeVendor = sw.lockDiff.Changed(anyExceptHash & ^verify.InputImportsChanged) || (newLock != nil && oldLock == nil)
117+
if newLock != nil && oldLock == nil {
118+
sw.writeVendor = true
119+
} else if sw.lockDiff.Changed(anyExceptHash & ^verify.InputImportsChanged) {
120+
sw.writeVendor = true
121+
} else {
122+
for _, stat := range status {
123+
if stat != verify.NoMismatch {
124+
sw.writeVendor = true
125+
break
126+
}
127+
}
128+
}
118129
}
119130

120131
if sw.writeVendor && newLock == nil {
@@ -442,7 +453,7 @@ func NewDeltaWriter(oldLock, newLock *Lock, status map[string]verify.VendorStatu
442453
if err != nil && os.IsNotExist(err) {
443454
// Provided dir does not exist, so there's no disk contents to compare
444455
// against. Fall back to the old SafeWriter.
445-
return NewSafeWriter(nil, oldLock, newLock, behavior, prune)
456+
return NewSafeWriter(nil, oldLock, newLock, behavior, prune, status)
446457
}
447458

448459
sw.lockDiff = verify.DiffLocks(oldLock, newLock)

‎txn_writer_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestSafeWriter_BadInput_MissingRoot(t *testing.T) {
3333
pc := NewTestProjectContext(h, safeWriterProject)
3434
defer pc.Release()
3535

36-
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions())
36+
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
3737
err := sw.Write("", pc.SourceManager, true, nil)
3838

3939
if err == nil {
@@ -51,7 +51,7 @@ func TestSafeWriter_BadInput_MissingSourceManager(t *testing.T) {
5151
pc.CopyFile(LockName, safeWriterGoldenLock)
5252
pc.Load()
5353

54-
sw, _ := NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions())
54+
sw, _ := NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
5555
err := sw.Write(pc.Project.AbsRoot, nil, true, nil)
5656

5757
if err == nil {
@@ -67,7 +67,7 @@ func TestSafeWriter_BadInput_ForceVendorMissingLock(t *testing.T) {
6767
pc := NewTestProjectContext(h, safeWriterProject)
6868
defer pc.Release()
6969

70-
_, err := NewSafeWriter(nil, nil, nil, VendorAlways, defaultCascadingPruneOptions())
70+
_, err := NewSafeWriter(nil, nil, nil, VendorAlways, defaultCascadingPruneOptions(), nil)
7171
if err == nil {
7272
t.Fatal("should have errored without a lock when forceVendor is true, but did not")
7373
} else if !strings.Contains(err.Error(), "newLock") {
@@ -83,7 +83,7 @@ func TestSafeWriter_BadInput_OldLockOnly(t *testing.T) {
8383
pc.CopyFile(LockName, safeWriterGoldenLock)
8484
pc.Load()
8585

86-
_, err := NewSafeWriter(nil, pc.Project.Lock, nil, VendorAlways, defaultCascadingPruneOptions())
86+
_, err := NewSafeWriter(nil, pc.Project.Lock, nil, VendorAlways, defaultCascadingPruneOptions(), nil)
8787
if err == nil {
8888
t.Fatal("should have errored with only an old lock, but did not")
8989
} else if !strings.Contains(err.Error(), "oldLock") {
@@ -97,7 +97,7 @@ func TestSafeWriter_BadInput_NonexistentRoot(t *testing.T) {
9797
pc := NewTestProjectContext(h, safeWriterProject)
9898
defer pc.Release()
9999

100-
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions())
100+
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
101101

102102
missingroot := filepath.Join(pc.Project.AbsRoot, "nonexistent")
103103
err := sw.Write(missingroot, pc.SourceManager, true, nil)
@@ -115,7 +115,7 @@ func TestSafeWriter_BadInput_RootIsFile(t *testing.T) {
115115
pc := NewTestProjectContext(h, safeWriterProject)
116116
defer pc.Release()
117117

118-
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions())
118+
sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
119119

120120
fileroot := pc.CopyFile("fileroot", "txn_writer/badinput_fileroot")
121121
err := sw.Write(fileroot, pc.SourceManager, true, nil)
@@ -139,7 +139,7 @@ func TestSafeWriter_Manifest(t *testing.T) {
139139
pc.CopyFile(ManifestName, safeWriterGoldenManifest)
140140
pc.Load()
141141

142-
sw, _ := NewSafeWriter(pc.Project.Manifest, nil, nil, VendorOnChanged, defaultCascadingPruneOptions())
142+
sw, _ := NewSafeWriter(pc.Project.Manifest, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
143143

144144
// Verify prepared actions
145145
if !sw.HasManifest() {
@@ -181,7 +181,7 @@ func TestSafeWriter_ManifestAndUnmodifiedLock(t *testing.T) {
181181
pc.CopyFile(LockName, safeWriterGoldenLock)
182182
pc.Load()
183183

184-
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorOnChanged, defaultCascadingPruneOptions())
184+
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorOnChanged, defaultCascadingPruneOptions(), nil)
185185

186186
// Verify prepared actions
187187
if !sw.HasManifest() {
@@ -226,7 +226,7 @@ func TestSafeWriter_ManifestAndUnmodifiedLockWithForceVendor(t *testing.T) {
226226
pc.CopyFile(LockName, safeWriterGoldenLock)
227227
pc.Load()
228228

229-
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions())
229+
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
230230

231231
// Verify prepared actions
232232
if !sw.HasManifest() {
@@ -273,12 +273,12 @@ func TestSafeWriter_ForceVendorWhenVendorAlreadyExists(t *testing.T) {
273273
pc.CopyFile(LockName, safeWriterGoldenLock)
274274
pc.Load()
275275

276-
sw, _ := NewSafeWriter(nil, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions())
276+
sw, _ := NewSafeWriter(nil, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
277277
err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
278278
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
279279

280280
// Verify prepared actions
281-
sw, _ = NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions())
281+
sw, _ = NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
282282
if sw.HasManifest() {
283283
t.Fatal("Did not expect the payload to contain the manifest")
284284
}
@@ -325,7 +325,7 @@ func TestSafeWriter_NewLock(t *testing.T) {
325325
defer lf.Close()
326326
newLock, err := readLock(lf)
327327
h.Must(err)
328-
sw, _ := NewSafeWriter(nil, nil, newLock, VendorOnChanged, defaultCascadingPruneOptions())
328+
sw, _ := NewSafeWriter(nil, nil, newLock, VendorOnChanged, defaultCascadingPruneOptions(), nil)
329329

330330
// Verify prepared actions
331331
if sw.HasManifest() {
@@ -372,7 +372,7 @@ func TestSafeWriter_NewLockSkipVendor(t *testing.T) {
372372
defer lf.Close()
373373
newLock, err := readLock(lf)
374374
h.Must(err)
375-
sw, _ := NewSafeWriter(nil, nil, newLock, VendorNever, defaultCascadingPruneOptions())
375+
sw, _ := NewSafeWriter(nil, nil, newLock, VendorNever, defaultCascadingPruneOptions(), nil)
376376

377377
// Verify prepared actions
378378
if sw.HasManifest() {
@@ -433,7 +433,7 @@ func TestSafeWriter_VendorDotGitPreservedWithForceVendor(t *testing.T) {
433433
pc.CopyFile(LockName, safeWriterGoldenLock)
434434
pc.Load()
435435

436-
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions())
436+
sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
437437

438438
// Verify prepared actions
439439
if !sw.HasManifest() {

0 commit comments

Comments
 (0)
This repository has been archived.