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

add ablity to test error scenarios in test harness #317

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,27 @@ 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
}

newLock := dep.LockFromInterface(solution)
sw.Prepare(manifest, p.Lock, newLock, 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
8 changes: 7 additions & 1 deletion cmd/dep/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ func TestIntegration(t *testing.T) {

// Run commands
testProj.RecordImportPaths()

var err error
for _, args := range testCase.Commands {
testProj.DoRun(args)
if err = testProj.DoRun(args); err != nil {
break
}
}

testCase.CompareError(err)

// Check final manifest and lock
testCase.CompareFile("manifest.json", testProj.ProjPath("manifest.json"))
testCase.CompareFile("lock.json", testProj.ProjPath("lock.json"))
Expand Down
13 changes: 4 additions & 9 deletions cmd/dep/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,10 @@ 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
newLock := dep.LockFromInterface(soln)
sw.Prepare(p.Manifest, p.Lock, newLock, 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
12 changes: 7 additions & 5 deletions cmd/dep/testdata/harness_tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ The `testcase.json` file has the following format:
"github.com/sdboyer/deptestdos",
"github.com/sdboyer/deptesttres",
"github.com/sdboyer/deptestquatro"
]
],
"error-expected":"something went wrong"
}

All of the categories are optional - if the `imports` list for a test is empty,
Expand All @@ -71,9 +72,10 @@ The test procedure is as follows:
4. Fetch the repos and versions in `gopath-initial` into `$TMPDIR/src` directory
5. Fetch the repos and versions in `vendor-initial` to the project's `vendor` directory
6. Run `commands` on the project, in declaration order
7. Check the resulting files against those in the `final` input directory
8. Check the `vendor` directory for the projects listed under `vendor-final`
9. Check that there were no changes to `src` listings
10. Clean up
7. Check that only expected errors were raised
8. Check the resulting files against those in the `final` input directory
9. Check the `vendor` directory for the projects listed under `vendor-final`
10. Check that there were no changes to `src` listings
11. Clean up

Note that for the remote fetches, only git repos are currently supported.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"commands": [
["init"],
["ensure", "-update", "github.com/sdboyer/deptest"]
],
"vendor-final": [
Expand Down
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": [
"."
]
}
]
}
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"]
]
}
27 changes: 14 additions & 13 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ package dep

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"syscall"

"github.com/pkg/errors"
)

func IsRegular(name string) (bool, error) {
Expand All @@ -25,7 +26,7 @@ func IsRegular(name string) (bool, error) {
return false, err
}
if fi.IsDir() {
return false, fmt.Errorf("%q is a directory, should be a file", name)
return false, errors.Errorf("%q is a directory, should be a file", name)
}
return true, nil
}
Expand All @@ -40,7 +41,7 @@ func IsDir(name string) (bool, error) {
return false, err
}
if !fi.IsDir() {
return false, fmt.Errorf("%q is not a directory", name)
return false, errors.Errorf("%q is not a directory", name)
}
return true, nil
}
Expand Down Expand Up @@ -80,15 +81,15 @@ func writeFile(path string, in json.Marshaler) error {
func renameWithFallback(src, dest string) error {
fi, err := os.Lstat(src)
if err != nil {
return err
return errors.Wrapf(err, "cannot stat %s", src)
}

// Windows cannot use syscall.Rename to rename a directory
if runtime.GOOS == "windows" && fi.IsDir() {
if err := CopyDir(src, dest); err != nil {
return err
}
return os.RemoveAll(src)
return errors.Wrapf(os.RemoveAll(src), "cannot delete %s", src)
}

err = os.Rename(src, dest)
Expand All @@ -98,7 +99,7 @@ func renameWithFallback(src, dest string) error {

terr, ok := err.(*os.LinkError)
if !ok {
return err
return errors.Wrapf(err, "cannot rename %s to %s", src, dest)
}

// Rename may fail if src and dest are on different devices; fall back to
Expand All @@ -123,38 +124,38 @@ func renameWithFallback(src, dest string) error {
cerr = CopyFile(src, dest)
}
} else {
return terr
return errors.Wrapf(terr, "link error: cannot rename %s to %s", src, dest)
}

if cerr != nil {
return cerr
return errors.Wrapf(cerr, "second attemp failed: cannot rename %s to %s", src, dest)
}

return os.RemoveAll(src)
return errors.Wrapf(os.RemoveAll(src), "cannot delete %s", src)
}

// CopyDir takes in a directory and copies its contents to the destination.
// It preserves the file mode on files as well.
func CopyDir(src string, dest string) error {
fi, err := os.Lstat(src)
if err != nil {
return err
return errors.Wrapf(err, "cannot stat %s", src)
}

err = os.MkdirAll(dest, fi.Mode())
if err != nil {
return err
return errors.Wrapf(err, "cannot mkdir %s", dest)
}

dir, err := os.Open(src)
if err != nil {
return err
return errors.Wrapf(err, "cannot open %s", src)
}
defer dir.Close()

objects, err := dir.Readdir(-1)
if err != nil {
return err
return errors.Wrapf(err, "cannot read directory %s", dir.Name())
}

for _, obj := range objects {
Expand Down
Loading