summaryrefslogtreecommitdiff
path: root/cmd/ponzu/new_test.go
blob: 76cacb1f2ba3a13514da57c83dce70c2d79d0620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"os"
	"path/filepath"
	"testing"
)

func TestNewName2Path(t *testing.T) {
	savedGOPATH := os.Getenv("GOPATH")
	defer os.Setenv("GOPATH", savedGOPATH)
	pwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("Could not determine current working directory: %s", err)
	}

	isNil := func(e error) bool { return e == nil }
	isNonNil := func(e error) bool { return e != nil }

	baseDir := filepath.Join(pwd, "test-fixtures", "new")

	testTable := []struct {
		gopath, wd, a,
		wantP string
		wantE func(e error) bool
	}{{
		gopath: baseDir,
		wd:     filepath.Join("src", "existing"),
		a:      ".",
		wantP:  filepath.Join(pwd, "test-fixtures", "new", "src", "existing"),
		wantE:  os.IsExist,
	}, {
		gopath: baseDir,
		wd:     filepath.Join(""),
		a:      "non-existing",
		wantP:  filepath.Join(pwd, "test-fixtures", "new", "src", "non-existing"),
		wantE:  isNil,
	}, {
		gopath: baseDir,
		wd:     filepath.Join(""),
		a:      ".",
		wantP:  "",
		wantE:  isNonNil,
	}, {
		gopath: baseDir,
		wd:     "..",
		a:      ".",
		wantP:  "",
		wantE:  isNonNil,
	}}

	for _, test := range testTable {
		os.Setenv("GOPATH", test.gopath)
		err = os.Chdir(filepath.Join(test.gopath, test.wd))
		if err != nil {
			t.Fatalf("could not setup base: %s", err)
		}
		got, gotE := name2path(test.a)
		if got != test.wantP {
			t.Errorf("got '%s', want: '%s'", got, test.wantP)
		}
		if !test.wantE(gotE) {
			t.Errorf("got error '%s'", gotE)
		}
	}
}