@@ -42,15 +42,15 @@ func Test_validate(t *testing.T) {
4242 name string
4343 args args
4444 wantErr bool
45- before func ()
46- after func ()
45+ before func (t * testing. T )
46+ after func (t * testing. T )
4747 }{
4848 {
4949 name : "Wrong output format" ,
5050 args : args {config : formatter.Config {OutputFormat : formatter .OutputFormat ("test" )}},
5151 wantErr : true ,
52- before : func () {},
53- after : func () {},
52+ before : func (t * testing. T ) {},
53+ after : func (t * testing. T ) {},
5454 },
5555 {
5656 name : "Missing input file" ,
@@ -60,8 +60,8 @@ func Test_validate(t *testing.T) {
6060 },
6161 },
6262 wantErr : true ,
63- before : func () {},
64- after : func () {},
63+ before : func (t * testing. T ) {},
64+ after : func (t * testing. T ) {},
6565 },
6666 {
6767 name : "Successful validation" ,
@@ -72,18 +72,26 @@ func Test_validate(t *testing.T) {
7272 },
7373 },
7474 wantErr : false ,
75- before : func () {
76- os .Create (path .Join (os .TempDir (), "formatter_cmd_valid_2" ))
75+ before : func (t * testing.T ) {
76+ path := path .Join (os .TempDir (), "formatter_cmd_valid_2" )
77+ _ , err := os .Create (path )
78+ if err != nil {
79+ t .Errorf ("could not create temporary file: %s" , path )
80+ }
7781 },
78- after : func () {
79- os .Remove (path .Join (os .TempDir (), "formatter_cmd_valid_2" ))
82+ after : func (t * testing.T ) {
83+ path := path .Join (os .TempDir (), "formatter_cmd_valid_2" )
84+ err := os .Remove (path )
85+ if err != nil {
86+ t .Errorf ("could not remove temporary file: %s" , path )
87+ }
8088 },
8189 },
8290 }
8391 for _ , tt := range tests {
8492 t .Run (tt .name , func (t * testing.T ) {
85- tt .before ()
86- defer tt .after ()
93+ tt .before (t )
94+ defer tt .after (t )
8795 if err := validate (tt .args .config ); (err != nil ) != tt .wantErr {
8896 t .Errorf ("validate() error = %v, wantErr %v" , err , tt .wantErr )
8997 }
@@ -145,14 +153,26 @@ func Test_run(t *testing.T) {
145153 cmd * cobra.Command
146154 args []string
147155 }
148- before := func (file string , testWorkflow formatter.Workflow , testConfig formatter.Config ) {
149- os .Create (file )
156+ before := func (file string , testWorkflow formatter.Workflow , testConfig formatter.Config , t * testing.T ) {
157+ var err error
158+ if len (file ) != 0 {
159+ _ , err = os .Create (file )
160+ }
161+ if err != nil {
162+ t .Errorf ("could not create temporary file: %s" , file )
163+ }
150164 workflow = testWorkflow
151165 config = testConfig
152166 config .InputFile = formatter .InputFile (file )
153167 }
154- after := func (file string ) {
155- os .Remove (file )
168+ after := func (file string , t * testing.T ) {
169+ var err error
170+ if len (file ) != 0 {
171+ err = os .Remove (file )
172+ }
173+ if err != nil {
174+ t .Errorf ("could not remove temporary file: %s" , file )
175+ }
156176 workflow = nil
157177 config = formatter.Config {}
158178 }
@@ -212,8 +232,8 @@ func Test_run(t *testing.T) {
212232 for _ , tt := range tests {
213233 t .Run (tt .name , func (t * testing.T ) {
214234 if tt .runBefore {
215- before (tt .input , tt .workflow , tt .config )
216- defer after (tt .input )
235+ before (tt .input , tt .workflow , tt .config , t )
236+ defer after (tt .input , t )
217237 }
218238 if err := run (tt .args .cmd , tt .args .args ); (err != nil ) != tt .wantErr {
219239 t .Errorf ("run() error = %v, wantErr %v" , err , tt .wantErr )
0 commit comments