@@ -4,13 +4,15 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
- "github.com/JekaMas/go-mutesting/internal/models"
8
7
"io"
9
8
"io/ioutil"
9
+ "math/big"
10
10
"os"
11
11
"testing"
12
12
13
- "github.com/stretchr/testify/assert"
13
+ "github.com/stretchr/testify/require"
14
+
15
+ "github.com/JekaMas/go-mutesting/internal/models"
14
16
)
15
17
16
18
func TestMainSimple (t * testing.T ) {
@@ -64,14 +66,14 @@ func TestMainSkipWithoutTest(t *testing.T) {
64
66
}
65
67
66
68
func TestMainJSONReport (t * testing.T ) {
67
- tmpDir , err := ioutil . TempDir ("" , "go-mutesting-main-test-" )
68
- assert .NoError (t , err )
69
+ tmpDir , err := os . MkdirTemp ("" , "go-mutesting-main-test-" )
70
+ require .NoError (t , err )
69
71
70
72
reportFileName := "reportTestMainJSONReport.json"
71
73
jsonFile := tmpDir + "/" + reportFileName
72
74
if _ , err := os .Stat (jsonFile ); err == nil {
73
75
err = os .Remove (jsonFile )
74
- assert .NoError (t , err )
76
+ require .NoError (t , err )
75
77
}
76
78
77
79
models .ReportFileName = jsonFile
@@ -85,8 +87,8 @@ func TestMainJSONReport(t *testing.T) {
85
87
)
86
88
87
89
info , err := os .Stat (jsonFile )
88
- assert .NoError (t , err )
89
- assert .NotNil (t , info )
90
+ require .NoError (t , err )
91
+ require .NotNil (t , info )
90
92
91
93
defer func () {
92
94
err = os .Remove (jsonFile )
@@ -96,11 +98,11 @@ func TestMainJSONReport(t *testing.T) {
96
98
}()
97
99
98
100
jsonData , err := ioutil .ReadFile (jsonFile )
99
- assert .NoError (t , err )
101
+ require .NoError (t , err )
100
102
101
103
var mutationReport models.Report
102
104
err = json .Unmarshal (jsonData , & mutationReport )
103
- assert .NoError (t , err )
105
+ require .NoError (t , err )
104
106
105
107
expectedStats := models.Stats {
106
108
TotalMutantsCount : 60 ,
@@ -110,60 +112,62 @@ func TestMainJSONReport(t *testing.T) {
110
112
ErrorCount : 0 ,
111
113
SkippedCount : 0 ,
112
114
TimeOutCount : 0 ,
113
- Msi : 0.5833333333333334 ,
115
+ Msi : big . NewFloat ( 0.5833333333333334 ) ,
114
116
MutationCodeCoverage : 0 ,
115
117
CoveredCodeMsi : 0 ,
116
118
DuplicatedCount : 0 ,
117
119
}
118
120
119
- assert .Equal (t , expectedStats , mutationReport .Stats )
120
- assert .Equal (t , 25 , len (mutationReport .Escaped ))
121
- assert .Nil (t , mutationReport .Timeouted )
122
- assert .Equal (t , 35 , len (mutationReport .Killed ))
123
- assert .Nil (t , mutationReport .Errored )
121
+ require .Equal (t , expectedStats , mutationReport .Stats )
122
+ require .Equal (t , 25 , len (mutationReport .Escaped ))
123
+ require .Nil (t , mutationReport .Timeouted )
124
+ require .Equal (t , 35 , len (mutationReport .Killed ))
125
+ require .Nil (t , mutationReport .Errored )
124
126
125
127
for i := 0 ; i < len (mutationReport .Escaped ); i ++ {
126
- assert .Contains (t , mutationReport .Escaped [i ].ProcessOutput , "FAIL" )
128
+ require .Contains (t , mutationReport .Escaped [i ].ProcessOutput , "FAIL" )
127
129
}
128
130
for i := 0 ; i < len (mutationReport .Killed ); i ++ {
129
- assert .Contains (t , mutationReport .Killed [i ].ProcessOutput , "PASS" )
131
+ require .Contains (t , mutationReport .Killed [i ].ProcessOutput , "PASS" )
130
132
}
131
133
}
132
134
133
135
func testMain (t * testing.T , root string , exec []string , expectedExitCode int , contains string ) {
136
+ t .Helper ()
137
+
134
138
saveStderr := os .Stderr
135
139
saveStdout := os .Stdout
136
140
saveCwd , err := os .Getwd ()
137
- assert .Nil (t , err )
141
+ require .Nil (t , err )
138
142
139
143
r , w , err := os .Pipe ()
140
- assert .Nil (t , err )
144
+ require .Nil (t , err )
141
145
142
146
os .Stderr = w
143
147
os .Stdout = w
144
- assert .Nil (t , os .Chdir (root ))
148
+ require .Nil (t , os .Chdir (root ))
145
149
146
150
bufChannel := make (chan string )
147
151
148
152
go func () {
149
153
buf := new (bytes.Buffer )
150
154
_ , err = io .Copy (buf , r )
151
- assert .Nil (t , err )
152
- assert .Nil (t , r .Close ())
155
+ require .Nil (t , err )
156
+ require .Nil (t , r .Close ())
153
157
154
158
bufChannel <- buf .String ()
155
159
}()
156
160
157
161
exitCode := mainCmd (exec )
158
162
159
- assert .Nil (t , w .Close ())
163
+ require .Nil (t , w .Close ())
160
164
161
165
os .Stderr = saveStderr
162
166
os .Stdout = saveStdout
163
- assert .Nil (t , os .Chdir (saveCwd ))
167
+ require .Nil (t , os .Chdir (saveCwd ))
164
168
165
169
out := <- bufChannel
166
170
167
- assert .Equal (t , expectedExitCode , exitCode )
168
- assert .Contains (t , out , contains )
171
+ require .Equal (t , expectedExitCode , exitCode )
172
+ require .Contains (t , out , contains )
169
173
}
0 commit comments