Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update ExcludeRegex to empty not work #308

Merged
merged 14 commits into from
Jul 11, 2022
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
go-version: ^1.17
- name: build
run: make build
- name: install dependency
run: if [ $(uname) == "Darwin" ]; then brew install gnu-sed ;fi
- name: run Unit tests.
run: go install github.com/go-delve/delve/cmd/dlv@latest && go test ./... -v -covermode=count -coverprofile=coverage.txt
- name: upload Coverage report to CodeCov
Expand Down
31 changes: 28 additions & 3 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ type cfgScreen struct {
ClearOnRebuild bool `toml:"clear_on_rebuild"`
}

type sliceTransformer struct {
}

func (t sliceTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ.Kind() == reflect.Slice {
return func(dst, src reflect.Value) error {
if !src.IsZero() {
dst.Set(src)
}
return nil
}
}
return nil
}

// InitConfig initializes the configuration.
func InitConfig(path string) (cfg *Config, err error) {
if path == "" {
Expand All @@ -100,12 +115,22 @@ func InitConfig(path string) (cfg *Config, err error) {
return nil, err
}
}
err = mergo.Merge(cfg, defaultConfig())
config := defaultConfig()
// get addr
ret := &config
err = mergo.Merge(ret, cfg, func(config *mergo.Config) {
// mergo.Merge will overwrite the fields if it is Empty
// So need use this to avoid that none-zero slice will be overwritten.
// https://github.com/imdario/mergo#transformers
config.Transformers = sliceTransformer{}
config.Overwrite = true
})
if err != nil {
return nil, err
}
err = cfg.preprocess()
return cfg, err

err = ret.preprocess()
return ret, err
}

func writeDefaultConfig() {
Expand Down
4 changes: 2 additions & 2 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestDefaultPathConfig(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv(airWd, tt.path)
t.Setenv(airWd, tt.path)
c, err := defaultPathConfig()
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
Expand All @@ -115,7 +115,7 @@ func TestReadConfByName(t *testing.T) {
}

func TestConfPreprocess(t *testing.T) {
_ = os.Setenv(airWd, "_testdata/toml")
t.Setenv(airWd, "_testdata/toml")
df := defaultConfig()
err := df.preprocess()
if err != nil {
Expand Down
154 changes: 154 additions & 0 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package runner
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -640,3 +643,154 @@ func TestWriteDefaultConfig(t *testing.T) {

assert.Equal(t, expect, *actual)
}

func TestCheckNilSliceShouldBeenOverwrite(t *testing.T) {
port, f := GetPort()
f()
t.Logf("port: %d", port)

tmpDir := initTestEnv(t, port)

// change dir to tmpDir
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}

// write easy config file

config := `
[build]
cmd = "go build ."
bin = "tmp/main"
exclude_regex = []
exclude_dir = ["test"]
exclude_file = ["main.go"]

`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0644); err != nil {
t.Fatal(err)
}
engine, err := NewEngine(".air.toml", true)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []string{"go", "tpl", "tmpl", "html"}, engine.config.Build.IncludeExt)
assert.Equal(t, []string{}, engine.config.Build.ExcludeRegex)
assert.Equal(t, []string{"test"}, engine.config.Build.ExcludeDir)
// add new config
assert.Equal(t, []string{"main.go"}, engine.config.Build.ExcludeFile)
assert.Equal(t, "go build .", engine.config.Build.Cmd)

}

func TestShouldIncludeGoTestFile(t *testing.T) {
port, f := GetPort()
f()
t.Logf("port: %d", port)

tmpDir := initTestEnv(t, port)
// change dir to tmpDir
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
writeDefaultConfig()

// write go test file
file, err := os.Create("main_test.go")
if err != nil {
t.Fatal(err)
}
_, err = file.WriteString(`package main

import "testing"

func Test(t *testing.T) {
t.Log("testing")
}
`)
// run sed
// check the file is exist
if _, err := os.Stat(dftTOML); err != nil {
t.Fatal(err)
}
// check is MacOS
var cmd *exec.Cmd
if runtime.GOOS == "darwin" {
cmd = exec.Command("gsed", "-i", "s/\"_test.*go\"//g", ".air.toml")
} else {
cmd = exec.Command("sed", "-i", "s/\"_test.*go\"//g", ".air.toml")
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
t.Fatal(err)
}

time.Sleep(time.Second * 3)
engine, err := NewEngine(".air.toml", false)
if err != nil {
t.Fatal(err)
}
go func() {
engine.Run()
}()

t.Logf("start change main_test.go")
// change file of main_test.go
// just append a new empty line to main_test.go
if err = waitingPortReady(t, port, time.Second*40); err != nil {
t.Fatal(err)
}
go func() {
file, err := os.OpenFile("main_test.go", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
defer file.Close()
_, err = file.WriteString("\n")
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
}()
// should Have rebuild
if err = waitingPortConnectionRefused(t, port, time.Second*10); err != nil {
t.Fatal(err)
}
}

func TestCreateNewDir(t *testing.T) {
// generate a random port
port, f := GetPort()
f()
t.Logf("port: %d", port)

tmpDir := initTestEnv(t, port)
// change dir to tmpDir
err := os.Chdir(tmpDir)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
engine, err := NewEngine("", true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}

go func() {
engine.Run()
}()
time.Sleep(time.Second * 2)
assert.True(t, checkPortHaveBeenUsed(port))

// create a new dir make dir
if err = os.Mkdir(tmpDir+"/dir", 0644); err != nil {
t.Fatal(err)
}

// no need reload
if err = waitingPortConnectionRefused(t, port, 3*time.Second); err == nil {
t.Fatal("should raise a error")
}
engine.Stop()
time.Sleep(2 * time.Second)

}