github.com/connorvict/air@v0.0.0-20231005162537-279bf07db0d5/runner/config_test.go (about) 1 package runner 2 3 import ( 4 "flag" 5 "os" 6 "runtime" 7 "strings" 8 "testing" 9 ) 10 11 const ( 12 bin = `./tmp/main` 13 cmd = "go build -o ./tmp/main ." 14 ) 15 16 func getWindowsConfig() Config { 17 build := cfgBuild{ 18 PreCmd: []string{"echo Hello Air"}, 19 Cmd: "go build -o ./tmp/main .", 20 Bin: "./tmp/main", 21 Log: "build-errors.log", 22 IncludeExt: []string{"go", "tpl", "tmpl", "html"}, 23 ExcludeDir: []string{"assets", "tmp", "vendor", "testdata"}, 24 ExcludeRegex: []string{"_test.go"}, 25 Delay: 1000, 26 StopOnError: true, 27 } 28 if runtime.GOOS == "windows" { 29 build.Bin = bin 30 build.Cmd = cmd 31 } 32 33 return Config{ 34 Root: ".", 35 TmpDir: "tmp", 36 TestDataDir: "testdata", 37 Build: build, 38 } 39 } 40 41 func TestBinCmdPath(t *testing.T) { 42 43 var err error 44 45 c := getWindowsConfig() 46 err = c.preprocess() 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 if runtime.GOOS == "windows" { 52 53 if !strings.HasSuffix(c.Build.Bin, "exe") { 54 t.Fail() 55 } 56 57 if !strings.Contains(c.Build.Bin, "exe") { 58 t.Fail() 59 } 60 } else { 61 62 if strings.HasSuffix(c.Build.Bin, "exe") { 63 t.Fail() 64 } 65 66 if strings.Contains(c.Build.Bin, "exe") { 67 t.Fail() 68 } 69 } 70 } 71 72 func TestDefaultPathConfig(t *testing.T) { 73 tests := []struct { 74 name string 75 path string 76 root string 77 }{{ 78 name: "Invalid Path", 79 path: "invalid/path", 80 root: ".", 81 }, { 82 name: "TOML", 83 path: "_testdata/toml", 84 root: "toml_root", 85 }, { 86 name: "Conf", 87 path: "_testdata/conf", 88 root: "conf_root", 89 }, { 90 name: "Both", 91 path: "_testdata/both", 92 root: "both_root", 93 }} 94 95 for _, tt := range tests { 96 tt := tt 97 t.Run(tt.name, func(t *testing.T) { 98 t.Setenv(airWd, tt.path) 99 c, err := defaultPathConfig() 100 if err != nil { 101 t.Fatalf("Should not be fail: %s.", err) 102 } 103 104 if got, want := c.Root, tt.root; got != want { 105 t.Fatalf("Root is %s, but want %s.", got, want) 106 } 107 }) 108 } 109 } 110 111 func TestReadConfByName(t *testing.T) { 112 _ = os.Unsetenv(airWd) 113 config, _ := readConfByName(dftTOML) 114 if config != nil { 115 t.Fatalf("expect Config is nil,but get a not nil Config") 116 } 117 } 118 119 func TestConfPreprocess(t *testing.T) { 120 t.Setenv(airWd, "_testdata/toml") 121 df := defaultConfig() 122 err := df.preprocess() 123 if err != nil { 124 t.Fatalf("preprocess error %v", err) 125 } 126 suffix := "/_testdata/toml/tmp/main" 127 binPath := df.Build.Bin 128 if !strings.HasSuffix(binPath, suffix) { 129 t.Fatalf("bin path is %s, but not have suffix %s.", binPath, suffix) 130 } 131 } 132 133 func TestConfigWithRuntimeArgs(t *testing.T) { 134 runtimeArg := "-flag=value" 135 136 // inject runtime arg 137 oldArgs := os.Args 138 defer func() { 139 os.Args = oldArgs 140 flag.Parse() 141 }() 142 os.Args = []string{"air", "--", runtimeArg} 143 flag.Parse() 144 145 t.Run("when using bin", func(t *testing.T) { 146 df := defaultConfig() 147 df.preprocess() 148 149 if !contains(df.Build.ArgsBin, runtimeArg) { 150 t.Fatalf("missing expected runtime arg: %s", runtimeArg) 151 } 152 }) 153 154 t.Run("when using full_bin", func(t *testing.T) { 155 df := defaultConfig() 156 df.Build.FullBin = "./tmp/main" 157 df.preprocess() 158 159 if !contains(df.Build.ArgsBin, runtimeArg) { 160 t.Fatalf("missing expected runtime arg: %s", runtimeArg) 161 } 162 }) 163 } 164 165 func TestReadConfigWithWrongPath(t *testing.T) { 166 c, err := readConfig("xxxx") 167 if err == nil { 168 t.Fatal("need throw a error") 169 } 170 if c != nil { 171 t.Fatal("expect is nil but got a conf") 172 } 173 } 174 175 func contains(sl []string, target string) bool { 176 for _, c := range sl { 177 if c == target { 178 return true 179 } 180 } 181 return false 182 }