Skip to content

Commit

Permalink
refactor: refactor test functions and remove unused variables
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
appleboy committed Oct 17, 2023
1 parent 9d67077 commit 99f07f8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
3 changes: 1 addition & 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,7 @@ func parseFlag(args []string) {
flag.BoolVar(&showVersion, "v", false, "show version")
cmd := flag.CommandLine
cmdArgs = runner.ParseConfigFlag(cmd)
flag.CommandLine.Parse(args)
_ = flag.CommandLine.Parse(args)
}

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 @@ -383,7 +382,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 @@ -841,7 +842,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 @@ -976,7 +977,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)
_ = 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)
_ = 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
9 changes: 5 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,7 @@ 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, _ := os.ReadFile("pid")
lines := strings.Split(string(bytesRead), "\n")
for _, line := range lines {
_, err := strconv.Atoi(line)
Expand Down Expand Up @@ -279,7 +280,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 99f07f8

Please sign in to comment.