Skip to content

Commit

Permalink
Merge pull request #158 from commander-cli/add-workdir-cmdline-arg
Browse files Browse the repository at this point in the history
Add workdir cmdline arg
  • Loading branch information
dylanhitt authored Dec 3, 2020
2 parents 93ed14f + bda6b73 commit 4675a95
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v2.5.0

- Add `--workdir` flag to change the wokring directory of commander `test` execution

# v2.4.0

- Add ability to test suite from a url
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ $ ./commander test https://your-url/commander_test.yaml

# Execute suites within a test directory
$ ./commander test --dir /tmp

# Execute suites in a different working directory
$ ./commander test --workdir /examples minimal_test.yaml
```

### Adding tests
Expand Down
4 changes: 4 additions & 0 deletions cmd/commander/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ commander test commander.yaml --filter="^filter1$"
Name: "dir",
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
},
cli.StringFlag{
Name: "workdir",
Usage: "Set the working directory of commander's execution",
},
cli.StringSliceFlag{
Name: "filter",
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,
Expand Down
7 changes: 7 additions & 0 deletions commander_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@ tests:
stdout:
contains:
- ✓ [local] hello world
exit-code: 0

test workdir flag:
command: ./commander test --workdir integration/unix/ workdir.yaml
stdout:
contains:
- ✓ [local] echo hello
exit-code: 0
2 changes: 1 addition & 1 deletion examples/commander.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ tests:
it should match file output:
command: printf "line one\nline two"
stdout:
file: ../../examples/_fixtures/output.txt
file: _fixtures/output.txt
1 change: 1 addition & 0 deletions integration/unix/_fixtures/workdir.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
4 changes: 4 additions & 0 deletions integration/unix/workdir.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests:
echo hello:
stdout:
file: _fixtures/workdir.txt
2 changes: 2 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TestCommandContext struct {
Verbose bool
NoColor bool
Dir bool
Workdir string
Concurrent int
Filters []string
}
Expand All @@ -26,6 +27,7 @@ func NewTestContextFromCli(c *cli.Context) TestCommandContext {
Verbose: c.Bool("verbose"),
NoColor: c.Bool("no-color"),
Dir: c.Bool("dir"),
Workdir: c.String("workdir"),
Concurrent: c.Int("concurrent"),
Filters: c.StringSlice("filter"),
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/app/test_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ var out output.OutputWriter
// ctx holds the command flags. If directory scanning is enabled with --dir,
// test filtering is not supported
func TestCommand(testPath string, ctx TestCommandContext) error {
if ctx.Workdir != "" {
err := os.Chdir(ctx.Workdir)
if err != nil {
return fmt.Errorf(err.Error())
}
}

if ctx.Verbose {
log.SetOutput(os.Stdout)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/app/test_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ func Test_ConvergeResults(t *testing.T) {
assert.Equal(t, 1, actual.Failed)
}

func Test_TestCommand_InvalidDir(t *testing.T) {
err := TestCommand("foo", TestCommandContext{Workdir: "invalid_dir"})
if runtime.GOOS == "windows" {
assert.Contains(t, err.Error(), "The system cannot find the file specified")
} else {
assert.Contains(t, err.Error(), "no such file or directory")
}
}

func captureOutput(f func()) string {
reader, writer, err := os.Pipe()
if err != nil {
Expand Down

0 comments on commit 4675a95

Please sign in to comment.