forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
147 lines (132 loc) · 3.03 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"github.com/spf13/viper"
)
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) {
dir, err := ioutil.TempDir("", "cobra-init")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tests := []struct {
name string
args []string
pkgName string
expectErr bool
}{
{
name: "successfully creates a project with name",
args: []string{"testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: false,
},
{
name: "returns error when passing an absolute path for project",
args: []string{dir},
pkgName: "github.com/spf13/testproject",
expectErr: true,
},
{
name: "returns error when passing an relative path for project",
args: []string{"github.com/spf13/testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
initCmd.Flags().Set("pkg-name", tt.pkgName)
viper.Set("useViper", true)
projectPath, err := initializeProject(tt.args)
defer func() {
if projectPath != "" {
os.RemoveAll(projectPath)
}
}()
if !tt.expectErr && err != nil {
t.Fatalf("did not expect an error, got %s", err)
}
if tt.expectErr {
if err == nil {
t.Fatal("expected an error but got none")
} else {
// got an expected error nothing more to do
return
}
}
expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles {
generatedFile := fmt.Sprintf("%s/%s", projectPath, 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, ", "),
)
}
}