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