Skip to content

Commit

Permalink
refactor: refactor test functions and remove unused variables (#476)
Browse files Browse the repository at this point in the history
* refactor: refactor test functions and remove unused variables

- Remove the `runArgs` variable
- Change the way `flag.CommandLine.Parse` is called
- Modify the `TestBinCmdPath` function
- Modify the `TestConfigWithRuntimeArgs` function
- Modify the `TestCtrlCWhenHaveKillDelay` function
- Modify the `include_file` value in the `TestCtrlCWhenHaveKillDelay` function
- Modify the `include_file` value in the `TestConfigRuntimeArgs` function
- Modify the `TestFlag` function
- Modify the `TestConfigRuntimeArgs` function
- Modify the `Test_killCmd_SendInterrupt_false` function
- Modify the `bytesRead` variable in the `Test_killCmd_SendInterrupt_false` function

Signed-off-by: Bo-Yi Wu <[email protected]>

* fix: testing

Signed-off-by: appleboy <[email protected]>

---------

Signed-off-by: Bo-Yi Wu <[email protected]>
Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy authored Oct 18, 2023
1 parent 149f99f commit bf9ec3c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
debugMode bool
showVersion bool
cmdArgs map[string]runner.TomlInfo
runArgs []string
)

func helpMessage() {
Expand All @@ -42,7 +41,9 @@ func parseFlag(args []string) {
flag.BoolVar(&showVersion, "v", false, "show version")
cmd := flag.CommandLine
cmdArgs = runner.ParseConfigFlag(cmd)
flag.CommandLine.Parse(args)
if err := flag.CommandLine.Parse(args); err != nil {
log.Fatal(err)
}
}

type versionInfo struct {
Expand Down
9 changes: 6 additions & 3 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func getWindowsConfig() Config {
}

func TestBinCmdPath(t *testing.T) {

var err error

c := getWindowsConfig()
Expand Down Expand Up @@ -144,7 +143,9 @@ func TestConfigWithRuntimeArgs(t *testing.T) {

t.Run("when using bin", func(t *testing.T) {
df := defaultConfig()
df.preprocess()
if err := df.preprocess(); err != nil {
t.Fatalf("preprocess error %v", err)
}

if !contains(df.Build.ArgsBin, runtimeArg) {
t.Fatalf("missing expected runtime arg: %s", runtimeArg)
Expand All @@ -154,7 +155,9 @@ func TestConfigWithRuntimeArgs(t *testing.T) {
t.Run("when using full_bin", func(t *testing.T) {
df := defaultConfig()
df.Build.FullBin = "./tmp/main"
df.preprocess()
if err := df.preprocess(); err != nil {
t.Fatalf("preprocess error %v", err)
}

if !contains(df.Build.ArgsBin, runtimeArg) {
t.Fatalf("missing expected runtime arg: %s", runtimeArg)
Expand Down
9 changes: 5 additions & 4 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runner
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"os"
Expand Down Expand Up @@ -387,7 +386,9 @@ func TestCtrlCWhenHaveKillDelay(t *testing.T) {
engine.config.Build.KillDelay = c.Build.KillDelay
engine.config.Build.Delay = 2000
engine.config.Build.SendInterrupt = true
engine.config.preprocess()
if err := engine.config.preprocess(); err != nil {
t.Fatalf("Should not be fail: %s.", err)
}

go func() {
engine.Run()
Expand Down Expand Up @@ -845,7 +846,7 @@ exclude_file = ["main.go"]
include_file = ["test/not_a_test.go"]
`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
if err := os.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
t.Fatal(err)
}
engine, err := NewEngine(".air.toml", true)
Expand Down Expand Up @@ -980,7 +981,7 @@ include_ext = ["sh"]
include_dir = ["nonexist"] # prevent default "." watch from taking effect
include_file = ["main.sh"]
`
if err := ioutil.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
if err := os.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
t.Fatal(err)
}

Expand Down
6 changes: 3 additions & 3 deletions runner/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestFlag(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
flag := flag.NewFlagSet(t.Name(), flag.ExitOnError)
cmdArgs := ParseConfigFlag(flag)
flag.Parse(tc.args)
assert.NoError(t, flag.Parse(tc.args))
assert.Equal(t, tc.expected, *cmdArgs[tc.key].Value)
})
}
Expand Down Expand Up @@ -121,10 +121,10 @@ func TestConfigRuntimeArgs(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
os.Chdir(dir)
assert.NoError(t, os.Chdir(dir))
flag := flag.NewFlagSet(t.Name(), flag.ExitOnError)
cmdArgs := ParseConfigFlag(flag)
flag.Parse(tc.args)
_ = flag.Parse(tc.args)
cfg, err := InitConfig("")
if err != nil {
log.Fatal(err)
Expand Down
10 changes: 6 additions & 4 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runner
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -206,7 +205,9 @@ func Test_killCmd_SendInterrupt_false(t *testing.T) {
pid int
cmd *exec.Cmd
}{pid: pid, cmd: cmd}
cmd.Wait()
if err := cmd.Wait(); err != nil {
t.Errorf("failed to wait command: %v", err)
}
t.Logf("wait finished")
}()
resp := <-startChan
Expand All @@ -220,7 +221,8 @@ func Test_killCmd_SendInterrupt_false(t *testing.T) {
t.Logf("%v was been killed", pid)
// check processes were being killed
// read pids from file
bytesRead, _ := ioutil.ReadFile("pid")
bytesRead, err := os.ReadFile("pid")
assert.NoError(t, err)
lines := strings.Split(string(bytesRead), "\n")
for _, line := range lines {
_, err := strconv.Atoi(line)
Expand Down Expand Up @@ -279,7 +281,7 @@ func TestCheckIncludeFile(t *testing.T) {
e := Engine{
config: &Config{
Build: cfgBuild{
IncludeFile: []string{"main.go"},
IncludeFile: []string{"main.go"},
},
},
}
Expand Down

0 comments on commit bf9ec3c

Please sign in to comment.