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