OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / spf13 / cobra / cobra / cmd / init_test.go
1 package cmd
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "path/filepath"
8         "testing"
9
10         "github.com/spf13/viper"
11 )
12
13 // TestGoldenInitCmd initializes the project "github.com/spf13/testproject"
14 // in GOPATH and compares the content of files in initialized project with
15 // appropriate golden files ("testdata/*.golden").
16 // Use -update to update existing golden files.
17 func TestGoldenInitCmd(t *testing.T) {
18         projectName := "github.com/spf13/testproject"
19         project := NewProject(projectName)
20         defer os.RemoveAll(project.AbsPath())
21
22         viper.Set("author", "NAME HERE <EMAIL ADDRESS>")
23         viper.Set("license", "apache")
24         viper.Set("year", 2017)
25         defer viper.Set("author", nil)
26         defer viper.Set("license", nil)
27         defer viper.Set("year", nil)
28
29         os.Args = []string{"cobra", "init", projectName}
30         if err := rootCmd.Execute(); err != nil {
31                 t.Fatal("Error by execution:", err)
32         }
33
34         expectedFiles := []string{".", "cmd", "LICENSE", "main.go", "cmd/root.go"}
35         gotFiles := []string{}
36
37         // Check project file hierarchy and compare the content of every single file
38         // with appropriate golden file.
39         err := filepath.Walk(project.AbsPath(), func(path string, info os.FileInfo, err error) error {
40                 if err != nil {
41                         return err
42                 }
43
44                 // Make path relative to project.AbsPath().
45                 // E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
46                 // then it returns just "cmd/root.go".
47                 relPath, err := filepath.Rel(project.AbsPath(), path)
48                 if err != nil {
49                         return err
50                 }
51                 relPath = filepath.ToSlash(relPath)
52                 gotFiles = append(gotFiles, relPath)
53                 goldenPath := filepath.Join("testdata", filepath.Base(path)+".golden")
54
55                 switch relPath {
56                 // Known directories.
57                 case ".", "cmd":
58                         return nil
59                 // Known files.
60                 case "LICENSE", "main.go", "cmd/root.go":
61                         if *update {
62                                 got, err := ioutil.ReadFile(path)
63                                 if err != nil {
64                                         return err
65                                 }
66                                 if err := ioutil.WriteFile(goldenPath, got, 0644); err != nil {
67                                         t.Fatal("Error while updating file:", err)
68                                 }
69                         }
70                         return compareFiles(path, goldenPath)
71                 }
72                 // Unknown file.
73                 return errors.New("unknown file: " + path)
74         })
75         if err != nil {
76                 t.Fatal(err)
77         }
78
79         // Check if some files lack.
80         if err := checkLackFiles(expectedFiles, gotFiles); err != nil {
81                 t.Fatal(err)
82         }
83 }