2017-05-05 08:06:27 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-01-30 08:33:51 +00:00
|
|
|
"fmt"
|
2020-04-28 22:59:18 +00:00
|
|
|
"io/ioutil"
|
2017-05-05 08:06:27 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-04-28 22:59:18 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2017-05-05 08:06:27 +00:00
|
|
|
)
|
|
|
|
|
2019-09-17 15:02:42 +00:00
|
|
|
func getProject() *Project {
|
2019-01-30 07:41:41 +00:00
|
|
|
wd, _ := os.Getwd()
|
2019-09-17 15:02:42 +00:00
|
|
|
return &Project{
|
2019-01-30 08:33:51 +00:00
|
|
|
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
|
2019-01-30 07:41:41 +00:00
|
|
|
Legal: getLicense(),
|
2019-01-30 08:46:53 +00:00
|
|
|
Copyright: copyrightLine(),
|
2019-01-30 07:41:41 +00:00
|
|
|
AppName: "testproject",
|
2019-09-17 15:02:42 +00:00
|
|
|
PkgName: "github.com/spf13/testproject",
|
|
|
|
Viper: true,
|
2019-01-30 07:41:41 +00:00
|
|
|
}
|
2019-09-17 15:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoldenInitCmd(t *testing.T) {
|
2019-01-30 07:41:41 +00:00
|
|
|
|
2020-04-28 22:59:18 +00:00
|
|
|
dir, err := ioutil.TempDir("", "cobra-init")
|
|
|
|
if err != nil {
|
2019-01-30 07:41:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-28 22:59:18 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2021-05-10 23:19:33 +00:00
|
|
|
name: "returns error when passing a relative path for project",
|
2020-04-28 22:59:18 +00:00
|
|
|
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) {
|
|
|
|
|
2021-02-08 00:08:50 +00:00
|
|
|
assertNoErr(t, initCmd.Flags().Set("pkg-name", tt.pkgName))
|
2020-04-28 22:59:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 07:41:41 +00:00
|
|
|
|
2020-04-28 22:59:18 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-05-05 08:06:27 +00:00
|
|
|
}
|
|
|
|
}
|