forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
93 lines (83 loc) · 1.82 KB
/
init_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
func getProject() *Project {
return &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", mustTempDir()),
Legal: getLicense(),
Copyright: copyrightLine(),
AppName: "testproject",
PkgName: "github.com/spf13/testproject",
Viper: true,
}
}
func mustTempDir() string {
dir, err := ioutil.TempDir("", "cobra_cli_test_")
if err != nil {
panic(err)
}
return dir
}
func TestGoldenInitCmd(t *testing.T) {
project := getProject()
defer os.RemoveAll(project.AbsolutePath)
if err := project.Create(); err != nil {
t.Fatal(err)
}
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles {
generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
err := compareFiles(generatedFile, goldenFile)
if err != nil {
t.Fatal(err)
}
}
}
func TestInitNoLicense(t *testing.T) {
project := getProject()
project.Legal = noLicense
defer os.RemoveAll(project.AbsolutePath)
err := project.Create()
if err != nil {
t.Fatal(err)
}
root := project.AbsolutePath
want := []string{"main.go", "cmd/root.go"}
var got []string
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relpath, err := filepath.Rel(root, path)
if err != nil {
return err
}
got = append(got, relpath)
return nil
})
if err != nil {
t.Fatalf("walking path %q: %v", root, err)
}
sort.StringSlice(got).Sort()
sort.StringSlice(want).Sort()
if !reflect.DeepEqual(got, want) {
t.Fatalf(
"In %s, found:\n %s\nwant:\n %s",
root,
strings.Join(got, ", "),
strings.Join(want, ", "),
)
}
}