Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81018a5

Browse files
committedOct 18, 2022
fix some tests
1 parent 81e08ab commit 81018a5

File tree

98 files changed

+207
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+207
-174
lines changed
 

‎cmd/go-mutesting/main.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ func checkArguments(args []string, opts *models.Options) (bool, int) {
8484
}
8585

8686
if opts.General.Config != "" {
87-
yamlFile, err := ioutil.ReadFile(opts.General.Config)
87+
yamlFile, err := os.ReadFile(opts.General.Config)
8888
if err != nil {
8989
return true, exitError("Could not read config file: %q", opts.General.Config)
9090
}
9191
err = yaml.Unmarshal(yamlFile, &opts.Config)
9292
if err != nil {
9393
return true, exitError("Could not unmarshall config file: %q, %v", opts.General.Config, err)
9494
}
95+
96+
// default values
97+
if opts.Config.MinMsi == nil {
98+
opts.Config.MinMsi = big.NewFloat(0)
99+
}
95100
}
96101

97102
return false, 0
@@ -122,6 +127,8 @@ type mutatorItem struct {
122127

123128
func mainCmd(args []string) int {
124129
var opts = &models.Options{}
130+
opts.Config.MinMsi = big.NewFloat(0)
131+
125132
var mutationBlackList = map[string]struct{}{}
126133

127134
if exit, exitCode := checkArguments(args, opts); exit {
@@ -158,7 +165,7 @@ func mainCmd(args []string) int {
158165

159166
if len(opts.Files.Blacklist) > 0 {
160167
for _, f := range opts.Files.Blacklist {
161-
c, err := ioutil.ReadFile(f)
168+
c, err := os.ReadFile(f)
162169
if err != nil {
163170
return exitError("Cannot read blacklist file %q: %v", f, err)
164171
}
@@ -306,8 +313,9 @@ MUTATOR:
306313

307314
verbose(opts, "Save report into %q", models.ReportFileName)
308315

309-
fmt.Println("!!!!!!!!!!!!!!!!", report.MsiScore(), opts.Config.MinMsi.String())
310-
if big.NewFloat(report.MsiScore()).Cmp(opts.Config.MinMsi) < 0 {
316+
fmt.Printf("MSI minimal %s, actual %s\n", opts.Config.MinMsi.Text('f', 6), report.MsiScore().Text('f', 6))
317+
318+
if report.MsiScore().Cmp(opts.Config.MinMsi) < 0 {
311319
return exitError("The MSI %f is less than the minimum allowed value %f", report.MsiScore(), opts.Config.MinMsi)
312320
}
313321

‎cmd/go-mutesting/main_test.go

+30-26
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/JekaMas/go-mutesting/internal/models"
87
"io"
98
"io/ioutil"
9+
"math/big"
1010
"os"
1111
"testing"
1212

13-
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
15+
"github.com/JekaMas/go-mutesting/internal/models"
1416
)
1517

1618
func TestMainSimple(t *testing.T) {
@@ -64,14 +66,14 @@ func TestMainSkipWithoutTest(t *testing.T) {
6466
}
6567

6668
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)
6971

7072
reportFileName := "reportTestMainJSONReport.json"
7173
jsonFile := tmpDir + "/" + reportFileName
7274
if _, err := os.Stat(jsonFile); err == nil {
7375
err = os.Remove(jsonFile)
74-
assert.NoError(t, err)
76+
require.NoError(t, err)
7577
}
7678

7779
models.ReportFileName = jsonFile
@@ -85,8 +87,8 @@ func TestMainJSONReport(t *testing.T) {
8587
)
8688

8789
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)
9092

9193
defer func() {
9294
err = os.Remove(jsonFile)
@@ -96,11 +98,11 @@ func TestMainJSONReport(t *testing.T) {
9698
}()
9799

98100
jsonData, err := ioutil.ReadFile(jsonFile)
99-
assert.NoError(t, err)
101+
require.NoError(t, err)
100102

101103
var mutationReport models.Report
102104
err = json.Unmarshal(jsonData, &mutationReport)
103-
assert.NoError(t, err)
105+
require.NoError(t, err)
104106

105107
expectedStats := models.Stats{
106108
TotalMutantsCount: 60,
@@ -110,60 +112,62 @@ func TestMainJSONReport(t *testing.T) {
110112
ErrorCount: 0,
111113
SkippedCount: 0,
112114
TimeOutCount: 0,
113-
Msi: 0.5833333333333334,
115+
Msi: big.NewFloat(0.5833333333333334),
114116
MutationCodeCoverage: 0,
115117
CoveredCodeMsi: 0,
116118
DuplicatedCount: 0,
117119
}
118120

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)
124126

125127
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")
127129
}
128130
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")
130132
}
131133
}
132134

133135
func testMain(t *testing.T, root string, exec []string, expectedExitCode int, contains string) {
136+
t.Helper()
137+
134138
saveStderr := os.Stderr
135139
saveStdout := os.Stdout
136140
saveCwd, err := os.Getwd()
137-
assert.Nil(t, err)
141+
require.Nil(t, err)
138142

139143
r, w, err := os.Pipe()
140-
assert.Nil(t, err)
144+
require.Nil(t, err)
141145

142146
os.Stderr = w
143147
os.Stdout = w
144-
assert.Nil(t, os.Chdir(root))
148+
require.Nil(t, os.Chdir(root))
145149

146150
bufChannel := make(chan string)
147151

148152
go func() {
149153
buf := new(bytes.Buffer)
150154
_, 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())
153157

154158
bufChannel <- buf.String()
155159
}()
156160

157161
exitCode := mainCmd(exec)
158162

159-
assert.Nil(t, w.Close())
163+
require.Nil(t, w.Close())
160164

161165
os.Stderr = saveStderr
162166
os.Stdout = saveStdout
163-
assert.Nil(t, os.Chdir(saveCwd))
167+
require.Nil(t, os.Chdir(saveCwd))
164168

165169
out := <-bufChannel
166170

167-
assert.Equal(t, expectedExitCode, exitCode)
168-
assert.Contains(t, out, contains)
171+
require.Equal(t, expectedExitCode, exitCode)
172+
require.Contains(t, out, contains)
169173
}

0 commit comments

Comments
 (0)
Please sign in to comment.