Skip to content

Commit

Permalink
Respect dry-run flag during dep ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs committed Mar 9, 2017
1 parent 1c50c3a commit e43a3b8
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 282 deletions.
25 changes: 13 additions & 12 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,26 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Wrap(err, "ensure Solve()")
}

sw := dep.SafeWriter{
Root: p.AbsRoot,
Lock: p.Lock,
NewLock: solution,
SourceManager: sm,
}
if !cmd.update {
sw.Manifest = p.Manifest
}

// check if vendor exists, because if the locks are the same but
// vendor does not exist we should write vendor
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(sw.Root, "vendor"))
vendorExists, err := dep.IsNonEmptyDir(filepath.Join(p.AbsRoot, "vendor"))
if err != nil {
return errors.Wrap(err, "ensure vendor is a directory")
}
writeV := !vendorExists && solution != nil

return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
var sw dep.SafeWriter
var manifest *dep.Manifest
if !cmd.update {
manifest = p.Manifest
}

sw.Prepare(manifest, p.Lock, solution, writeV)
if cmd.dryRun {
return sw.PrintPreparedActions()
}

return errors.Wrap(sw.Write(p.AbsRoot, sm), "grouped write of manifest, lock and vendor")
}

func applyUpdateArgs(args []string, params *gps.SolveParameters) {
Expand Down
22 changes: 8 additions & 14 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return err
}
m := dep.Manifest{
m := &dep.Manifest{
Dependencies: pd.constraints,
}

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

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

sw := dep.SafeWriter{
Root: root,
SourceManager: sm,
Manifest: &m,
}

if len(pd.notondisk) > 0 {
vlogf("Solving...")
params := gps.SolveParameters{
RootDir: root,
RootPackageTree: pkgT,
Manifest: &m,
Lock: &l,
Manifest: m,
Lock: l,
}

if *verbose {
Expand All @@ -163,14 +157,14 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
handleAllTheFailuresOfTheWorld(err)
return err
}
sw.Lock = dep.LockFromInterface(soln)
} else {
sw.Lock = &l
l = dep.LockFromInterface(soln)
}

vlogf("Writing manifest and lock files.")

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

Expand Down
12 changes: 3 additions & 9 deletions cmd/dep/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,9 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
return err
}

sw := dep.SafeWriter{
Root: p.AbsRoot,
Manifest: p.Manifest,
Lock: p.Lock,
NewLock: soln,
SourceManager: sm,
}

if err := sw.WriteAllSafe(false); err != nil {
var sw dep.SafeWriter
sw.Prepare(p.Manifest, p.Lock, soln, false)
if err := sw.Write(p.AbsRoot, sm); err != nil {
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
}
return nil
Expand Down
21 changes: 21 additions & 0 deletions cmd/dep/testdata/harness_tests/ensure/update/case2/final/lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"memo": "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5",
"projects": [
{
"name": "github.com/sdboyer/deptest",
"version": "v0.8.0",
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
"packages": [
"."
]
},
{
"name": "github.com/sdboyer/deptestdos",
"version": "v2.0.0",
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
"packages": [
"."
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"github.com/sdboyer/deptest": {
"version": "~0.8.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"memo": "9a5243dd3fa20feeaa20398e7283d6c566532e2af1aae279a010df34793761c5",
"projects": [
{
"name": "github.com/sdboyer/deptest",
"version": "v0.8.0",
"revision": "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
"packages": [
"."
]
},
{
"name": "github.com/sdboyer/deptestdos",
"version": "v2.0.0",
"revision": "5c607206be5decd28e6263ffffdcee067266015e",
"packages": [
"."
]
}
]
}
18 changes: 18 additions & 0 deletions cmd/dep/testdata/harness_tests/ensure/update/case2/initial/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"github.com/sdboyer/deptest"
"github.com/sdboyer/deptestdos"
)

func main() {
err := nil
if err != nil {
deptest.Map["yo yo!"]
}
deptestdos.diMeLo("whatev")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"github.com/sdboyer/deptest": {
"version": "~0.8.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"commands": [
["init"],
["ensure", "-n", "-update", "github.com/sdboyer/deptest"]
]
}
25 changes: 15 additions & 10 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,7 @@ func (l *Lock) MarshalJSON() ([]byte, error) {
}

v := lp.Version()
// Figure out how to get the underlying revision
switch tv := v.(type) {
case gps.UnpairedVersion:
// TODO we could error here, if we want to be very defensive about not
// allowing a lock to be written if without an immmutable revision
case gps.Revision:
ld.Revision = tv.String()
case gps.PairedVersion:
ld.Revision = tv.Underlying().String()
}
ld.Revision = GetRevisionFromVersion(v)

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

func GetRevisionFromVersion(v gps.Version) string {
// Figure out how to get the underlying revision
switch tv := v.(type) {
case gps.UnpairedVersion:
// TODO we could error here, if we want to be very defensive about not
// allowing a lock to be written if without an immmutable revision
case gps.Revision:
return tv.String()
case gps.PairedVersion:
return tv.Underlying().String()
}
return ""
}

// LockFromInterface converts an arbitrary gps.Lock to dep's representation of a
// lock. If the input is already dep's *lock, the input is returned directly.
//
Expand Down
22 changes: 20 additions & 2 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,18 @@ func (h *Helper) Path(name string) string {

// MustExist fails if path does not exist.
func (h *Helper) MustExist(path string) {
if err := h.ShouldExist(path); err != nil {
h.t.Fatalf("%+v", err)
}
}

// ShouldExist returns an error if path does not exist.
func (h *Helper) ShouldExist(path string) error {
if !h.Exist(path) {
h.t.Fatalf("%+v", errors.Errorf("%s does not exist but should", path))
return errors.Errorf("%s does not exist but should", path)
}

return nil
}

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

// MustNotExist fails if path exists.
func (h *Helper) MustNotExist(path string) {
if err := h.ShouldNotExist(path); err != nil {
h.t.Fatalf("%+v", err)
}
}

// ShouldNotExist returns an error if path exists.
func (h *Helper) ShouldNotExist(path string) error {
if h.Exist(path) {
h.t.Fatalf("%+v", errors.Errorf("%s exists but should not", path))
return errors.Errorf("%s exists but should not", path)
}

return nil
}

// Cleanup cleans up a test that runs testgo.
Expand Down
Loading

0 comments on commit e43a3b8

Please sign in to comment.