github.com/fujimura/git-gsub@v0.1.3-0.20230615040236-397545a1d305/main_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "io/ioutil" 8 "log" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "runtime" 13 "testing" 14 ) 15 16 func RunInTmpDir(run func()) { 17 dir, err := ioutil.TempDir("", "") 18 if err != nil { 19 log.Fatal(err) 20 } 21 22 current, err := os.Getwd() 23 if err != nil { 24 panic(err) 25 } 26 27 err = os.Chdir(dir) 28 if err != nil { 29 panic(err) 30 } 31 32 run() 33 34 defer func() { 35 os.RemoveAll(dir) // clean up 36 os.Chdir(current) 37 }() 38 39 } 40 41 func RunInTmpRepo(run func()) { 42 commands := []string{ 43 "git init", 44 "git config --local user.email \"you@example.com\"", 45 "git config --local user.name \"Your Name\"", 46 } 47 RunInTmpDir(func() { 48 for _, command := range commands { 49 err := exec.Command("sh", "-c", command).Run() 50 if err != nil { 51 panic(err) 52 } 53 } 54 55 run() 56 }) 57 } 58 59 func CommitFile(name string, content string) { 60 err := os.MkdirAll(filepath.Dir(name), os.ModePerm) 61 if err != nil { 62 panic(err) 63 } 64 65 err = ioutil.WriteFile(name, []byte(content), os.ModePerm) 66 if err != nil { 67 panic(err) 68 } 69 70 err = exec.Command("git", "add", ".").Run() 71 if err != nil { 72 panic(err) 73 } 74 75 err = exec.Command("git", "commit", "-m", fmt.Sprintf("\"Add\" %s", name)).Run() 76 if err != nil { 77 panic(err) 78 } 79 } 80 81 func GitGsubPath() string { 82 _, filename, _, _ := runtime.Caller(0) 83 84 return filepath.Clean(fmt.Sprintf("%s/../bin/git-gsub", filename)) 85 } 86 87 func RunGitGsub(args ...string) ([]byte, error) { 88 var out []byte 89 var err error 90 91 _, e2e := os.LookupEnv("E2E") 92 93 if e2e { 94 out, err = exec.Command(GitGsubPath(), args...).Output() 95 } else { 96 outStream, errStream := new(bytes.Buffer), new(bytes.Buffer) 97 98 cli := &CLI{outStream: outStream, errStream: errStream} 99 exitcode := cli.Run(args) 100 if exitcode != 0 { 101 err = errors.New(errStream.String()) 102 } 103 out = outStream.Bytes() 104 } 105 return out, err 106 } 107 108 func TestVersion(t *testing.T) { 109 out, err := RunGitGsub("--version") 110 111 if err != nil { 112 t.Errorf("Command failed: %s", err) 113 } 114 115 if string(out) != "v0.1.2\n" { 116 t.Errorf("Failed: %s", string(out)) 117 } 118 } 119 120 func TestSimpleSubstitution(t *testing.T) { 121 RunInTmpRepo(func() { 122 CommitFile("README.md", "Git Subversion Bzr") 123 _, err := RunGitGsub("Bzr", "Mercurial") 124 125 if err != nil { 126 t.Errorf("Command failed: %s", err) 127 } 128 129 dat, _ := ioutil.ReadFile("./README.md") 130 if string(dat) != "Git Subversion Mercurial" { 131 t.Errorf("Failed: %s", string(dat)) 132 } 133 }) 134 } 135 136 func TestSimpleSubstitutionManyFiles(t *testing.T) { 137 RunInTmpRepo(func() { 138 CommitFile("README_1.md", "Git Subversion Bzr") 139 CommitFile("README_2.md", "Git Subversion Bzr") 140 CommitFile("README_3.md", "Git Subversion Bzr") 141 CommitFile("README_4.md", "Git Subversion Bzr") 142 CommitFile("README_5.md", "Git Subversion Bzr") 143 CommitFile("README_6.md", "Git Subversion Bzr") 144 _, err := RunGitGsub("Bzr", "Mercurial") 145 146 if err != nil { 147 t.Errorf("Command failed: %s", err) 148 } 149 150 dat, _ := ioutil.ReadFile("./README_1.md") 151 if string(dat) != "Git Subversion Mercurial" { 152 t.Errorf("Failed: %s", string(dat)) 153 } 154 }) 155 } 156 157 func TestSubstitutionWithPath(t *testing.T) { 158 RunInTmpRepo(func() { 159 CommitFile("README.md", "Git Subversion Bzr") 160 CommitFile("foo/git", "Git Subversion Bzr") 161 CommitFile("bar/git", "Git Subversion Bzr") 162 163 _, err := RunGitGsub("Git", "Svn", "foo") 164 165 if err != nil { 166 t.Errorf("Command failed: %s", err) 167 } 168 169 dat, _ := ioutil.ReadFile("./README.md") 170 if string(dat) != "Git Subversion Bzr" { 171 t.Errorf("Failed: %s", string(dat)) 172 } 173 174 dat, _ = ioutil.ReadFile("./foo/git") 175 if string(dat) != "Svn Subversion Bzr" { 176 t.Errorf("Failed: %s", string(dat)) 177 } 178 }) 179 } 180 181 func TestSubstitutionWithCaseConversion(t *testing.T) { 182 RunInTmpRepo(func() { 183 CommitFile("README.md", "GitGsub git_gsub git-gsub") 184 _, err := RunGitGsub("--camel", "--kebab", "--snake", "git-gsub", "svn-gsub") 185 186 if err != nil { 187 t.Errorf("Command failed: %s", err) 188 } 189 190 dat, _ := ioutil.ReadFile("./README.md") 191 if string(dat) != "SvnGsub svn_gsub svn-gsub" { 192 t.Errorf("Failed: %s", string(dat)) 193 } 194 }) 195 } 196 197 func TestSubstitutionWithUpperAndLowerCamelCaseConversion(t *testing.T) { 198 RunInTmpRepo(func() { 199 CommitFile("README.md", "GitGsub gitGsub") 200 _, err := RunGitGsub("--upper-camel", "--lower-camel", "git-gsub", "svn-gsub") 201 202 if err != nil { 203 t.Errorf("Command failed: %s", err) 204 } 205 206 dat, _ := ioutil.ReadFile("./README.md") 207 if string(dat) != "SvnGsub svnGsub" { 208 t.Errorf("Failed: %s", string(dat)) 209 } 210 }) 211 } 212 213 func TestSubstitutionOfAllUnderscoredPhraseWithCaseConversion(t *testing.T) { 214 RunInTmpRepo(func() { 215 CommitFile("README.md", "activerecord") 216 _, err := RunGitGsub("activerecord", "inactiverecord", "--kebab", "--snake", "--camel") 217 218 if err != nil { 219 t.Errorf("Command failed: %s", err) 220 } 221 222 dat, _ := ioutil.ReadFile("./README.md") 223 if string(dat) != "inactiverecord" { 224 t.Errorf("Failed: %s", string(dat)) 225 } 226 }) 227 } 228 229 func TestOptionsCanBePutAfterArguments(t *testing.T) { 230 RunInTmpRepo(func() { 231 CommitFile("README.md", "GitGsub git_gsub git-gsub") 232 _, err := RunGitGsub("git-gsub", "svn-gsub", "--camel", "--kebab", "--snake") 233 234 if err != nil { 235 t.Errorf("Command failed: %s", err) 236 } 237 238 dat, _ := ioutil.ReadFile("./README.md") 239 if string(dat) != "SvnGsub svn_gsub svn-gsub" { 240 t.Errorf("Failed: %s", string(dat)) 241 } 242 }) 243 } 244 245 func TestSubstitutionWithFixedStringOption(t *testing.T) { 246 RunInTmpRepo(func() { 247 CommitFile("hello.rb", "puts('hello')") 248 249 _, err := RunGitGsub("--fgrep", "(", " ") 250 if err != nil { 251 t.Errorf("Command failed: %s", err) 252 } 253 254 _, err = RunGitGsub("-F", ")", "") 255 if err != nil { 256 t.Errorf("Command failed: %s", err) 257 } 258 259 dat, _ := ioutil.ReadFile("./hello.rb") 260 if string(dat) != "puts 'hello'" { 261 t.Errorf("Failed: %s", string(dat)) 262 } 263 }) 264 } 265 266 func TestEscape(t *testing.T) { 267 RunInTmpRepo(func() { 268 CommitFile("README.md", `<h1 class="foo">`) 269 _, err := RunGitGsub(`<h1 class="foo">`, `<h1 class="bar">`) 270 271 if err != nil { 272 t.Errorf("Command failed: %s", err) 273 } 274 275 dat, _ := ioutil.ReadFile("./README.md") 276 if string(dat) != `<h1 class="bar">` { 277 t.Errorf("Failed: %s", string(dat)) 278 } 279 }) 280 } 281 282 func TestAtMark(t *testing.T) { 283 RunInTmpRepo(func() { 284 CommitFile("README.md", "foo@example.com") 285 _, err := RunGitGsub("foo@example.com", "bar@example.com") 286 if err != nil { 287 t.Errorf("Command failed: %s", err) 288 } 289 290 dat, _ := ioutil.ReadFile("./README.md") 291 if string(dat) != "bar@example.com" { 292 t.Errorf("Failed: %s", string(dat)) 293 } 294 }) 295 } 296 297 func TestConsequesingAtMark(t *testing.T) { 298 RunInTmpRepo(func() { 299 CommitFile("README.md", "foo@example.com") 300 _, err := RunGitGsub("foo@example.com", "bar@@example.com") 301 if err != nil { 302 t.Errorf("Command failed: %s", err) 303 } 304 305 dat, _ := ioutil.ReadFile("./README.md") 306 if string(dat) != "bar@@example.com" { 307 t.Errorf("Failed: %s", string(dat)) 308 } 309 }) 310 } 311 312 func TestDoubleQuoteToSingleQuote(t *testing.T) { 313 RunInTmpRepo(func() { 314 CommitFile("README.md", `hello this is "git"`) 315 _, err := RunGitGsub(`"git"`, `'svn'`) 316 if err != nil { 317 t.Errorf("Command failed: %s", err) 318 } 319 320 dat, _ := ioutil.ReadFile("./README.md") 321 if string(dat) != "hello this is 'svn'" { 322 t.Errorf("Failed: %s", string(dat)) 323 } 324 }) 325 } 326 327 func TestSingleQuoteToDoubleQuote(t *testing.T) { 328 RunInTmpRepo(func() { 329 CommitFile("README.md", `hello this is 'git'`) 330 _, err := RunGitGsub(`'git'`, `"svn"`) 331 if err != nil { 332 t.Errorf("Command failed: %s", err) 333 } 334 335 dat, _ := ioutil.ReadFile("./README.md") 336 if string(dat) != `hello this is "svn"` { 337 t.Errorf("Failed: %s", string(dat)) 338 } 339 }) 340 } 341 342 func TestBracket(t *testing.T) { 343 RunInTmpRepo(func() { 344 CommitFile("README.md", `{git{svn}}`) 345 _, err := RunGitGsub("{git{svn}}", "{hg{svn}}") 346 if err != nil { 347 t.Errorf("Command failed: %s", err) 348 } 349 350 dat, _ := ioutil.ReadFile("./README.md") 351 if string(dat) != "{hg{svn}}" { 352 t.Errorf("Failed: %s", string(dat)) 353 } 354 }) 355 } 356 357 func TestSubmatch(t *testing.T) { 358 RunInTmpRepo(func() { 359 CommitFile("README.md", "git-foo-1 git-bar-22 git-baz-3") 360 _, err := RunGitGsub(`git-([a-z]+)-([\d]{1,2})`, `$2-$1`) 361 if err != nil { 362 t.Errorf("Command failed: %s", err) 363 } 364 365 dat, _ := ioutil.ReadFile("./README.md") 366 if string(dat) != "1-foo 22-bar 3-baz" { 367 t.Errorf("Failed: %s", string(dat)) 368 } 369 }) 370 } 371 372 func TestSubstituteToEmptyString(t *testing.T) { 373 RunInTmpRepo(func() { 374 CommitFile("README.md", "Git Svn Hg") 375 _, err := RunGitGsub("Svn ", "") 376 if err != nil { 377 t.Errorf("Command failed: %s", err) 378 } 379 380 dat, _ := ioutil.ReadFile("./README.md") 381 if string(dat) != "Git Hg" { 382 t.Errorf("Failed: %s", string(dat)) 383 } 384 }) 385 } 386 387 func TestUTF8Filename(t *testing.T) { 388 RunInTmpRepo(func() { 389 CommitFile("よんでね.txt", "よんでね") 390 _, err := RunGitGsub("でね", "だよ") 391 if err != nil { 392 t.Errorf("Command failed: %s", err) 393 } 394 395 dat, _ := ioutil.ReadFile("./よんでね.txt") 396 if string(dat) != "よんだよ" { 397 t.Errorf("Failed: %s", string(dat)) 398 } 399 }) 400 } 401 402 func TestSimpleRename(t *testing.T) { 403 RunInTmpRepo(func() { 404 CommitFile("README-git_gsub.md", "GitGsub git_gsub git-gsub") 405 _, err := RunGitGsub("--snake", "--rename", "GitGsub", "SvnGsub") 406 if err != nil { 407 t.Errorf("Command failed: %s", err) 408 } 409 410 dat, _ := ioutil.ReadFile("./README-svn_gsub.md") 411 if string(dat) != "SvnGsub svn_gsub git-gsub" { 412 t.Errorf("Failed: %s", string(dat)) 413 } 414 }) 415 } 416 417 func TestRuby(t *testing.T) { 418 RunInTmpRepo(func() { 419 CommitFile("./foo_bar/baz.rb", "module FooBar::Baz; foo_bar baz # foo_bar/baz; end") 420 _, err := RunGitGsub("--ruby", "--rename", "FooBar::Baz", "QuxQuux::Quuz") 421 if err != nil { 422 t.Errorf("Command failed: %s", err) 423 } 424 425 dat, _ := ioutil.ReadFile("./qux_quux/quuz.rb") 426 if string(dat) != "module QuxQuux::Quuz; foo_bar baz # qux_quux/quuz; end" { 427 t.Errorf("Failed: %s", string(dat)) 428 } 429 }) 430 } 431 func TestAll(t *testing.T) { 432 RunInTmpRepo(func() { 433 CommitFile("./foo_bar.rb", "module FooBar; foo_bar foo-bar; end") 434 _, err := RunGitGsub("--all", "--rename", "FooBar", "BazQux") 435 if err != nil { 436 t.Errorf("Command failed: %s", err) 437 } 438 439 dat, _ := ioutil.ReadFile("./baz_qux.rb") 440 if string(dat) != "module BazQux; baz_qux baz-qux; end" { 441 t.Errorf("Failed: %s", string(dat)) 442 } 443 }) 444 } 445 446 func TestAllDoesntImplyRuby(t *testing.T) { 447 RunInTmpRepo(func() { 448 CommitFile("./foo_bar/baz.rb", "module FooBar::Baz; foo_bar baz # foo_bar/baz; end") 449 _, err := RunGitGsub("--all", "--rename", "FooBar::Baz", "QuxQuux::Quuz") 450 if err != nil { 451 t.Errorf("Command failed: %s", err) 452 } 453 454 dat, _ := ioutil.ReadFile("./foo_bar/baz.rb") 455 if string(dat) != "module QuxQuux::Quuz; foo_bar baz # foo_bar/baz; end" { 456 t.Errorf("Failed: %s", string(dat)) 457 } 458 }) 459 } 460 461 func TestAllPlusRuby(t *testing.T) { 462 RunInTmpRepo(func() { 463 CommitFile("./foo_bar/baz.rb", "module FooBar::Baz; foo_bar baz # foo_bar/baz; end") 464 _, err := RunGitGsub("--all", "--ruby", "--rename", "FooBar::Baz", "QuxQuux::Quuz") 465 if err != nil { 466 t.Errorf("Command failed: %s", err) 467 } 468 469 dat, _ := ioutil.ReadFile("./qux_quux/quuz.rb") 470 if string(dat) != "module QuxQuux::Quuz; foo_bar baz # qux_quux/quuz; end" { 471 t.Errorf("Failed: %s", string(dat)) 472 } 473 }) 474 } 475 476 func TestRenameWithPath(t *testing.T) { 477 RunInTmpRepo(func() { 478 CommitFile("foo/git.rb", "puts 'Git'") 479 CommitFile("bar/git.rb", "puts 'Git'") 480 _, err := RunGitGsub("git", "svn", "bar", "--rename") 481 if err != nil { 482 t.Errorf("Command failed: %s", err) 483 } 484 485 dat, _ := ioutil.ReadFile("./foo/git.rb") 486 if string(dat) != "puts 'Git'" { 487 t.Errorf("Failed: %s", string(dat)) 488 } 489 490 dat, _ = ioutil.ReadFile("./bar/svn.rb") 491 if string(dat) != "puts 'Git'" { 492 t.Errorf("Failed: %s", string(dat)) 493 } 494 }) 495 } 496 497 func TestRenameWithSubmatch(t *testing.T) { 498 RunInTmpRepo(func() { 499 CommitFile("git/lib.rb", "puts 'Git'") 500 CommitFile("svn/lib.rb", "puts 'Git'") 501 CommitFile("bzr/lib.rb", "puts 'Git'") 502 _, err := RunGitGsub("--rename", "(git|svn|bzr)/lib", "lib/$1") 503 if err != nil { 504 t.Errorf("Command failed: %s", err) 505 } 506 507 for _, path := range []string{"git", "svn", "bzr"} { 508 files, _ := ioutil.ReadDir(path) 509 if len(files) != 0 { 510 t.Errorf("Failed: %d", len(files)) 511 } 512 } 513 514 files, _ := ioutil.ReadDir("./lib") 515 if len(files) != 3 { 516 t.Errorf("Failed: %d", len(files)) 517 } 518 519 for _, path := range []string{"lib/git.rb", "lib/svn.rb", "lib/bzr.rb"} { 520 dat, _ := ioutil.ReadFile(path) 521 if string(dat) != "puts 'Git'" { 522 t.Errorf("Failed: %s", string(dat)) 523 } 524 } 525 }) 526 } 527 528 func TestRenameWithSpaceInPath(t *testing.T) { 529 RunInTmpRepo(func() { 530 CommitFile("git/l b.rb", "puts 'Git'") 531 _, err := RunGitGsub("--rename", "l b.rb", "lib.rb") 532 if err != nil { 533 t.Errorf("Command failed: %s", err) 534 } 535 536 _, err = ioutil.ReadFile("git/lib.rb") 537 if err != nil { 538 t.Errorf("Failed") 539 } 540 }) 541 }