Skip to content

Commit 4675a95

Browse files
authored
Merge pull request #158 from commander-cli/add-workdir-cmdline-arg
Add workdir cmdline arg
2 parents 93ed14f + bda6b73 commit 4675a95

10 files changed

Lines changed: 42 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v2.5.0
2+
3+
- Add `--workdir` flag to change the wokring directory of commander `test` execution
4+
15
# v2.4.0
26

37
- Add ability to test suite from a url

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ $ ./commander test https://your-url/commander_test.yaml
213213

214214
# Execute suites within a test directory
215215
$ ./commander test --dir /tmp
216+
217+
# Execute suites in a different working directory
218+
$ ./commander test --workdir /examples minimal_test.yaml
216219
```
217220

218221
### Adding tests

cmd/commander/commander.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ commander test commander.yaml --filter="^filter1$"
8787
Name: "dir",
8888
Usage: "Execute all test files in a directory sorted by file name, this is not recursive - e.g. /path/to/test_files/",
8989
},
90+
cli.StringFlag{
91+
Name: "workdir",
92+
Usage: "Set the working directory of commander's execution",
93+
},
9094
cli.StringSliceFlag{
9195
Name: "filter",
9296
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,

commander_unix.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,11 @@ tests:
140140
stdout:
141141
contains:
142142
- ✓ [local] hello world
143+
exit-code: 0
144+
145+
test workdir flag:
146+
command: ./commander test --workdir integration/unix/ workdir.yaml
147+
stdout:
148+
contains:
149+
- ✓ [local] echo hello
143150
exit-code: 0

examples/commander.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ tests:
2929
it should match file output:
3030
command: printf "line one\nline two"
3131
stdout:
32-
file: ../../examples/_fixtures/output.txt
32+
file: _fixtures/output.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

integration/unix/workdir.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
echo hello:
3+
stdout:
4+
file: _fixtures/workdir.txt

pkg/app/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type TestCommandContext struct {
1616
Verbose bool
1717
NoColor bool
1818
Dir bool
19+
Workdir string
1920
Concurrent int
2021
Filters []string
2122
}
@@ -26,6 +27,7 @@ func NewTestContextFromCli(c *cli.Context) TestCommandContext {
2627
Verbose: c.Bool("verbose"),
2728
NoColor: c.Bool("no-color"),
2829
Dir: c.Bool("dir"),
30+
Workdir: c.String("workdir"),
2931
Concurrent: c.Int("concurrent"),
3032
Filters: c.StringSlice("filter"),
3133
}

pkg/app/test_command.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ var out output.OutputWriter
2222
// ctx holds the command flags. If directory scanning is enabled with --dir,
2323
// test filtering is not supported
2424
func TestCommand(testPath string, ctx TestCommandContext) error {
25+
if ctx.Workdir != "" {
26+
err := os.Chdir(ctx.Workdir)
27+
if err != nil {
28+
return fmt.Errorf(err.Error())
29+
}
30+
}
31+
2532
if ctx.Verbose {
2633
log.SetOutput(os.Stdout)
2734
}

pkg/app/test_command_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ func Test_ConvergeResults(t *testing.T) {
178178
assert.Equal(t, 1, actual.Failed)
179179
}
180180

181+
func Test_TestCommand_InvalidDir(t *testing.T) {
182+
err := TestCommand("foo", TestCommandContext{Workdir: "invalid_dir"})
183+
if runtime.GOOS == "windows" {
184+
assert.Contains(t, err.Error(), "The system cannot find the file specified")
185+
} else {
186+
assert.Contains(t, err.Error(), "no such file or directory")
187+
}
188+
}
189+
181190
func captureOutput(f func()) string {
182191
reader, writer, err := os.Pipe()
183192
if err != nil {

0 commit comments

Comments
 (0)