@@ -2,53 +2,110 @@ package app
22
33import (
44 "fmt"
5- "github.com/SimonBaeumer/commander/pkg/output"
6- "github.com/SimonBaeumer/commander/pkg/runtime"
7- "github.com/SimonBaeumer/commander/pkg/suite"
85 "io/ioutil"
96 "log"
107 "os"
8+ "sync"
9+
10+ "github.com/SimonBaeumer/commander/pkg/output"
11+ "github.com/SimonBaeumer/commander/pkg/runtime"
12+ "github.com/SimonBaeumer/commander/pkg/suite"
1113)
1214
1315// TestCommand executes the test argument
1416// file is the path to the configuration file
1517// title ist the title of test which should be executed, if empty it will execute all tests
1618// ctx holds the command flags
17- func TestCommand (file string , title string , ctx AddCommandContext ) error {
19+ func TestCommand (input string , title string , ctx AddCommandContext ) error {
1820 if ctx .Verbose == true {
1921 log .SetOutput (os .Stdout )
2022 }
2123
22- if file == "" {
23- file = CommanderFile
24+ out := output .NewCliOutput (! ctx .NoColor )
25+
26+ if input == "" {
27+ input = CommanderFile
28+ }
29+ inputType , err := os .Stat (input )
30+ if err != nil {
31+ return fmt .Errorf ("Error " + err .Error ())
32+ }
33+
34+ var results <- chan runtime.TestResult
35+ if inputType .IsDir () {
36+ fmt .Println ("Starting test against directory: " + input + "..." )
37+ fmt .Println ("" )
38+
39+ results , err = testDir (input , title )
40+
41+ } else {
42+ fmt .Println ("Starting test file " + input + "..." )
43+ fmt .Println ("" )
44+ results , err = testFile (input , title )
2445 }
2546
26- fmt .Println ("Starting test file " + file + "..." )
27- fmt .Println ("" )
28- content , err := ioutil .ReadFile (file )
2947 if err != nil {
3048 return fmt .Errorf ("Error " + err .Error ())
3149 }
3250
51+ if ! out .Start (results ) {
52+ return fmt .Errorf ("Test suite failed, use --verbose for more detailed output" )
53+ }
54+
55+ return nil
56+ }
57+
58+ func testDir (directory string , title string ) (<- chan runtime.TestResult , error ) {
59+ files , err := ioutil .ReadDir (directory )
60+ if err != nil {
61+ return nil , fmt .Errorf ("Error " + err .Error ())
62+ }
63+ results := make (chan runtime.TestResult )
64+
65+ var wg sync.WaitGroup
66+ for _ , f := range files {
67+ wg .Add (1 )
68+ go func (f os.FileInfo ) {
69+ defer wg .Done ()
70+ fileResults , err := testFile (directory + "/" + f .Name (), title )
71+ if err != nil {
72+ results <- runtime.TestResult {FileError : err }
73+ return
74+ }
75+ for bb := range fileResults {
76+ results <- bb //fan in results
77+ }
78+ }(f )
79+ }
80+
81+ go func (ch chan runtime.TestResult ) {
82+ wg .Wait ()
83+ close (results )
84+ }(results )
85+
86+ return results , nil
87+ }
88+
89+ func testFile (input string , title string ) (<- chan runtime.TestResult , error ) {
90+ content , err := ioutil .ReadFile (input )
91+ if err != nil {
92+ return nil , fmt .Errorf ("Error " + err .Error ())
93+ }
94+
3395 var s suite.Suite
3496 s = suite .ParseYAML (content )
3597 tests := s .GetTests ()
3698 // Filter tests if test title was given
3799 if title != "" {
38100 test , err := s .GetTestByTitle (title )
39101 if err != nil {
40- return err
102+ return nil , fmt . Errorf ( "Error " + err . Error ())
41103 }
42104 tests = []runtime.TestCase {test }
43105 }
44106
45107 r := runtime .NewRuntime (s .Nodes ... )
46-
47108 results := r .Start (tests )
48- out := output .NewCliOutput (! ctx .NoColor )
49- if ! out .Start (results ) {
50- return fmt .Errorf ("Test suite failed, use --verbose for more detailed output" )
51- }
52109
53- return nil
110+ return results , nil
54111}
0 commit comments