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