github.com/Azure/draft-classic@v0.16.0/cmd/draft/create_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "regexp" 10 "strings" 11 "testing" 12 13 "github.com/Azure/draft/pkg/draft/draftpath" 14 "github.com/Azure/draft/pkg/testing/helpers" 15 ) 16 17 const gitkeepfile = ".gitkeep" 18 19 func TestCreate(t *testing.T) { 20 var generatedpath = filepath.Join("testdata", "create", "generated") 21 22 testCases := []struct { 23 src string 24 expectedErr error 25 }{ 26 {filepath.Join("testdata", "create", "src", "empty"), nil}, 27 {filepath.Join("testdata", "create", "src", "html-but-actually-go"), nil}, 28 {filepath.Join("testdata", "create", "src", "simple-go"), nil}, 29 {filepath.Join("testdata", "create", "src", "simple-go-with-draftignore"), nil}, 30 {filepath.Join("testdata", "create", "src", "simple-go-with-chart"), nil}, 31 } 32 for _, tc := range testCases { 33 t.Run(fmt.Sprintf("create %s", tc.src), func(t *testing.T) { 34 pDir, teardown := tempDir(t, "draft-create") 35 defer teardown() 36 37 destcompare := filepath.Join(generatedpath, filepath.Base(tc.src)) 38 helpers.CopyTree(t, tc.src, pDir) 39 // Test 40 create := &createCmd{ 41 appName: "myapp", 42 out: ioutil.Discard, 43 home: draftpath.Home(filepath.Join("testdata", "drafthome")), 44 dest: pDir, 45 } 46 err := create.run() 47 48 // Error checking 49 if err != tc.expectedErr { 50 t.Errorf("draft create returned an unexpected error: '%v'", err) 51 return 52 } 53 54 // append .gitkeep file on empty directories when we expect `draft create` to pass 55 if tc.expectedErr == nil { 56 addGitKeep(t, pDir) 57 } 58 59 // Compare directories to ensure they are identical 60 assertIdentical(t, pDir, destcompare) 61 }) 62 } 63 } 64 65 func TestNormalizeApplicationName(t *testing.T) { 66 testCases := []string{ 67 "AppName", 68 "appName", 69 "appname", 70 } 71 72 expected := "appname" 73 74 for _, tc := range testCases { 75 t.Run(fmt.Sprintf("normalizeApplicationName %s", tc), func(t *testing.T) { 76 create := &createCmd{ 77 appName: tc, 78 out: os.Stdout, 79 home: draftpath.Home(filepath.Join("..", "..")), 80 dest: "", 81 } 82 83 create.normalizeApplicationName() 84 assertEqualString(t, create.appName, expected) 85 }) 86 } 87 } 88 89 // tempDir create and clean a temporary directory to work in our tests 90 func tempDir(t *testing.T, description string) (string, func()) { 91 t.Helper() 92 path, err := ioutil.TempDir("", description) 93 if err != nil { 94 t.Fatalf("err: %s", err) 95 } 96 return path, func() { 97 if err := os.RemoveAll(path); err != nil { 98 t.Fatalf("err: %s", err) 99 } 100 } 101 } 102 103 // add .gitkeep to generated empty directories 104 func addGitKeep(t *testing.T, p string) { 105 t.Helper() 106 if err := filepath.Walk(p, func(p string, info os.FileInfo, err error) error { 107 if err != nil { 108 return err 109 } 110 if !info.IsDir() { 111 return nil 112 } 113 files, err := ioutil.ReadDir(p) 114 if err != nil { 115 return err 116 } 117 if len(files) == 0 { 118 f, err := os.OpenFile(filepath.Join(p, gitkeepfile), os.O_RDONLY|os.O_CREATE, 0666) 119 if err != nil { 120 return err 121 } 122 defer f.Close() 123 } 124 return nil 125 }); err != nil { 126 t.Fatalf("couldn't stamp git keep files: %v", err) 127 } 128 } 129 130 // Compares two strings and asserts equivalence. 131 func assertEqualString(t *testing.T, is string, shouldBe string) { 132 t.Helper() 133 if is == shouldBe { 134 return 135 } 136 137 t.Fatalf("Assertion failed: Expected: %s. Got: %s", shouldBe, is) 138 } 139 140 // assertIdentical compares recursively all original and generated file content 141 func assertIdentical(t *testing.T, original, generated string) { 142 t.Helper() 143 if err := filepath.Walk(original, func(f string, fi os.FileInfo, err error) error { 144 relp := strings.TrimPrefix(f, original) 145 // root path 146 if relp == "" { 147 return nil 148 } 149 relp = relp[1:] 150 p := filepath.Join(generated, relp) 151 152 // .keep files are only for keeping directory creations in remote git repo 153 if filepath.Base(p) == gitkeepfile { 154 return nil 155 } 156 157 // chartdir should match app name, not pack name. 158 re := regexp.MustCompile("(charts.)myapp") 159 p = re.ReplaceAllString(p, "${1}go") 160 161 fo, err := os.Stat(p) 162 if err != nil { 163 t.Fatalf("%s doesn't exist while %s does", p, f) 164 } 165 166 if fi.IsDir() { 167 if !fo.IsDir() { 168 t.Fatalf("%s is a directory and %s isn't", f, p) 169 } 170 // else, it's a directory as well and we are done. 171 return nil 172 } 173 174 wanted, err := ioutil.ReadFile(f) 175 if err != nil { 176 t.Fatalf("Couldn't read %s: %v", f, err) 177 } 178 wanted = bytes.Replace(wanted, []byte("name: myapp"), []byte("name: go"), 1) 179 actual, err := ioutil.ReadFile(p) 180 if err != nil { 181 t.Fatalf("Couldn't read %s: %v", p, err) 182 } 183 if !bytes.Equal(actual, wanted) { 184 t.Errorf("%s and %s content differs:\nACTUAL:\n%s\n\nWANTED:\n%s", p, f, actual, wanted) 185 } 186 return nil 187 }); err != nil { 188 t.Fatalf("err: %s", err) 189 } 190 191 // on the other side, check that all generated items are in origin 192 if err := filepath.Walk(generated, func(f string, _ os.FileInfo, err error) error { 193 relp := strings.TrimPrefix(f, generated) 194 // root path 195 if relp == "" { 196 return nil 197 } 198 relp = relp[1:] 199 p := filepath.Join(original, relp) 200 201 // chartdir should match app name, not pack name. 202 re := regexp.MustCompile("(charts.)go") 203 p = re.ReplaceAllString(p, "${1}myapp") 204 205 // .keep files are only for keeping directory creations in remote git repo 206 if filepath.Base(p) == gitkeepfile { 207 return nil 208 } 209 210 if _, err := os.Stat(p); err != nil { 211 t.Errorf("%s doesn't exist while %s does", p, f) 212 } 213 return nil 214 }); err != nil { 215 t.Fatalf("err: %s", err) 216 } 217 }