@@ -2,6 +2,7 @@ package e2e
2
2
3
3
import (
4
4
"context"
5
+ "os"
5
6
"os/exec"
6
7
"testing"
7
8
@@ -17,13 +18,8 @@ func RunTF(args ...string) (string, error) {
17
18
return string (resp ), err
18
19
}
19
20
20
- func RunClu2Adv (args ... string ) (string , error ) {
21
- args = append ([]string {"clu2adv" }, args ... )
22
- return RunTF (args ... )
23
- }
24
-
25
- func RunAdv2New (args ... string ) (string , error ) {
26
- args = append ([]string {"adv2new" }, args ... )
21
+ func RunTFCommand (command string , args ... string ) (string , error ) {
22
+ args = append ([]string {command }, args ... )
27
23
return RunTF (args ... )
28
24
}
29
25
@@ -35,3 +31,98 @@ func CompareFiles(t *testing.T, fs afero.Fs, file1, file2 string) {
35
31
require .NoError (t , err2 )
36
32
assert .Equal (t , string (data1 ), string (data2 ))
37
33
}
34
+
35
+ type TestFiles struct {
36
+ Fs afero.Fs
37
+ Prefix string
38
+ FileIn string
39
+ FileOut string
40
+ FileExpected string
41
+ FileUnexisting string
42
+ CmdName string
43
+ }
44
+
45
+ // GetTestFiles creates a TestFiles struct with standard file paths for the given command name
46
+ func GetTestFiles (t * testing.T , cmdName string ) * TestFiles {
47
+ t .Helper ()
48
+ cwd , err := os .Getwd ()
49
+ require .NoError (t , err )
50
+
51
+ prefix := cwd + "/testdata/"
52
+ files := & TestFiles {
53
+ Fs : afero .NewOsFs (),
54
+ CmdName : cmdName ,
55
+ Prefix : prefix ,
56
+ }
57
+ files .FileIn = files .GetCustomFilePath ("in.tf" )
58
+ files .FileOut = files .GetCustomFilePath ("out.tf" )
59
+ files .FileExpected = files .GetCustomFilePath ("expected.tf" )
60
+ files .FileUnexisting = files .GetCustomFilePath ("unexisting.tf" )
61
+ return files
62
+ }
63
+
64
+ // GetCustomFilePath returns a file path using the testdata prefix
65
+ func (tf * TestFiles ) GetCustomFilePath (suffix string ) string {
66
+ return tf .Prefix + tf .CmdName + "." + suffix
67
+ }
68
+
69
+ type TestCase struct {
70
+ ExpectedErrContains string
71
+ Assert func (t * testing.T )
72
+ Args []string
73
+ }
74
+
75
+ // RunTests runs common parameter validation tests for both commands. Specific tests can be provided in extraTests.
76
+ func RunTests (t * testing.T , cmdName string , extraTests map [string ]TestCase ) {
77
+ t .Helper ()
78
+ files := GetTestFiles (t , cmdName )
79
+ commonTests := map [string ]TestCase {
80
+ "no params" : {
81
+ ExpectedErrContains : "required flag(s) \" file\" , \" output\" not set" ,
82
+ },
83
+ "no input file" : {
84
+ Args : []string {"--output" , files .FileOut },
85
+ ExpectedErrContains : "required flag(s) \" file\" not set" ,
86
+ },
87
+ "no output file" : {
88
+ Args : []string {"--file" , files .FileIn },
89
+ ExpectedErrContains : "required flag(s) \" output\" not set" ,
90
+ },
91
+ "unexisting input file" : {
92
+ Args : []string {"--file" , files .FileUnexisting , "--output" , files .FileOut },
93
+ ExpectedErrContains : "file must exist: " + files .FileUnexisting ,
94
+ },
95
+ "existing output file without replaceOutput flag" : {
96
+ Args : []string {"--file" , files .FileIn , "--output" , files .FileExpected },
97
+ ExpectedErrContains : "file must not exist: " + files .FileExpected ,
98
+ },
99
+ "basic use" : {
100
+ Args : []string {"--file" , files .FileIn , "--output" , files .FileOut },
101
+ Assert : func (t * testing.T ) { t .Helper (); CompareFiles (t , files .Fs , files .FileOut , files .FileExpected ) },
102
+ },
103
+ }
104
+
105
+ allTests := make (map [string ]TestCase )
106
+ for name , test := range commonTests {
107
+ allTests [name ] = test
108
+ }
109
+ for name , test := range extraTests {
110
+ allTests [name ] = test
111
+ }
112
+
113
+ for name , tc := range allTests {
114
+ t .Run (name , func (t * testing.T ) {
115
+ resp , err := RunTFCommand (cmdName , tc .Args ... )
116
+ assert .Equal (t , tc .ExpectedErrContains == "" , err == nil )
117
+ if err == nil {
118
+ assert .Empty (t , resp )
119
+ if tc .Assert != nil {
120
+ tc .Assert (t )
121
+ }
122
+ } else {
123
+ assert .Contains (t , resp , tc .ExpectedErrContains )
124
+ }
125
+ _ = files .Fs .Remove (files .FileOut ) // Ensure output file does not exist in case it was generated in some test case
126
+ })
127
+ }
128
+ }
0 commit comments