github.com/anthonyme00/gomarkdoc@v1.0.0/cmd/gomarkdoc/command_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "log" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/matryer/is" 14 ) 15 16 var wd, _ = os.Getwd() 17 18 func TestCommand(t *testing.T) { 19 tests := []string{ 20 "./simple", 21 "./lang/function", 22 "./docs", 23 "./untagged", 24 } 25 26 for _, test := range tests { 27 t.Run(test, func(t *testing.T) { 28 is := is.New(t) 29 30 err := os.Chdir(filepath.Join(wd, "../../testData")) 31 is.NoErr(err) 32 33 harness(t, test, []string{ 34 "gomarkdoc", test, 35 "--repository.url", "https://github.com/princjef/gomarkdoc", 36 "--repository.default-branch", "master", 37 "--repository.path", "/testData/", 38 }) 39 }) 40 } 41 } 42 43 func TestCommand_check(t *testing.T) { 44 is := is.New(t) 45 46 err := os.Chdir(filepath.Join(wd, "../../testData")) 47 is.NoErr(err) 48 49 os.Args = []string{ 50 "gomarkdoc", "./simple", 51 "-c", 52 "-o", "{{.Dir}}/README-github.md", 53 "--repository.url", "https://github.com/princjef/gomarkdoc", 54 "--repository.default-branch", "master", 55 "--repository.path", "/testData/", 56 } 57 cleanup(t, "simple") 58 59 main() 60 } 61 62 func TestCommand_nested(t *testing.T) { 63 is := is.New(t) 64 65 err := os.Chdir(filepath.Join(wd, "../../testData")) 66 is.NoErr(err) 67 68 os.Args = []string{ 69 "gomarkdoc", "./nested/...", 70 "-o", "{{.Dir}}/README-github-test.md", 71 "--repository.url", "https://github.com/princjef/gomarkdoc", 72 "--repository.default-branch", "master", 73 "--repository.path", "/testData/", 74 } 75 cleanup(t, "nested") 76 cleanup(t, "nested/inner") 77 78 main() 79 80 verify(t, "nested", "github") 81 verify(t, "nested/inner", "github") 82 } 83 84 func TestCommand_unexported(t *testing.T) { 85 is := is.New(t) 86 87 err := os.Chdir(filepath.Join(wd, "../../testData")) 88 is.NoErr(err) 89 90 harness(t, "unexported", []string{ 91 "gomarkdoc", "./unexported", 92 "-u", 93 "-o", "{{.Dir}}/README-test.md", 94 "--repository.url", "https://github.com/princjef/gomarkdoc", 95 "--repository.default-branch", "master", 96 "--repository.path", "/testData/", 97 }) 98 } 99 100 func TestCommand_version(t *testing.T) { 101 is := is.New(t) 102 103 err := os.Chdir(filepath.Join(wd, "../../testData")) 104 is.NoErr(err) 105 106 os.Args = []string{"gomarkdoc", "--version"} 107 108 oldStdout := os.Stdout 109 r, w, _ := os.Pipe() 110 os.Stdout = w 111 defer func() { os.Stdout = oldStdout }() 112 113 main() 114 w.Close() 115 116 data, err := io.ReadAll(r) 117 is.NoErr(err) 118 119 is.Equal(strings.TrimSpace(string(data)), "(devel)") 120 } 121 122 func TestCommand_invalidCheck(t *testing.T) { 123 is := is.New(t) 124 125 err := os.Chdir(filepath.Join(wd, "../../testData")) 126 is.NoErr(err) 127 128 os.Args = []string{ 129 "gomarkdoc", "./simple", 130 "-c", 131 "--repository.url", "https://github.com/princjef/gomarkdoc", 132 "--repository.default-branch", "master", 133 "--repository.path", "/testData/", 134 } 135 cleanup(t, "simple") 136 137 cmd := buildCommand() 138 err = cmd.Execute() 139 t.Log(err.Error()) 140 141 is.Equal(err.Error(), "gomarkdoc: check mode cannot be run without an output set") 142 } 143 144 func TestCommand_defaultDirectory(t *testing.T) { 145 is := is.New(t) 146 147 err := os.Chdir(filepath.Join(wd, "../../testData/simple")) 148 is.NoErr(err) 149 150 harness(t, ".", []string{ 151 "gomarkdoc", 152 "--repository.url", "https://github.com/princjef/gomarkdoc", 153 "--repository.default-branch", "master", 154 "--repository.path", "/testData/simple/", 155 }) 156 } 157 158 func TestCommand_nonexistant(t *testing.T) { 159 is := is.New(t) 160 161 err := os.Chdir(filepath.Join(wd, "../../testData")) 162 is.NoErr(err) 163 164 os.Args = []string{ 165 "gomarkdoc", "./nonexistant", 166 "-o", "{{.Dir}}/README-test.md", 167 "--repository.url", "https://github.com/princjef/gomarkdoc", 168 "--repository.default-branch", "master", 169 "--repository.path", "/testData/", 170 } 171 172 cmd := buildCommand() 173 err = cmd.Execute() 174 t.Log(err.Error()) 175 is.Equal(err.Error(), fmt.Sprintf("gomarkdoc: invalid package in directory: .%snonexistant", string(filepath.Separator))) 176 } 177 178 func TestCommand_tags(t *testing.T) { 179 is := is.New(t) 180 181 err := os.Chdir(filepath.Join(wd, "../../testData")) 182 is.NoErr(err) 183 184 harness(t, "tags", []string{ 185 "gomarkdoc", "./tags", 186 "--tags", "tagged", 187 "--repository.url", "https://github.com/princjef/gomarkdoc", 188 "--repository.default-branch", "master", 189 "--repository.path", "/testData/", 190 }) 191 } 192 193 func TestCommand_tagsWithGOFLAGS(t *testing.T) { 194 is := is.New(t) 195 196 err := os.Chdir(filepath.Join(wd, "../../testData")) 197 is.NoErr(err) 198 199 os.Setenv("GOFLAGS", "-tags=tagged") 200 os.Args = []string{ 201 "gomarkdoc", "./tags", 202 "--config", "../.gomarkdoc-empty.yml", 203 "-o", "{{.Dir}}/README-github-test.md", 204 "--repository.url", "https://github.com/princjef/gomarkdoc", 205 "--repository.default-branch", "master", 206 "--repository.path", "/testData/", 207 } 208 cleanup(t, "tags") 209 210 cmd := buildCommand() 211 err = cmd.Execute() 212 is.NoErr(err) 213 214 verify(t, "./tags", "github") 215 } 216 217 func TestCommand_tagsWithGOFLAGSNoTags(t *testing.T) { 218 is := is.New(t) 219 220 err := os.Chdir(filepath.Join(wd, "../../testData")) 221 is.NoErr(err) 222 223 err = os.Setenv("GOFLAGS", "-other=foo") 224 is.NoErr(err) 225 226 os.Args = []string{ 227 "gomarkdoc", "./tags", 228 "--config", "../.gomarkdoc-empty.yml", 229 "-o", "{{.Dir}}/README-github-test.md", 230 "--repository.url", "https://github.com/princjef/gomarkdoc", 231 "--repository.default-branch", "master", 232 "--repository.path", "/testData/", 233 } 234 cleanup(t, "tags") 235 236 cmd := buildCommand() 237 err = cmd.Execute() 238 is.NoErr(err) 239 240 verifyNotEqual(t, "./tags", "github") 241 } 242 243 func TestCommand_tagsWithGOFLAGSNoParse(t *testing.T) { 244 is := is.New(t) 245 246 err := os.Chdir(filepath.Join(wd, "../../testData")) 247 is.NoErr(err) 248 249 err = os.Setenv("GOFLAGS", "invalid") 250 is.NoErr(err) 251 252 os.Args = []string{ 253 "gomarkdoc", "./tags", 254 "--config", "../.gomarkdoc-empty.yml", 255 "-o", "{{.Dir}}/README-github-test.md", 256 "--repository.url", "https://github.com/princjef/gomarkdoc", 257 "--repository.default-branch", "master", 258 "--repository.path", "/testData/", 259 } 260 cleanup(t, "tags") 261 262 cmd := buildCommand() 263 err = cmd.Execute() 264 is.NoErr(err) 265 266 verifyNotEqual(t, "./tags", "github") 267 } 268 269 func TestCommand_embed(t *testing.T) { 270 is := is.New(t) 271 272 err := os.Chdir(filepath.Join(wd, "../../testData")) 273 is.NoErr(err) 274 275 os.Args = []string{ 276 "gomarkdoc", "./embed", 277 "--embed", 278 "-o", "{{.Dir}}/README-github-test.md", 279 "--repository.url", "https://github.com/princjef/gomarkdoc", 280 "--repository.default-branch", "master", 281 "--repository.path", "/testData/", 282 } 283 cleanup(t, "embed") 284 285 data, err := os.ReadFile("./embed/README-template.md") 286 is.NoErr(err) 287 288 err = os.WriteFile("./embed/README-github-test.md", data, 0664) 289 is.NoErr(err) 290 291 main() 292 293 verify(t, "./embed", "github") 294 } 295 296 func TestCommand_embed_check(t *testing.T) { 297 is := is.New(t) 298 299 err := os.Chdir(filepath.Join(wd, "../../testData")) 300 is.NoErr(err) 301 302 os.Args = []string{ 303 "gomarkdoc", "./embed", 304 "--embed", 305 "--check", 306 "-o", "{{.Dir}}/README-github-invalid.md", 307 "--repository.url", "https://github.com/princjef/gomarkdoc", 308 "--repository.default-branch", "master", 309 "--repository.path", "/testData/", 310 } 311 cleanup(t, "embed") 312 313 log.SetFlags(0) 314 315 cmd := buildCommand() 316 317 err = cmd.Execute() 318 is.True(err != nil) // Should fail 319 320 os.Args = []string{ 321 "gomarkdoc", "./embed", 322 "--embed", 323 "--check", 324 "-o", "{{.Dir}}/README-github.md", 325 "--repository.url", "https://github.com/princjef/gomarkdoc", 326 "--repository.default-branch", "master", 327 "--repository.path", "/testData/", 328 } 329 cleanup(t, "embed") 330 331 log.SetFlags(0) 332 333 cmd = buildCommand() 334 335 err = cmd.Execute() 336 is.NoErr(err) // Should pass 337 } 338 339 func TestCompare(t *testing.T) { 340 tests := []struct { 341 b1, b2 []byte 342 equal bool 343 }{ 344 {[]byte("abc"), []byte("abc"), true}, 345 {[]byte("abc"), []byte("def"), false}, 346 {[]byte{}, []byte{}, true}, 347 {[]byte("abc"), []byte{}, false}, 348 {[]byte{}, []byte("abc"), false}, 349 } 350 351 for _, test := range tests { 352 name := fmt.Sprintf(`"%s" == "%s"`, string(test.b1), string(test.b2)) 353 if !test.equal { 354 name = fmt.Sprintf(`"%s" != "%s"`, string(test.b1), string(test.b2)) 355 } 356 357 t.Run(name, func(t *testing.T) { 358 is := is.New(t) 359 360 eq, err := compare(bytes.NewBuffer(test.b1), bytes.NewBuffer(test.b2)) 361 is.NoErr(err) 362 363 is.Equal(eq, test.equal) 364 }) 365 } 366 } 367 368 func verify(t *testing.T, dir, format string) { 369 is := is.New(t) 370 371 data, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("README-%s.md", format))) 372 is.NoErr(err) 373 374 data2, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("README-%s-test.md", format))) 375 is.NoErr(err) 376 377 is.Equal(string(data), string(data2)) 378 } 379 380 func verifyNotEqual(t *testing.T, dir, format string) { 381 is := is.New(t) 382 383 data, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("README-%s.md", format))) 384 is.NoErr(err) 385 386 data2, err := os.ReadFile(filepath.Join(dir, fmt.Sprintf("README-%s-test.md", format))) 387 is.NoErr(err) 388 389 is.True(string(data) != string(data2)) 390 } 391 392 func cleanup(t *testing.T, dir string) { 393 f, err := os.ReadDir(dir) 394 if err != nil { 395 t.Fatal(err) 396 } 397 398 for _, n := range f { 399 if n.IsDir() { 400 continue 401 } 402 403 if !strings.HasPrefix(n.Name(), "README") || !strings.HasSuffix(n.Name(), "-test.md") { 404 continue 405 } 406 407 os.Remove(filepath.Join(dir, n.Name())) 408 409 } 410 } 411 412 // harness runs the test for all formats. Omit the --output and --format args to 413 // the command when running this as it will fill them in for you 414 func harness(t *testing.T, dir string, args []string) { 415 for _, format := range []string{"plain", "github", "azure-devops"} { 416 os.Args = args 417 os.Args = append(os.Args, "-o", fmt.Sprintf("{{.Dir}}/README-%s-test.md", format)) 418 os.Args = append(os.Args, "--format", format) 419 420 cleanup(t, dir) 421 422 main() 423 424 verify(t, dir, format) 425 } 426 }