github.com/bishtawi/migrate/v4@v4.8.11/internal/cli/commands_test.go (about) 1 package cli 2 3 import ( 4 "testing" 5 ) 6 7 func TestCleanDir(t *testing.T) { 8 cases := []struct { 9 dir string 10 expectedCleanDir string 11 }{ 12 {dir: "", expectedCleanDir: ""}, 13 {dir: ".", expectedCleanDir: ""}, 14 {dir: "/", expectedCleanDir: "/"}, 15 {dir: "./", expectedCleanDir: ""}, 16 {dir: ".test", expectedCleanDir: ".test/"}, 17 {dir: ".test/", expectedCleanDir: ".test/"}, 18 {dir: "test", expectedCleanDir: "test/"}, 19 {dir: "test/", expectedCleanDir: "test/"}, 20 {dir: "./test", expectedCleanDir: "test/"}, 21 {dir: "./test/", expectedCleanDir: "test/"}, 22 {dir: "test/test", expectedCleanDir: "test/test/"}, 23 {dir: "test/test/", expectedCleanDir: "test/test/"}, 24 {dir: "./test/test", expectedCleanDir: "test/test/"}, 25 {dir: "./test/test/", expectedCleanDir: "test/test/"}, 26 } 27 28 for _, c := range cases { 29 t.Run(c.dir, func(t *testing.T) { 30 cleanedDir := cleanDir(c.dir) 31 if cleanedDir != c.expectedCleanDir { 32 t.Error("Incorrectly cleaned dir: " + cleanedDir + " != " + c.expectedCleanDir) 33 } 34 }) 35 } 36 } 37 38 func TestNextSeq(t *testing.T) { 39 cases := []struct { 40 name string 41 matches []string 42 dir string 43 seqDigits int 44 expected string 45 expectedErrStr string 46 }{ 47 {"Bad digits", []string{}, "migrationDir/", 0, "", "Digits must be positive"}, 48 {"Single digit initialize", []string{}, "migrationDir/", 1, "1", ""}, 49 {"Single digit malformed", []string{"bad"}, "migrationDir/", 1, "", "Malformed migration filename: bad"}, 50 {"Single digit no int", []string{"bad_bad"}, "migrationDir/", 1, "", "strconv.Atoi: parsing \"bad\": invalid syntax"}, 51 {"Single digit negative seq", []string{"-5_test"}, "migrationDir/", 1, "", "Next sequence number must be positive"}, 52 {"Single digit increment", []string{"3_test", "4_test"}, "migrationDir/", 1, "5", ""}, 53 {"Single digit overflow", []string{"9_test"}, "migrationDir/", 1, "", "Next sequence number 10 too large. At most 1 digits are allowed"}, 54 {"Zero-pad initialize", []string{}, "migrationDir/", 6, "000001", ""}, 55 {"Zero-pad malformed", []string{"bad"}, "migrationDir/", 6, "", "Malformed migration filename: bad"}, 56 {"Zero-pad no int", []string{"bad_bad"}, "migrationDir/", 6, "", "strconv.Atoi: parsing \"bad\": invalid syntax"}, 57 {"Zero-pad negative seq", []string{"-000005_test"}, "migrationDir/", 6, "", "Next sequence number must be positive"}, 58 {"Zero-pad increment", []string{"000003_test", "000004_test"}, "migrationDir/", 6, "000005", ""}, 59 {"Zero-pad overflow", []string{"999999_test"}, "migrationDir/", 6, "", "Next sequence number 1000000 too large. At most 6 digits are allowed"}, 60 {"dir - no trailing slash", []string{"migrationDir/000001_test"}, "migrationDir", 6, "", `strconv.Atoi: parsing "/000001": invalid syntax`}, 61 {"dir - with dot prefix success", []string{"migrationDir/000001_test"}, "./migrationDir/", 6, "", `strconv.Atoi: parsing "migrationDir/000001": invalid syntax`}, 62 {"dir - no dir prefix", []string{"000001_test"}, "migrationDir/", 6, "000002", ""}, 63 {"dir - strip success", []string{"migrationDir/000001_test"}, "migrationDir/", 6, "000002", ""}, 64 } 65 for _, c := range cases { 66 t.Run(c.name, func(t *testing.T) { 67 nextSeq, err := nextSeq(c.matches, c.dir, c.seqDigits) 68 if nextSeq != c.expected { 69 t.Error("Incorrect nextSeq: " + nextSeq + " != " + c.expected) 70 } 71 if err != nil { 72 if err.Error() != c.expectedErrStr { 73 t.Error("Incorrect error: " + err.Error() + " != " + c.expectedErrStr) 74 } 75 } else if c.expectedErrStr != "" { 76 t.Error("Expected error: " + c.expectedErrStr + " but got nil instead") 77 } 78 }) 79 } 80 } 81 82 func TestNumDownFromArgs(t *testing.T) { 83 cases := []struct { 84 name string 85 args []string 86 applyAll bool 87 expectedNeedConfirm bool 88 expectedNum int 89 expectedErrStr string 90 }{ 91 {"no args", []string{}, false, true, -1, ""}, 92 {"down all", []string{}, true, false, -1, ""}, 93 {"down 5", []string{"5"}, false, false, 5, ""}, 94 {"down N", []string{"N"}, false, false, 0, "can't read limit argument N"}, 95 {"extra arg after -all", []string{"5"}, true, false, 0, "-all cannot be used with other arguments"}, 96 {"extra arg before -all", []string{"5", "-all"}, false, false, 0, "too many arguments"}, 97 } 98 for _, c := range cases { 99 t.Run(c.name, func(t *testing.T) { 100 num, needsConfirm, err := numDownMigrationsFromArgs(c.applyAll, c.args) 101 if needsConfirm != c.expectedNeedConfirm { 102 t.Errorf("Incorrect needsConfirm was: %v wanted %v", needsConfirm, c.expectedNeedConfirm) 103 } 104 105 if num != c.expectedNum { 106 t.Errorf("Incorrect num was: %v wanted %v", num, c.expectedNum) 107 } 108 109 if err != nil { 110 if err.Error() != c.expectedErrStr { 111 t.Error("Incorrect error: " + err.Error() + " != " + c.expectedErrStr) 112 } 113 } else if c.expectedErrStr != "" { 114 t.Error("Expected error: " + c.expectedErrStr + " but got nil instead") 115 } 116 }) 117 } 118 }