55 "io/ioutil"
66 "log"
77 "os"
8+ "strings"
89 "sync"
910
1011 "github.com/SimonBaeumer/commander/pkg/output"
@@ -13,32 +14,32 @@ import (
1314)
1415
1516// TestCommand executes the test argument
16- // file is the path to the configuration file
17- // title ist the title of test which should be executed, if empty it will execute all tests
17+ // testPath is the path to the configuration object, an object can be a dir or file
18+ // titleFilterTitle is the title of test which should be executed, if empty it will execute all tests
1819// ctx holds the command flags
19- func TestCommand (input string , title string , ctx AddCommandContext ) error {
20+ // when --dir is enabled testFilterPath must be a zero value
21+ func TestCommand (testPath string , testFilterTitle string , ctx AddCommandContext ) error {
2022 if ctx .Verbose == true {
2123 log .SetOutput (os .Stdout )
2224 }
2325
24- if input == "" {
25- input = CommanderFile
26- }
27- inputType , err := os .Stat (input )
28- if err != nil {
29- return fmt .Errorf ("Error " + err .Error ())
26+ if testPath == "" {
27+ testPath = CommanderFile
3028 }
3129
3230 var results <- chan runtime.TestResult
33- if inputType .IsDir () {
34- fmt .Println ("Starting test against directory: " + input + "..." )
31+ var err error
32+ if ctx .Dir {
33+ if testFilterTitle != "" {
34+ return fmt .Errorf ("Test may not be filtered when --dir is enabled" )
35+ }
36+ fmt .Println ("Starting test against directory: " + testPath + "..." )
3537 fmt .Println ("" )
36- // since no handling of file errors occur should we not allow title
37- results , err = testDir (input , title )
38+ results , err = testDir (testPath , testFilterTitle )
3839 } else {
39- fmt .Println ("Starting test file " + input + "..." )
40+ fmt .Println ("Starting test file " + testPath + "..." )
4041 fmt .Println ("" )
41- results , err = testFile (input , title )
42+ results , err = testFile (testPath , testFilterTitle )
4243 }
4344
4445 if err != nil {
@@ -56,7 +57,7 @@ func TestCommand(input string, title string, ctx AddCommandContext) error {
5657func testDir (directory string , title string ) (<- chan runtime.TestResult , error ) {
5758 files , err := ioutil .ReadDir (directory )
5859 if err != nil {
59- return nil , fmt .Errorf ("Error " + err .Error ())
60+ return nil , fmt .Errorf (err .Error ())
6061 }
6162
6263 results := make (chan runtime.TestResult )
@@ -94,6 +95,9 @@ func testDir(directory string, title string) (<-chan runtime.TestResult, error)
9495func testFile (input string , title string ) (<- chan runtime.TestResult , error ) {
9596 content , err := ioutil .ReadFile (input )
9697 if err != nil {
98+ if strings .Contains (err .Error (), "is a directory" ) {
99+ return nil , fmt .Errorf ("Error %s: is a directory\n Use --dir to test directories with multiple test files" , input )
100+ }
97101 return nil , fmt .Errorf ("Error " + err .Error ())
98102 }
99103
0 commit comments