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

Commit e43a3b8

Browse files
committed
Respect dry-run flag during dep ensure
1 parent 1c50c3a commit e43a3b8

File tree

13 files changed

+578
-282
lines changed

13 files changed

+578
-282
lines changed

cmd/dep/ensure.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,26 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
147147
return errors.Wrap(err, "ensure Solve()")
148148
}
149149

150-
sw := dep.SafeWriter{
151-
Root: p.AbsRoot,
152-
Lock: p.Lock,
153-
NewLock: solution,
154-
SourceManager: sm,
155-
}
156-
if !cmd.update {
157-
sw.Manifest = p.Manifest
158-
}
159-
160150
// check if vendor exists, because if the locks are the same but
161151
// vendor does not exist we should write vendor
162-
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(sw.Root, "vendor"))
152+
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(p.AbsRoot, "vendor"))
163153
if err != nil {
164154
return errors.Wrap(err, "ensure vendor is a directory")
165155
}
166156
writeV := !vendorExists && solution != nil
167157

168-
return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
158+
var sw dep.SafeWriter
159+
var manifest *dep.Manifest
160+
if !cmd.update {
161+
manifest = p.Manifest
162+
}
163+
164+
sw.Prepare(manifest, p.Lock, solution, writeV)
165+
if cmd.dryRun {
166+
return sw.PrintPreparedActions()
167+
}
168+
169+
return errors.Wrap(sw.Write(p.AbsRoot, sm), "grouped write of manifest, lock and vendor")
169170
}
170171

171172
func applyUpdateArgs(args []string, params *gps.SolveParameters) {

cmd/dep/init.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
106106
if err != nil {
107107
return err
108108
}
109-
m := dep.Manifest{
109+
m := &dep.Manifest{
110110
Dependencies: pd.constraints,
111111
}
112112

113113
// Make an initial lock from what knowledge we've collected about the
114114
// versions on disk
115-
l := dep.Lock{
115+
l := &dep.Lock{
116116
P: make([]gps.LockedProject, 0, len(pd.ondisk)),
117117
}
118118

@@ -134,19 +134,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
134134
)
135135
}
136136

137-
sw := dep.SafeWriter{
138-
Root: root,
139-
SourceManager: sm,
140-
Manifest: &m,
141-
}
142-
143137
if len(pd.notondisk) > 0 {
144138
vlogf("Solving...")
145139
params := gps.SolveParameters{
146140
RootDir: root,
147141
RootPackageTree: pkgT,
148-
Manifest: &m,
149-
Lock: &l,
142+
Manifest: m,
143+
Lock: l,
150144
}
151145

152146
if *verbose {
@@ -163,14 +157,14 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
163157
handleAllTheFailuresOfTheWorld(err)
164158
return err
165159
}
166-
sw.Lock = dep.LockFromInterface(soln)
167-
} else {
168-
sw.Lock = &l
160+
l = dep.LockFromInterface(soln)
169161
}
170162

171163
vlogf("Writing manifest and lock files.")
172164

173-
if err := sw.WriteAllSafe(true); err != nil {
165+
var sw dep.SafeWriter
166+
sw.Prepare(m, l, nil, true)
167+
if err := sw.Write(root, sm); err != nil {
174168
return errors.Wrap(err, "safe write of manifest and lock")
175169
}
176170

cmd/dep/remove.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,9 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
179179
return err
180180
}
181181

182-
sw := dep.SafeWriter{
183-
Root: p.AbsRoot,
184-
Manifest: p.Manifest,
185-
Lock: p.Lock,
186-
NewLock: soln,
187-
SourceManager: sm,
188-
}
189-
190-
if err := sw.WriteAllSafe(false); err != nil {
182+
var sw dep.SafeWriter
183+
sw.Prepare(p.Manifest, p.Lock, soln, false)
184+
if err := sw.Write(p.AbsRoot, sm); err != nil {
191185
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
192186
}
193187
return nil
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"memo": "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5",
3+
"projects": [
4+
{
5+
"name": "github.com/sdboyer/deptest",
6+
"version": "v0.8.0",
7+
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
8+
"packages": [
9+
"."
10+
]
11+
},
12+
{
13+
"name": "github.com/sdboyer/deptestdos",
14+
"version": "v2.0.0",
15+
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
16+
"packages": [
17+
"."
18+
]
19+
}
20+
]
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"github.com/sdboyer/deptest": {
4+
"version": "~0.8.0"
5+
}
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"memo": "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5",
3+
"projects": [
4+
{
5+
"name": "github.com/sdboyer/deptest",
6+
"version": "v0.8.0",
7+
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
8+
"packages": [
9+
"."
10+
]
11+
},
12+
{
13+
"name": "github.com/sdboyer/deptestdos",
14+
"version": "v2.0.0",
15+
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
16+
"packages": [
17+
"."
18+
]
19+
}
20+
]
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/sdboyer/deptest"
9+
"github.com/sdboyer/deptestdos"
10+
)
11+
12+
func main() {
13+
err := nil
14+
if err != nil {
15+
deptest.Map["yo yo!"]
16+
}
17+
deptestdos.diMeLo("whatev")
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"github.com/sdboyer/deptest": {
4+
"version": "~0.8.0"
5+
}
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commands": [
3+
["init"],
4+
["ensure", "-n", "-update", "github.com/sdboyer/deptest"]
5+
]
6+
}

lock.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,7 @@ func (l *Lock) MarshalJSON() ([]byte, error) {
102102
}
103103

104104
v := lp.Version()
105-
// Figure out how to get the underlying revision
106-
switch tv := v.(type) {
107-
case gps.UnpairedVersion:
108-
// TODO we could error here, if we want to be very defensive about not
109-
// allowing a lock to be written if without an immmutable revision
110-
case gps.Revision:
111-
ld.Revision = tv.String()
112-
case gps.PairedVersion:
113-
ld.Revision = tv.Underlying().String()
114-
}
105+
ld.Revision = GetRevisionFromVersion(v)
115106

116107
switch v.Type() {
117108
case gps.IsBranch:
@@ -134,6 +125,20 @@ func (l *Lock) MarshalJSON() ([]byte, error) {
134125
return buf.Bytes(), err
135126
}
136127

128+
func GetRevisionFromVersion(v gps.Version) string {
129+
// Figure out how to get the underlying revision
130+
switch tv := v.(type) {
131+
case gps.UnpairedVersion:
132+
// TODO we could error here, if we want to be very defensive about not
133+
// allowing a lock to be written if without an immmutable revision
134+
case gps.Revision:
135+
return tv.String()
136+
case gps.PairedVersion:
137+
return tv.Underlying().String()
138+
}
139+
return ""
140+
}
141+
137142
// LockFromInterface converts an arbitrary gps.Lock to dep's representation of a
138143
// lock. If the input is already dep's *lock, the input is returned directly.
139144
//

test/test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,18 @@ func (h *Helper) Path(name string) string {
513513

514514
// MustExist fails if path does not exist.
515515
func (h *Helper) MustExist(path string) {
516+
if err := h.ShouldExist(path); err != nil {
517+
h.t.Fatalf("%+v", err)
518+
}
519+
}
520+
521+
// ShouldExist returns an error if path does not exist.
522+
func (h *Helper) ShouldExist(path string) error {
516523
if !h.Exist(path) {
517-
h.t.Fatalf("%+v", errors.Errorf("%s does not exist but should", path))
524+
return errors.Errorf("%s does not exist but should", path)
518525
}
526+
527+
return nil
519528
}
520529

521530
// Exist returns whether or not a path exists
@@ -532,9 +541,18 @@ func (h *Helper) Exist(path string) bool {
532541

533542
// MustNotExist fails if path exists.
534543
func (h *Helper) MustNotExist(path string) {
544+
if err := h.ShouldNotExist(path); err != nil {
545+
h.t.Fatalf("%+v", err)
546+
}
547+
}
548+
549+
// ShouldNotExist returns an error if path exists.
550+
func (h *Helper) ShouldNotExist(path string) error {
535551
if h.Exist(path) {
536-
h.t.Fatalf("%+v", errors.Errorf("%s exists but should not", path))
552+
return errors.Errorf("%s exists but should not", path)
537553
}
554+
555+
return nil
538556
}
539557

540558
// Cleanup cleans up a test that runs testgo.

0 commit comments

Comments
 (0)