Moving to want/got pattern and adding unit test coverage

This commit is contained in:
tro3 2017-02-10 10:08:47 -08:00 коммит произвёл sam boyer
Родитель fceb52f636
Коммит 0c16a76633
7 изменённых файлов: 134 добавлений и 154 удалений

2
.gitignore поставляемый
Просмотреть файл

@ -5,5 +5,3 @@
/dep
/testdep
/dep.exe
debug
debug.*

Просмотреть файл

@ -16,27 +16,16 @@ import (
func TestAnalyzerInfo(t *testing.T) {
a := analyzer{}
n, v := a.Info()
if n != "dep" {
t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", n)
gotn, gotv := a.Info()
if gotn != "dep" {
t.Errorf("analyzer.Info() returned an incorrect name: '%s' (expected 'dep')", gotn)
}
expV, err := semver.NewVersion("v0.0.1")
wantv, err := semver.NewVersion("v0.0.1")
if err != nil {
t.Fatal(err)
} else if v != expV {
t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", v, expV)
}
}
func TestAnalyzerErrors(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("dep")
a := analyzer{}
_, _, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
if err == nil {
t.Fatal("analyzer.DeriveManifestAndLock with manifest not present should have produced an error")
if gotv != wantv {
t.Fatalf("analyzer.Info() returned an incorrect version: %v (expected %v)", gotv, wantv)
}
}
@ -46,7 +35,7 @@ func TestDeriveManifestAndLock(t *testing.T) {
h.TempDir("dep")
golden := "analyzer/manifest.json"
contents := h.GetTestFileString(golden)
want := h.GetTestFileString(golden)
h.TempCopy(filepath.Join("dep", ManifestName), golden)
a := analyzer{}
@ -56,18 +45,18 @@ func TestDeriveManifestAndLock(t *testing.T) {
t.Fatal(err)
}
b, err := m.(*Manifest).MarshalJSON()
got, err := m.(*Manifest).MarshalJSON()
if err != nil {
t.Fatal(err)
}
if contents != string(b) {
if want != string(got) {
if *test.UpdateGolden {
if err := h.WriteTestFile(golden, string(b)); err != nil {
if err := h.WriteTestFile(golden, string(got)); err != nil {
t.Fatal(err)
}
} else {
t.Fatalf("expected %s\n got %s", contents, string(b))
t.Fatalf("expected %s\n got %s", want, string(got))
}
}

Просмотреть файл

@ -43,21 +43,21 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
"my/silly/thing",
}
for _, ip := range importPaths {
fullpath := filepath.Join(depCtx.GOPATH, "src", ip)
pr, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
for _, want := range importPaths {
fullpath := filepath.Join(depCtx.GOPATH, "src", want)
got, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
if err != nil {
t.Fatal(err)
}
if pr != ip {
t.Fatalf("expected %s, got %s", ip, pr)
if got != want {
t.Fatalf("expected %s, got %s", want, got)
}
}
// test where it should return error
pr, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
got, err := depCtx.SplitAbsoluteProjectRoot("tra/la/la/la")
if err == nil {
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", pr)
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", got)
}
}
@ -81,12 +81,12 @@ func TestAbsoluteProjectRoot(t *testing.T) {
}
for i, ok := range importPaths {
pr, err := depCtx.absoluteProjectRoot(i)
got, err := depCtx.absoluteProjectRoot(i)
if ok {
h.Must(err)
expected := h.Path(filepath.Join("src", i))
if pr != expected {
t.Fatalf("expected %s, got %q", expected, pr)
want := h.Path(filepath.Join("src", i))
if got != want {
t.Fatalf("expected %s, got %q", want, got)
}
continue
}
@ -140,11 +140,11 @@ func TestVersionInWorkspace(t *testing.T) {
h.RunGit(repoDir, "checkout", info.rev.String())
}
v, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip))
got, err := depCtx.VersionInWorkspace(gps.ProjectRoot(ip))
h.Must(err)
if v != info.rev {
t.Fatalf("expected %q, got %q", v.String(), info.rev.String())
if got != info.rev {
t.Fatalf("expected %q, got %q", got.String(), info.rev.String())
}
}
}

Просмотреть файл

@ -21,7 +21,7 @@ func TestCopyDir(t *testing.T) {
defer os.RemoveAll(dir)
srcdir := filepath.Join(dir, "src")
if err := os.MkdirAll(srcdir, 0755); err != nil {
if err = os.MkdirAll(srcdir, 0755); err != nil {
t.Fatal(err)
}
@ -30,14 +30,14 @@ func TestCopyDir(t *testing.T) {
t.Fatal(err)
}
contents := "hello world"
if _, err := srcf.Write([]byte(contents)); err != nil {
want := "hello world"
if _, err = srcf.Write([]byte(want)); err != nil {
t.Fatal(err)
}
srcf.Close()
destdir := filepath.Join(dir, "dest")
if err := CopyDir(srcdir, destdir); err != nil {
if err = CopyDir(srcdir, destdir); err != nil {
t.Fatal(err)
}
@ -50,27 +50,27 @@ func TestCopyDir(t *testing.T) {
}
destf := filepath.Join(destdir, "myfile")
destcontents, err := ioutil.ReadFile(destf)
got, err := ioutil.ReadFile(destf)
if err != nil {
t.Fatal(err)
}
if contents != string(destcontents) {
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
if want != string(got) {
t.Fatalf("expected: %s, got: %s", want, string(got))
}
srcinfo, err := os.Stat(srcf.Name())
wantinfo, err := os.Stat(srcf.Name())
if err != nil {
t.Fatal(err)
}
destinfo, err := os.Stat(destf)
gotinfo, err := os.Stat(destf)
if err != nil {
t.Fatal(err)
}
if srcinfo.Mode() != destinfo.Mode() {
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode())
if wantinfo.Mode() != gotinfo.Mode() {
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
}
}
@ -86,8 +86,8 @@ func TestCopyFile(t *testing.T) {
t.Fatal(err)
}
contents := "hello world"
if _, err := srcf.Write([]byte(contents)); err != nil {
want := "hello world"
if _, err := srcf.Write([]byte(want)); err != nil {
t.Fatal(err)
}
srcf.Close()
@ -97,27 +97,27 @@ func TestCopyFile(t *testing.T) {
t.Fatal(err)
}
destcontents, err := ioutil.ReadFile(destf)
got, err := ioutil.ReadFile(destf)
if err != nil {
t.Fatal(err)
}
if contents != string(destcontents) {
t.Fatalf("expected: %s, got: %s", contents, string(destcontents))
if want != string(got) {
t.Fatalf("expected: %s, got: %s", want, string(got))
}
srcinfo, err := os.Stat(srcf.Name())
wantinfo, err := os.Stat(srcf.Name())
if err != nil {
t.Fatal(err)
}
destinfo, err := os.Stat(destf)
gotinfo, err := os.Stat(destf)
if err != nil {
t.Fatal(err)
}
if srcinfo.Mode() != destinfo.Mode() {
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode())
if wantinfo.Mode() != gotinfo.Mode() {
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
}
}
@ -134,10 +134,10 @@ func TestIsRegular(t *testing.T) {
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
}
for f, expected := range tests {
fileOK, err := IsRegular(f)
for f, want := range tests {
got, err := IsRegular(f)
if err != nil {
if !expected {
if !want {
// this is the case where we expect an error so continue
// to the check below
continue
@ -145,8 +145,8 @@ func TestIsRegular(t *testing.T) {
t.Fatalf("expected no error, got %v", err)
}
if fileOK != expected {
t.Fatalf("expected %t for %s, got %t", expected, f, fileOK)
if got != want {
t.Fatalf("expected %t for %s, got %t", want, f, got)
}
}
@ -165,10 +165,10 @@ func TestIsDir(t *testing.T) {
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
}
for f, expected := range tests {
dirOK, err := IsDir(f)
for f, want := range tests {
got, err := IsDir(f)
if err != nil {
if !expected {
if !want {
// this is the case where we expect an error so continue
// to the check below
continue
@ -176,8 +176,8 @@ func TestIsDir(t *testing.T) {
t.Fatalf("expected no error, got %v", err)
}
if dirOK != expected {
t.Fatalf("expected %t for %s, got %t", expected, f, dirOK)
if got != want {
t.Fatalf("expected %t for %s, got %t", want, f, got)
}
}

Просмотреть файл

@ -19,13 +19,13 @@ func TestReadLock(t *testing.T) {
defer h.Cleanup()
golden := "lock/golden0.json"
l, err := readLock(h.GetTestFile(golden))
got, err := readLock(h.GetTestFile(golden))
if err != nil {
t.Fatalf("Should have read Lock correctly, but got err %q", err)
}
b, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
l2 := &Lock{
want := &Lock{
Memo: b,
P: []gps.LockedProject{
gps.NewLockedProject(
@ -36,18 +36,18 @@ func TestReadLock(t *testing.T) {
},
}
if !reflect.DeepEqual(l, l2) {
if !reflect.DeepEqual(got, want) {
t.Error("Valid lock did not parse as expected")
}
golden = "lock/golden1.json"
l, err = readLock(h.GetTestFile(golden))
got, err = readLock(h.GetTestFile(golden))
if err != nil {
t.Fatalf("Should have read Lock correctly, but got err %q", err)
}
b, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
l2 = &Lock{
want = &Lock{
Memo: b,
P: []gps.LockedProject{
gps.NewLockedProject(
@ -58,7 +58,7 @@ func TestReadLock(t *testing.T) {
},
}
if !reflect.DeepEqual(l, l2) {
if !reflect.DeepEqual(got, want) {
t.Error("Valid lock did not parse as expected")
}
}
@ -68,7 +68,7 @@ func TestWriteLock(t *testing.T) {
defer h.Cleanup()
golden := "lock/golden0.json"
lg := h.GetTestFileString(golden)
want := h.GetTestFileString(golden)
memo, _ := hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
l := &Lock{
Memo: memo,
@ -81,23 +81,23 @@ func TestWriteLock(t *testing.T) {
},
}
b, err := l.MarshalJSON()
got, err := l.MarshalJSON()
if err != nil {
t.Fatalf("Error while marshaling valid lock to JSON: %q", err)
}
if string(b) != lg {
if string(got) != want {
if *test.UpdateGolden {
if err = h.WriteTestFile(golden, string(b)); err != nil {
if err = h.WriteTestFile(golden, string(got)); err != nil {
t.Fatal(err)
}
} else {
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b))
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want)
}
}
golden = "lock/golden1.json"
lg = h.GetTestFileString(golden)
want = h.GetTestFileString(golden)
memo, _ = hex.DecodeString("2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e")
l = &Lock{
Memo: memo,
@ -110,18 +110,18 @@ func TestWriteLock(t *testing.T) {
},
}
b, err = l.MarshalJSON()
got, err = l.MarshalJSON()
if err != nil {
t.Fatalf("Error while marshaling valid lock to JSON: %q", err)
}
if string(b) != lg {
if string(got) != want {
if *test.UpdateGolden {
if err = h.WriteTestFile(golden, string(b)); err != nil {
if err = h.WriteTestFile(golden, string(got)); err != nil {
t.Fatal(err)
}
} else {
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", lg, string(b))
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want)
}
}
}
@ -148,26 +148,4 @@ func TestReadLockErrors(t *testing.T) {
t.Errorf("Unexpected error %q; expected %s error", err, tst.name)
}
}
// _, err = readLock(h.GetTestFile("lock/error0.json"))
// if err == nil {
// t.Error("Reading lock with invalid props should have caused error, but did not")
// } else if !strings.Contains(err.Error(), "both a branch") {
// t.Errorf("Unexpected error %q; expected multiple version error", err)
// }
//
// _, err = readLock(h.GetTestFile("lock/error1.json"))
// if err == nil {
// t.Error("Reading lock with invalid hash should have caused error, but did not")
// } else if !strings.Contains(err.Error(), "invalid hash") {
// t.Errorf("Unexpected error %q; expected invalid hash error", err)
// }
//
// _, err = readLock(h.GetTestFile("lock/error2.json"))
// if err == nil {
// t.Error("Reading lock with invalid props should have caused error, but did not")
// } else if !strings.Contains(err.Error(), "no version") {
// t.Errorf("Unexpected error %q; expected no version error", err)
// }
}

Просмотреть файл

@ -17,20 +17,13 @@ func TestReadManifest(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
_, err := readManifest(h.GetTestFile("manifest/error.json"))
if err == nil {
t.Error("Reading manifest with invalid props should have caused error, but did not")
} else if !strings.Contains(err.Error(), "multiple constraints") {
t.Errorf("Unexpected error %q; expected multiple constraint error", err)
}
m2, err := readManifest(h.GetTestFile("manifest/golden.json"))
got, err := readManifest(h.GetTestFile("manifest/golden.json"))
if err != nil {
t.Fatalf("Should have read Manifest correctly, but got err %q", err)
}
c, _ := gps.NewSemverConstraint(">=0.12.0, <1.0.0")
em := Manifest{
want := Manifest{
Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{
gps.ProjectRoot("github.com/sdboyer/gps"): {
Constraint: c,
@ -48,13 +41,13 @@ func TestReadManifest(t *testing.T) {
Ignores: []string{"github.com/foo/bar"},
}
if !reflect.DeepEqual(m2.Dependencies, em.Dependencies) {
if !reflect.DeepEqual(got.Dependencies, want.Dependencies) {
t.Error("Valid manifest's dependencies did not parse as expected")
}
if !reflect.DeepEqual(m2.Ovr, em.Ovr) {
if !reflect.DeepEqual(got.Ovr, want.Ovr) {
t.Error("Valid manifest's overrides did not parse as expected")
}
if !reflect.DeepEqual(m2.Ignores, em.Ignores) {
if !reflect.DeepEqual(got.Ignores, want.Ignores) {
t.Error("Valid manifest's ignores did not parse as expected")
}
}
@ -64,7 +57,7 @@ func TestWriteManifest(t *testing.T) {
defer h.Cleanup()
golden := "manifest/golden.json"
jg := h.GetTestFileString(golden)
want := h.GetTestFileString(golden)
c, _ := gps.NewSemverConstraint("^v0.12.0")
m := &Manifest{
Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{
@ -84,18 +77,40 @@ func TestWriteManifest(t *testing.T) {
Ignores: []string{"github.com/foo/bar"},
}
b, err := m.MarshalJSON()
got, err := m.MarshalJSON()
if err != nil {
t.Fatalf("Error while marshaling valid manifest to JSON: %q", err)
}
if string(b) != jg {
if string(got) != want {
if *test.UpdateGolden {
if err = h.WriteTestFile(golden, string(b)); err != nil {
if err = h.WriteTestFile(golden, string(got)); err != nil {
t.Fatal(err)
}
} else {
t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", jg, string(b))
t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want)
}
}
}
func TestReadManifestErrors(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()
var err error
tests := []struct {
name string
file string
}{
{"multiple constraints", "manifest/error.json"},
}
for _, tst := range tests {
_, err = readManifest(h.GetTestFile(tst.file))
if err == nil {
t.Errorf("Reading manifest with %s should have caused error, but did not", tst.name)
} else if !strings.Contains(err.Error(), tst.name) {
t.Errorf("Unexpected error %q; expected %s error", err, tst.name)
}
}
}

Просмотреть файл

@ -94,14 +94,14 @@ func TestTxnWriter(t *testing.T) {
reset()
// super basic manifest and lock
goldenMan := "txn_writer/expected_manifest.json"
goldenLock := "txn_writer/expected_lock.json"
expectedManifest := h.GetTestFileString(goldenMan)
expectedLock := h.GetTestFileString(goldenLock)
goldenm := "txn_writer/expected_manifest.json"
goldenl := "txn_writer/expected_lock.json"
wantm := h.GetTestFileString(goldenm)
wantl := h.GetTestFileString(goldenl)
m, err := readManifest(h.GetTestFile(goldenMan))
m, err := readManifest(h.GetTestFile(goldenm))
h.Must(err)
l, err := readLock(h.GetTestFile(goldenLock))
l, err := readLock(h.GetTestFile(goldenl))
h.Must(err)
// Just write manifest
@ -111,15 +111,15 @@ func TestTxnWriter(t *testing.T) {
h.MustNotExist(lpath)
h.MustNotExist(vpath)
diskm := h.ReadManifest()
if expectedManifest != diskm {
gotm := h.ReadManifest()
if wantm != gotm {
if *test.UpdateGolden {
expectedManifest = diskm
if err = h.WriteTestFile(goldenMan, diskm); err != nil {
wantm = gotm
if err = h.WriteTestFile(goldenm, gotm); err != nil {
t.Fatal(err)
}
} else {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
t.Fatalf("expected %s, got %s", wantm, gotm)
}
}
@ -130,20 +130,20 @@ func TestTxnWriter(t *testing.T) {
h.MustExist(lpath)
h.MustNotExist(vpath)
diskm = h.ReadManifest()
if expectedManifest != diskm {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
gotm = h.ReadManifest()
if wantm != gotm {
t.Fatalf("expected %s, got %s", wantm, gotm)
}
diskl := h.ReadLock()
if expectedLock != diskl {
gotl := h.ReadLock()
if wantl != gotl {
if *test.UpdateGolden {
expectedLock = diskl
if err = h.WriteTestFile(goldenLock, diskl); err != nil {
wantl = gotl
if err = h.WriteTestFile(goldenl, gotl); err != nil {
t.Fatal(err)
}
} else {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
t.Fatalf("expected %s, got %s", wantl, gotl)
}
}
@ -153,14 +153,14 @@ func TestTxnWriter(t *testing.T) {
h.MustExist(vpath)
h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskm = h.ReadManifest()
if expectedManifest != diskm {
t.Fatalf("expected %s, got %s", expectedManifest, diskm)
gotm = h.ReadManifest()
if wantm != gotm {
t.Fatalf("expected %s, got %s", wantm, gotm)
}
diskl = h.ReadLock()
if expectedLock != diskl {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
gotl = h.ReadLock()
if wantl != gotl {
t.Fatalf("expected %s, got %s", wantl, gotl)
}
// start fresh, ignoring the manifest now
@ -185,9 +185,9 @@ func TestTxnWriter(t *testing.T) {
h.MustExist(vpath)
h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskl = h.ReadLock()
if expectedLock != diskl {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
gotl = h.ReadLock()
if wantl != gotl {
t.Fatalf("expected %s, got %s", wantl, gotl)
}
// repeat op to ensure good behavior when vendor dir already exists
@ -198,9 +198,9 @@ func TestTxnWriter(t *testing.T) {
h.MustExist(vpath)
h.MustExist(filepath.Join(vpath, "github.com", "sdboyer", "dep-test"))
diskl = h.ReadLock()
if expectedLock != diskl {
t.Fatalf("expected %s, got %s", expectedLock, diskl)
gotl = h.ReadLock()
if wantl != gotl {
t.Fatalf("expected %s, got %s", wantl, gotl)
}
// TODO test txn rollback cases. maybe we can force errors with chmodding?