Skip to content

Commit dc4d317

Browse files
committed
Improve coverage for DeriveManifestAndLock
Test error conditions in Analyzer.DeriveManifestAndLock(...): * Manifest file does not exist * Manifest file cannot be read * Manifest file is invalid Add test for Analyzer.Info() to make sure that changes to name or version do not go unnoticed. This addresses issue golang#41. Signed-off-by: Marcelo E. Magallon <[email protected]>
1 parent 89e092a commit dc4d317

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (a Analyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Man
2323
}
2424
f, err := os.Open(mf)
2525
if err != nil {
26-
return nil, nil, nil
26+
return nil, nil, err
2727
}
2828
defer f.Close()
2929

analyzer_test.go

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
package dep
66

77
import (
8-
"io/ioutil"
98
"os"
109
"path/filepath"
10+
"runtime"
1111
"testing"
1212

1313
"github.com/golang/dep/test"
1414
)
1515

16-
func TestDeriveManifestAndLock(t *testing.T) {
16+
func TestAnalyzerDeriveManifestAndLock(t *testing.T) {
1717
h := test.NewHelper(t)
1818
defer h.Cleanup()
1919

@@ -49,17 +49,75 @@ func TestDeriveManifestAndLock(t *testing.T) {
4949
}
5050
}
5151

52-
func TestDeriveManifestAndLockDoesNotExist(t *testing.T) {
53-
dir, err := ioutil.TempDir("", "dep")
52+
func TestAnalyzerDeriveManifestAndLockDoesNotExist(t *testing.T) {
53+
h := test.NewHelper(t)
54+
defer h.Cleanup()
55+
56+
h.TempDir("dep")
57+
58+
a := Analyzer{}
59+
60+
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
61+
if m != nil || l != nil || err != nil {
62+
t.Fatalf("expected manifest & lock & err to be nil: m -> %#v l -> %#v err-> %#v", m, l, err)
63+
}
64+
}
65+
66+
func TestAnalyzerDeriveManifestAndLockCannotOpen(t *testing.T) {
67+
if runtime.GOOS == "windows" {
68+
// TODO: find an implementation that works on Microsoft
69+
// Windows. Setting permissions works differently there.
70+
// os.Chmod(..., 0222) below is not enough for the file
71+
// to be write-only (unreadable), and os.Chmod(...,
72+
// 0000) returns an invalid argument error.
73+
t.Skip("skipping on windows")
74+
}
75+
76+
h := test.NewHelper(t)
77+
defer h.Cleanup()
78+
79+
h.TempDir("dep")
80+
81+
// Create an empty manifest file
82+
h.TempFile(filepath.Join("dep", ManifestName), "")
83+
84+
// Change its mode so that it cannot be read
85+
err := os.Chmod(filepath.Join(h.Path("dep"), ManifestName), 0222)
5486
if err != nil {
5587
t.Fatal(err)
5688
}
57-
defer os.RemoveAll(dir)
5889

5990
a := Analyzer{}
6091

61-
m, l, err := a.DeriveManifestAndLock(dir, "my/fake/project")
62-
if m != nil || l != nil || err != nil {
92+
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
93+
if m != nil || l != nil || err == nil {
94+
t.Fatalf("expected manifest & lock to be nil, err to be not nil: m -> %#v l -> %#v err -> %#v", m, l, err)
95+
}
96+
}
97+
98+
func TestAnalyzerDeriveManifestAndLockInvalidManifest(t *testing.T) {
99+
h := test.NewHelper(t)
100+
defer h.Cleanup()
101+
102+
h.TempDir("dep")
103+
104+
// Create a manifest with invalid contents
105+
h.TempFile(filepath.Join("dep", ManifestName), "invalid manifest")
106+
107+
a := Analyzer{}
108+
109+
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
110+
if m != nil || l != nil || err == nil {
63111
t.Fatalf("expected manifest & lock & err to be nil: m -> %#v l -> %#v err-> %#v", m, l, err)
64112
}
65113
}
114+
115+
func TestAnalyzerInfo(t *testing.T) {
116+
a := Analyzer{}
117+
118+
name, vers := a.Info()
119+
120+
if name != "dep" || vers != 1 {
121+
t.Fatalf("expected name to be 'dep' and version to be 1: name -> %q vers -> %d", name, vers)
122+
}
123+
}

0 commit comments

Comments
 (0)