github.com/ilidemi/air@v1.42.3/runner/flag_test.go (about) 1 package runner 2 3 import ( 4 "flag" 5 "log" 6 "os" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestFlag(t *testing.T) { 14 // table driven tests 15 type testCase struct { 16 name string 17 args []string 18 expected string 19 key string 20 } 21 testCases := []testCase{ 22 { 23 name: "test1", 24 args: []string{"--build.cmd", "go build -o ./tmp/main ."}, 25 expected: "go build -o ./tmp/main .", 26 key: "build.cmd", 27 }, 28 { 29 name: "tmp dir test", 30 args: []string{"--tmp_dir", "test"}, 31 expected: "test", 32 key: "tmp_dir", 33 }, 34 { 35 name: "check bool", 36 args: []string{"--build.exclude_unchanged", "true"}, 37 expected: "true", 38 key: "build.exclude_unchanged", 39 }, 40 { 41 name: "check int", 42 args: []string{"--build.kill_delay", "1000"}, 43 expected: "1000", 44 key: "build.kill_delay", 45 }, 46 { 47 name: "check exclude_regex", 48 args: []string{"--build.exclude_regex", `["_test.go"]`}, 49 expected: `["_test.go"]`, 50 key: "build.exclude_regex", 51 }, 52 } 53 for _, tc := range testCases { 54 t.Run(tc.name, func(t *testing.T) { 55 flag := flag.NewFlagSet(t.Name(), flag.ExitOnError) 56 cmdArgs := ParseConfigFlag(flag) 57 flag.Parse(tc.args) 58 assert.Equal(t, tc.expected, *cmdArgs[tc.key].Value) 59 }) 60 } 61 } 62 63 func TestConfigRuntimeArgs(t *testing.T) { 64 // table driven tests 65 type testCase struct { 66 name string 67 args []string 68 key string 69 check func(t *testing.T, conf *Config) 70 } 71 testCases := []testCase{ 72 { 73 name: "test1", 74 args: []string{"--build.cmd", "go build -o ./tmp/main ."}, 75 key: "build.cmd", 76 check: func(t *testing.T, conf *Config) { 77 assert.Equal(t, "go build -o ./tmp/main .", conf.Build.Cmd) 78 }, 79 }, 80 { 81 name: "tmp dir test", 82 args: []string{"--tmp_dir", "test"}, 83 key: "tmp_dir", 84 check: func(t *testing.T, conf *Config) { 85 assert.Equal(t, "test", conf.TmpDir) 86 }, 87 }, 88 { 89 name: "check int64", 90 args: []string{"--build.kill_delay", "1000"}, 91 key: "build.kill_delay", 92 check: func(t *testing.T, conf *Config) { 93 assert.Equal(t, time.Duration(1000), conf.Build.KillDelay) 94 }, 95 }, 96 { 97 name: "check bool", 98 args: []string{"--build.exclude_unchanged", "true"}, 99 key: "build.exclude_unchanged", 100 check: func(t *testing.T, conf *Config) { 101 assert.Equal(t, true, conf.Build.ExcludeUnchanged) 102 }, 103 }, 104 { 105 name: "check exclude_regex", 106 args: []string{"--build.exclude_regex", "_test.go,.html"}, 107 check: func(t *testing.T, conf *Config) { 108 assert.Equal(t, []string{"_test.go", ".html"}, conf.Build.ExcludeRegex) 109 }, 110 }, 111 { 112 name: "check exclude_regex with empty string", 113 args: []string{"--build.exclude_regex", ""}, 114 check: func(t *testing.T, conf *Config) { 115 assert.Equal(t, []string{}, conf.Build.ExcludeRegex) 116 t.Logf("%+v", conf.Build.ExcludeDir) 117 assert.NotEqual(t, []string{}, conf.Build.ExcludeDir) 118 }, 119 }, 120 } 121 for _, tc := range testCases { 122 t.Run(tc.name, func(t *testing.T) { 123 dir := t.TempDir() 124 os.Chdir(dir) 125 flag := flag.NewFlagSet(t.Name(), flag.ExitOnError) 126 cmdArgs := ParseConfigFlag(flag) 127 flag.Parse(tc.args) 128 cfg, err := InitConfig("") 129 if err != nil { 130 log.Fatal(err) 131 return 132 } 133 cfg.WithArgs(cmdArgs) 134 tc.check(t, cfg) 135 }) 136 } 137 }