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

Pre & Post commands #461

Merged
merged 10 commits into from
Sep 21, 2023
2 changes: 2 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ root = "."
tmp_dir = "tmp"

[build]
# Array of commands to run before each build
pre_cmd = ["echo Hello","echo Air"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate with spaces

# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
Expand Down
1 change: 1 addition & 0 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
}

type cfgBuild struct {
PreCmd []string `toml:"pre_cmd"`
Cmd string `toml:"cmd"`
Bin string `toml:"bin"`
FullBin string `toml:"full_bin"`
Expand Down
1 change: 1 addition & 0 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (

func getWindowsConfig() Config {
build := cfgBuild{
PreCmd: []string{"echo Hello Air"},
Cmd: "go build -o ./tmp/main .",
Bin: "./tmp/main",
Log: "build-errors.log",
Expand Down
38 changes: 33 additions & 5 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ func (e *Engine) buildRun() {
default:
}
var err error
if err = e.runPreCmd(); err != nil {
e.canExit <- true
e.mainLog("failed to execute pre_cmd: %s", err.Error())
if e.config.Build.StopOnError {
return
}
}
if err = e.building(); err != nil {
e.canExit <- true
e.buildLog("failed to build, error: %s", err.Error())
Expand Down Expand Up @@ -410,10 +417,9 @@ func (e *Engine) flushEvents() {
}
}

func (e *Engine) building() error {
var err error
e.buildLog("building...")
cmd, stdout, stderr, err := e.startCmd(e.config.Build.Cmd)
// utility to execute commands, such as cmd & pre_cmd
func (e *Engine) runCommand(command string) error {
cmd, stdout, stderr, err := e.startCmd(command)
if err != nil {
return err
}
Expand All @@ -423,14 +429,36 @@ func (e *Engine) building() error {
}()
_, _ = io.Copy(os.Stdout, stdout)
_, _ = io.Copy(os.Stderr, stderr)
// wait for building
// wait for command to finish
err = cmd.Wait()
if err != nil {
return err
}
return nil
}

// run cmd option in .air.toml
func (e *Engine) building() error {
e.buildLog("building...")
err := e.runCommand(e.config.Build.Cmd)
if err != nil {
return err
}
return nil
}

// run pre_cmd option in .air.toml
func (e *Engine) runPreCmd() error {
for _, command := range e.config.Build.PreCmd {
e.runnerLog("> %s", command)
err := e.runCommand(command)
if err != nil {
return err
}
}
return nil
}

func (e *Engine) runBin() error {
// control killFunc should be kill or not
killCh := make(chan struct{})
Expand Down
22 changes: 22 additions & 0 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ func TestRerunWhenFileChanged(t *testing.T) {
}
}

func TestRunCommand(t *testing.T) {
engine, err := NewEngine("", true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
err = engine.runCommand("echo Hello Air")
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
}

func TestRunPreCmd(t *testing.T) {
engine, err := NewEngine("", true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
err = engine.runPreCmd()
xiantang marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use a real command for integration testing, such as writing a file to the test folder and then checking if the file exists after execution is complete

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the current unit tests can test anything

if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
}

func TestRunBin(t *testing.T) {
engine, err := NewEngine("", true)
if err != nil {
Expand Down