-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: move shared logic from execute{Local,SGE}Job to helper
executeLocalJob and executeSGEJob have nearly identical code for running a command and handling the errors. While the code is not too complicated, it's worth consolidating because 1) it ensures that all runners (local, SGE, and upcoming Slurm) behave in the same way (e.g., in terms of reporting errors and writing output files) and 2) the logic is going to get a bit more involved with the next commit.
- Loading branch information
Showing
4 changed files
with
141 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/metrumresearchgroup/turnstile" | ||
"github.com/metrumresearchgroup/wrapt" | ||
) | ||
|
||
func skipIfNoSh(t *wrapt.T) { | ||
t.Helper() | ||
_, err := exec.LookPath("sh") | ||
if err != nil { | ||
t.Skipf("skipped because sh is not available") | ||
} | ||
} | ||
|
||
func NewTestModel(t *wrapt.T) *NonMemModel { | ||
t.Helper() | ||
|
||
return &NonMemModel{Model: "foo", FileName: "foo.ctl", OutputDir: t.TempDir()} | ||
} | ||
|
||
func never(_ error, _ string) bool { | ||
return false | ||
} | ||
|
||
func TestRunModelCommand(tt *testing.T) { | ||
t := wrapt.WrapT(tt) | ||
|
||
skipIfNoSh(t) | ||
|
||
cmd := exec.Command("sh", "-c", "printf 'stdout\n'; printf >&2 'stderr\n'") | ||
mod := NewTestModel(t) | ||
cerr := runModelCommand(mod, cmd, never) | ||
t.A.Equal(cerr, turnstile.ConcurrentError{}) | ||
|
||
outfile := filepath.Join(mod.OutputDir, mod.Model+".out") | ||
t.R.FileExists(outfile) | ||
bs, err := os.ReadFile(outfile) | ||
t.R.NoError(err) | ||
output = string(bs) | ||
|
||
t.A.Contains(output, "stdout") | ||
t.A.Contains(output, "stderr") | ||
} | ||
|
||
func TestRunModelCommandError(tt *testing.T) { | ||
t := wrapt.WrapT(tt) | ||
|
||
skipIfNoSh(t) | ||
|
||
cmd := exec.Command("sh", "-c", "printf 'stdout\n'; printf >&2 'stderr\n'; exit 1") | ||
mod := NewTestModel(t) | ||
|
||
cerr := runModelCommand(mod, cmd, never) | ||
t.A.Error(cerr.Error) | ||
} | ||
|
||
func TestRunModelCommandIgnoreError(tt *testing.T) { | ||
t := wrapt.WrapT(tt) | ||
|
||
skipIfNoSh(t) | ||
|
||
cmd := exec.Command("sh", "-c", "printf 'stdout\n'; printf >&2 'IGNORE\n'; exit 1") | ||
mod := NewTestModel(t) | ||
|
||
ign := func(_ error, output string) bool { | ||
return strings.Contains(output, "IGNORE") | ||
} | ||
|
||
cerr := runModelCommand(mod, cmd, ign) | ||
t.A.Equal(cerr, turnstile.ConcurrentError{}) | ||
|
||
outfile := filepath.Join(mod.OutputDir, mod.Model+".out") | ||
t.R.FileExists(outfile) | ||
bs, err := os.ReadFile(outfile) | ||
t.R.NoError(err) | ||
output = string(bs) | ||
|
||
t.A.Contains(output, "stdout") | ||
t.A.Contains(output, "IGNORE") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters