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

Commit 13f512f

Browse files
committed
dep: cleanup TestLoadProject and TestDetectProjectGOPATH
Signed-off-by: Ibrahim AshShohail <[email protected]>
1 parent a69d6ae commit 13f512f

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (c *Ctx) DetectProjectGOPATH(p *Project) (string, error) {
230230
// detectGOPATH detects the GOPATH for a given path from ctx.GOPATHs.
231231
func (c *Ctx) detectGOPATH(path string) (string, error) {
232232
for _, gp := range c.GOPATHs {
233-
if fs.HasFilepathPrefix(filepath.ToSlash(path), gp) {
233+
if fs.HasFilepathPrefix(filepath.FromSlash(path), gp) {
234234
return gp, nil
235235
}
236236
}

context_test.go

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -149,49 +149,50 @@ func TestVersionInWorkspace(t *testing.T) {
149149
}
150150

151151
func TestLoadProject(t *testing.T) {
152-
tg := test.NewHelper(t)
153-
defer tg.Cleanup()
152+
h := test.NewHelper(t)
153+
defer h.Cleanup()
154154

155-
tg.TempDir("src")
156-
tg.TempDir("src/test1")
157-
tg.TempDir("src/test1/sub")
158-
tg.TempFile(filepath.Join("src/test1", ManifestName), "")
159-
tg.TempFile(filepath.Join("src/test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
160-
tg.TempDir("src/test2")
161-
tg.TempDir("src/test2/sub")
162-
tg.TempFile(filepath.Join("src/test2", ManifestName), "")
155+
h.TempDir(filepath.Join("src", "test1", "sub"))
156+
h.TempFile(filepath.Join("src", "test1", ManifestName), "")
157+
h.TempFile(filepath.Join("src", "test1", LockName), `memo = "cdafe8641b28cd16fe025df278b0a49b9416859345d8b6ba0ace0272b74925ee"`)
158+
h.TempDir(filepath.Join("src", "test2", "sub"))
159+
h.TempFile(filepath.Join("src", "test2", ManifestName), "")
163160

164161
var testcases = []struct {
165-
lock bool
166-
start string
162+
name string
163+
lock bool
164+
wd string
167165
}{
168-
{true, filepath.Join("src", "test1")}, //direct
169-
{true, filepath.Join("src", "test1", "sub")}, //ascending
170-
{false, filepath.Join("src", "test2")}, //repeat without lockfile present
171-
{false, filepath.Join("src", "test2", "sub")},
166+
{"direct", true, filepath.Join("src", "test1")},
167+
{"ascending", true, filepath.Join("src", "test1", "sub")},
168+
{"without lock", false, filepath.Join("src", "test2")},
169+
{"ascending without lock", false, filepath.Join("src", "test2", "sub")},
172170
}
173171

174-
for _, testcase := range testcases {
175-
start := testcase.start
172+
for _, tc := range testcases {
173+
t.Run(tc.name, func(t *testing.T) {
174+
ctx := &Ctx{
175+
Out: discardLogger,
176+
Err: discardLogger,
177+
}
176178

177-
ctx := &Ctx{
178-
GOPATHs: []string{tg.Path(".")},
179-
WorkingDir: tg.Path(start),
180-
Out: discardLogger,
181-
Err: discardLogger,
182-
}
179+
err := ctx.SetPaths(h.Path(tc.wd), h.Path("."))
180+
if err != nil {
181+
t.Fatalf("%+v", err)
182+
}
183183

184-
proj, err := ctx.LoadProject()
185-
switch {
186-
case err != nil:
187-
t.Errorf("%s: LoadProject failed: %+v", start, err)
188-
case proj.Manifest == nil:
189-
t.Errorf("%s: Manifest file didn't load", start)
190-
case testcase.lock && proj.Lock == nil:
191-
t.Errorf("%s: Lock file didn't load", start)
192-
case !testcase.lock && proj.Lock != nil:
193-
t.Errorf("%s: Non-existent Lock file loaded", start)
194-
}
184+
p, err := ctx.LoadProject()
185+
switch {
186+
case err != nil:
187+
t.Fatalf("%s: LoadProject failed: %+v", tc.wd, err)
188+
case p.Manifest == nil:
189+
t.Fatalf("%s: Manifest file didn't load", tc.wd)
190+
case tc.lock && p.Lock == nil:
191+
t.Fatalf("%s: Lock file didn't load", tc.wd)
192+
case !tc.lock && p.Lock != nil:
193+
t.Fatalf("%s: Non-existent Lock file loaded", tc.wd)
194+
}
195+
})
195196
}
196197
}
197198

@@ -438,10 +439,10 @@ func TestDetectProjectGOPATH(t *testing.T) {
438439
}
439440

440441
GOPATH, err := ctx.DetectProjectGOPATH(project)
441-
if !tc.expectErr {
442-
h.Must(err)
443-
} else if err == nil {
444-
t.Fatal("expected an error, got nil")
442+
if !tc.expectErr && err != nil {
443+
t.Fatalf("%+v", err)
444+
} else if tc.expectErr && err == nil {
445+
t.Fatalf("expected an error, got nil and gopath %s", GOPATH)
445446
}
446447
if GOPATH != tc.GOPATH {
447448
t.Errorf("expected GOPATH %s, got %s", tc.GOPATH, GOPATH)

0 commit comments

Comments
 (0)