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

Commit 42f2cb1

Browse files
committed
Simplify ensure payload logic
* Always write the manifest when provided. * Write the lock only when modified or when writing vendor/. * Clarify what should be passed for the two locks args: old lock and new lock. Updated `dep init` to match this expectation. * Add test case for writing a new lock
1 parent f5fbfe4 commit 42f2cb1

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

cmd/dep/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
163163
vlogf("Writing manifest and lock files.")
164164

165165
var sw dep.SafeWriter
166-
sw.Prepare(m, l, nil, true)
166+
sw.Prepare(m, nil, l, true)
167167
if err := sw.Write(root, sm); err != nil {
168168
return errors.Wrap(err, "safe write of manifest and lock")
169169
}

txn_writer.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,29 +157,23 @@ func (diff StringDiff) MarshalJSON() ([]byte, error) {
157157
// and the vendor directory in the same way
158158
// - If the forceVendor param is true, then vendor/ will be unconditionally
159159
// written out based on newLock if present, else lock, else error.
160-
func (sw *SafeWriter) Prepare(manifest *Manifest, lock *Lock, newLock *Lock, forceVendor bool) {
160+
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock *Lock, newLock *Lock, forceVendor bool) {
161161
sw.Payload = &SafeWriterPayload{
162-
Manifest: manifest,
163-
WriteVendor: forceVendor,
162+
Manifest: manifest,
164163
}
165164

166-
if newLock != nil {
167-
if lock == nil {
168-
sw.Payload.Lock = newLock
169-
sw.Payload.WriteVendor = true
170-
} else {
171-
diff := diffLocks(lock, newLock)
172-
if diff != nil {
173-
sw.Payload.Lock = newLock
174-
sw.Payload.LockDiff = diff
175-
sw.Payload.WriteVendor = true
176-
} else if forceVendor {
177-
sw.Payload.Lock = newLock
178-
sw.Payload.WriteVendor = true
179-
}
180-
}
181-
} else if lock != nil {
182-
sw.Payload.Lock = lock
165+
if oldLock != nil && newLock != nil {
166+
sw.Payload.LockDiff = diffLocks(oldLock, newLock)
167+
}
168+
169+
if forceVendor || sw.Payload.LockDiff != nil {
170+
sw.Payload.Lock = newLock
171+
sw.Payload.WriteVendor = true
172+
}
173+
174+
if oldLock == nil && newLock != nil {
175+
sw.Payload.Lock = newLock
176+
sw.Payload.WriteVendor = true
183177
}
184178
}
185179

txn_writer_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,54 @@ func TestSafeWriter_ForceVendorWhenVendorAlreadyExists(t *testing.T) {
373373
}
374374
}
375375

376+
func TestSafeWriter_NewLock(t *testing.T) {
377+
test.NeedsExternalNetwork(t)
378+
test.NeedsGit(t)
379+
380+
h := test.NewHelper(t)
381+
defer h.Cleanup()
382+
383+
pc := NewTestProjectContext(h, safeWriterProject)
384+
defer pc.Release()
385+
pc.Load()
386+
387+
var sw SafeWriter
388+
lf := h.GetTestFile(safeWriterGoldenLock)
389+
defer lf.Close()
390+
newLock, err := readLock(lf)
391+
h.Must(err)
392+
sw.Prepare(nil, nil, newLock, false)
393+
394+
// Verify prepared actions
395+
if sw.Payload.HasManifest() {
396+
t.Fatal("Did not expect the payload to contain the manifest")
397+
}
398+
if !sw.Payload.HasLock() {
399+
t.Fatal("Expected the payload to contain the lock")
400+
}
401+
if !sw.Payload.HasVendor() {
402+
t.Fatal("Expected the payload to the vendor directory")
403+
}
404+
405+
// Write changes
406+
err = sw.Write(pc.Project.AbsRoot, pc.SourceManager)
407+
h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
408+
409+
// Verify file system changes
410+
if err := pc.ManifestShouldNotExist(); err != nil {
411+
t.Fatal(err)
412+
}
413+
if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
414+
t.Fatal(err)
415+
}
416+
if err := pc.VendorShouldExist(); err != nil {
417+
t.Fatal(err)
418+
}
419+
if err := pc.VendorFileShouldExist("github.com/sdboyer/dep-test"); err != nil {
420+
t.Fatal(err)
421+
}
422+
}
423+
376424
func TestSafeWriter_DiffLocks(t *testing.T) {
377425
test.NeedsExternalNetwork(t)
378426
test.NeedsGit(t)
@@ -435,4 +483,4 @@ func TestSafeWriter_DiffLocks(t *testing.T) {
435483
if err = pc.ShouldMatchGolden(goldenOutput, output); err != nil {
436484
t.Fatal(err)
437485
}
438-
}
486+
}

0 commit comments

Comments
 (0)