github.com/fairyhunter13/air@v1.40.5/runner/config_test.go (about) 1 package runner 2 3 import ( 4 "os" 5 "runtime" 6 "strings" 7 "testing" 8 ) 9 10 const ( 11 bin = `./tmp/main` 12 cmd = "go build -o ./tmp/main ." 13 ) 14 15 func getWindowsConfig() Config { 16 build := cfgBuild{ 17 Cmd: "go build -o ./tmp/main .", 18 Bin: "./tmp/main", 19 Log: "build-errors.log", 20 IncludeExt: []string{"go", "tpl", "tmpl", "html"}, 21 ExcludeDir: []string{"assets", "tmp", "vendor", "testdata"}, 22 ExcludeRegex: []string{"_test.go"}, 23 Delay: 1000, 24 StopOnError: true, 25 } 26 if runtime.GOOS == "windows" { 27 build.Bin = bin 28 build.Cmd = cmd 29 } 30 31 return Config{ 32 Root: ".", 33 TmpDir: "tmp", 34 TestDataDir: "testdata", 35 Build: build, 36 } 37 } 38 39 func TestBinCmdPath(t *testing.T) { 40 41 var err error 42 43 c := getWindowsConfig() 44 err = c.preprocess() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if runtime.GOOS == "windows" { 50 51 if !strings.HasSuffix(c.Build.Bin, "exe") { 52 t.Fail() 53 } 54 55 if !strings.Contains(c.Build.Bin, "exe") { 56 t.Fail() 57 } 58 } else { 59 60 if strings.HasSuffix(c.Build.Bin, "exe") { 61 t.Fail() 62 } 63 64 if strings.Contains(c.Build.Bin, "exe") { 65 t.Fail() 66 } 67 } 68 } 69 70 func TestDefaultPathConfig(t *testing.T) { 71 tests := []struct { 72 name string 73 path string 74 root string 75 }{{ 76 name: "Invalid Path", 77 path: "invalid/path", 78 root: ".", 79 }, { 80 name: "TOML", 81 path: "_testdata/toml", 82 root: "toml_root", 83 }, { 84 name: "Conf", 85 path: "_testdata/conf", 86 root: "conf_root", 87 }, { 88 name: "Both", 89 path: "_testdata/both", 90 root: "both_root", 91 }} 92 93 for _, tt := range tests { 94 tt := tt 95 t.Run(tt.name, func(t *testing.T) { 96 _ = os.Setenv(airWd, tt.path) 97 c, err := defaultPathConfig() 98 if err != nil { 99 t.Fatalf("Should not be fail: %s.", err) 100 } 101 102 if got, want := c.Root, tt.root; got != want { 103 t.Fatalf("Root is %s, but want %s.", got, want) 104 } 105 }) 106 } 107 } 108 109 func TestReadConfByName(t *testing.T) { 110 _ = os.Unsetenv(airWd) 111 config, _ := readConfByName(dftTOML) 112 if config != nil { 113 t.Fatalf("expect Config is nil,but get a not nil Config") 114 } 115 } 116 117 func TestConfPreprocess(t *testing.T) { 118 _ = os.Setenv(airWd, "_testdata/toml") 119 df := defaultConfig() 120 err := df.preprocess() 121 if err != nil { 122 t.Fatalf("preprocess error %v", err) 123 } 124 suffix := "/_testdata/toml/tmp/main" 125 binPath := df.Build.Bin 126 if !strings.HasSuffix(binPath, suffix) { 127 t.Fatalf("bin path is %s, but not have suffix %s.", binPath, suffix) 128 } 129 } 130 131 func TestReadConfigWithWrongPath(t *testing.T) { 132 c, err := readConfig("xxxx") 133 if err == nil { 134 t.Fatal("need throw a error") 135 } 136 if c != nil { 137 t.Fatal("expect is nil but got a conf") 138 } 139 }