Skip to content

Commit 88f96c5

Browse files
committed
Use most appropriate test assertion types
The require package's assertion functions immediately terminate the test when the assertion fails. The assert package's assertion functions allow the test to continue when the assertion fails. For this reason, the require functions should always and only be used when the failure of the assertion indicates that the results of all subsequent tests will be meaningless. In cases where the failure of the assertion has no bearing on the results of subsequent tests, the assert functions should always be used so the test results can be as comprehensive as possible to allow all issues to be fixed before the next test run.
1 parent 9fd649e commit 88f96c5

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

Diff for: internal/result/result_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ func TestWriteReport(t *testing.T) {
297297
require.Nil(t, err)
298298

299299
flags.Set("report-file", reportFilePath.Join("report-file.json").String())
300-
assert.Nil(t, configuration.Initialize(flags, projectPaths))
300+
require.Nil(t, configuration.Initialize(flags, projectPaths))
301301
assert.Error(t, Results.WriteReport(), "Parent folder creation should fail due to a collision with an existing file at that path")
302302

303303
reportFilePath = reportFolderPath.Join("report-file-subfolder", "report-file-subsubfolder", "report-file.json")
304304
flags.Set("report-file", reportFilePath.String())
305-
assert.Nil(t, configuration.Initialize(flags, projectPaths))
305+
require.Nil(t, configuration.Initialize(flags, projectPaths))
306306
assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders")
307307

308308
reportFile, err := reportFilePath.Open()

Diff for: internal/rule/schema/schema_test.go

+47-46
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-lint/internal/rule/schema/testdata"
2424
"github.com/arduino/go-properties-orderedmap"
2525
"github.com/ory/jsonschema/v3"
26+
"github.com/stretchr/testify/assert"
2627
"github.com/stretchr/testify/require"
2728
)
2829

@@ -50,27 +51,27 @@ func init() {
5051
}
5152

5253
func TestCompile(t *testing.T) {
53-
require.Panics(t, func() {
54+
assert.Panics(t, func() {
5455
Compile("valid-schema-with-references.json", []string{"nonexistent.json"}, testdata.Asset)
5556
})
5657

57-
require.Panics(t, func() {
58+
assert.Panics(t, func() {
5859
Compile("valid-schema-with-references.json", []string{"schema-without-id.json"}, testdata.Asset)
5960
})
6061

61-
require.Panics(t, func() {
62+
assert.Panics(t, func() {
6263
Compile("invalid-schema.json", []string{}, testdata.Asset)
6364
})
6465

65-
require.Panics(t, func() {
66+
assert.Panics(t, func() {
6667
Compile("valid-schema-with-references.json", []string{}, testdata.Asset)
6768
})
6869

69-
require.NotPanics(t, func() {
70+
assert.NotPanics(t, func() {
7071
Compile("valid-schema.json", []string{}, testdata.Asset)
7172
})
7273

73-
require.NotPanics(t, func() {
74+
assert.NotPanics(t, func() {
7475
Compile(
7576
"valid-schema-with-references.json",
7677
[]string{
@@ -86,134 +87,134 @@ func TestValidate(t *testing.T) {
8687
schemaObject := Compile("valid-schema.json", []string{}, testdata.Asset)
8788
propertiesMap := properties.NewFromHashmap(validMap)
8889
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject)
89-
require.Nil(t, validationResult.Result)
90+
assert.Nil(t, validationResult.Result)
9091

9192
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
92-
require.Nil(t, validationResult.Result)
93+
assert.Nil(t, validationResult.Result)
9394

9495
propertiesMap.Set("property1", "a")
9596
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject)
96-
require.Equal(t, "#/property1", validationResult.Result.InstancePtr)
97-
require.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr)
97+
assert.Equal(t, "#/property1", validationResult.Result.InstancePtr)
98+
assert.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr)
9899
}
99100

100101
func TestRequiredPropertyMissing(t *testing.T) {
101102
propertiesMap := properties.NewFromHashmap(validMap)
102103
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
103-
require.False(t, RequiredPropertyMissing("property1", validationResult))
104+
assert.False(t, RequiredPropertyMissing("property1", validationResult))
104105

105106
propertiesMap.Remove("property1")
106107
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
107-
require.True(t, RequiredPropertyMissing("property1", validationResult))
108+
assert.True(t, RequiredPropertyMissing("property1", validationResult))
108109
}
109110

110111
func TestPropertyPatternMismatch(t *testing.T) {
111112
propertiesMap := properties.NewFromHashmap(validMap)
112113
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
113-
require.False(t, PropertyPatternMismatch("property2", validationResult))
114+
assert.False(t, PropertyPatternMismatch("property2", validationResult))
114115

115116
propertiesMap.Set("property2", "fOo")
116117
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
117-
require.True(t, PropertyPatternMismatch("property2", validationResult))
118+
assert.True(t, PropertyPatternMismatch("property2", validationResult))
118119

119-
require.False(t, PropertyPatternMismatch("property1", validationResult))
120+
assert.False(t, PropertyPatternMismatch("property1", validationResult))
120121
}
121122

122123
func TestPropertyLessThanMinLength(t *testing.T) {
123124
propertiesMap := properties.NewFromHashmap(validMap)
124125
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
125-
require.False(t, PropertyLessThanMinLength("property1", validationResult))
126+
assert.False(t, PropertyLessThanMinLength("property1", validationResult))
126127

127128
propertiesMap.Set("property1", "a")
128129
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
129-
require.True(t, PropertyLessThanMinLength("property1", validationResult))
130+
assert.True(t, PropertyLessThanMinLength("property1", validationResult))
130131
}
131132

132133
func TestPropertyGreaterThanMaxLength(t *testing.T) {
133134
propertiesMap := properties.NewFromHashmap(validMap)
134135
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
135-
require.False(t, PropertyGreaterThanMaxLength("property1", validationResult))
136+
assert.False(t, PropertyGreaterThanMaxLength("property1", validationResult))
136137

137138
propertiesMap.Set("property1", "12345")
138139
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
139-
require.True(t, PropertyGreaterThanMaxLength("property1", validationResult))
140+
assert.True(t, PropertyGreaterThanMaxLength("property1", validationResult))
140141
}
141142

142143
func TestPropertyEnumMismatch(t *testing.T) {
143144
propertiesMap := properties.NewFromHashmap(validMap)
144145
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
145-
require.False(t, PropertyEnumMismatch("property3", validationResult))
146+
assert.False(t, PropertyEnumMismatch("property3", validationResult))
146147

147148
propertiesMap.Set("property3", "invalid")
148149
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
149-
require.True(t, PropertyEnumMismatch("property3", validationResult))
150+
assert.True(t, PropertyEnumMismatch("property3", validationResult))
150151
}
151152

152153
func TestPropertyDependenciesMissing(t *testing.T) {
153154
propertiesMap := properties.NewFromHashmap(validMap)
154155
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
155-
require.False(t, PropertyDependenciesMissing("dependentProperty", validationResult))
156+
assert.False(t, PropertyDependenciesMissing("dependentProperty", validationResult))
156157

157158
propertiesMap.Remove("dependencyProperty")
158159
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
159-
require.True(t, PropertyDependenciesMissing("dependentProperty", validationResult))
160+
assert.True(t, PropertyDependenciesMissing("dependentProperty", validationResult))
160161
}
161162

162163
func TestMisspelledOptionalPropertyFound(t *testing.T) {
163164
propertiesMap := properties.NewFromHashmap(validMap)
164165
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
165-
require.False(t, MisspelledOptionalPropertyFound(validationResult))
166+
assert.False(t, MisspelledOptionalPropertyFound(validationResult))
166167

167168
propertiesMap.Set("porperties", "foo")
168169
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
169-
require.True(t, MisspelledOptionalPropertyFound(validationResult))
170+
assert.True(t, MisspelledOptionalPropertyFound(validationResult))
170171
}
171172

172173
func TestValidationErrorMatch(t *testing.T) {
173174
propertiesMap := properties.NewFromHashmap(validMap)
174175
validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
175-
require.False(t, ValidationErrorMatch("", "", "", "", validationResult))
176+
assert.False(t, ValidationErrorMatch("", "", "", "", validationResult))
176177

177178
propertiesMap.Set("property2", "fOo")
178179
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
179-
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
180-
require.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult))
181-
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult))
182-
require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult))
183-
require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult))
184-
require.True(t, ValidationErrorMatch("", "", "", "", validationResult))
180+
assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
181+
assert.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult))
182+
assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult))
183+
assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult))
184+
assert.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult))
185+
assert.True(t, ValidationErrorMatch("", "", "", "", validationResult))
185186

186187
propertiesMap.Set("property3", "bAz")
187188
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
188-
require.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword")
189+
assert.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword")
189190

190191
propertiesMap = properties.NewFromHashmap(validMap)
191192
propertiesMap.Remove("property1")
192193
validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences)
193-
require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
194-
require.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult))
194+
assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult))
195+
assert.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult))
195196
}
196197

197198
func Test_loadReferencedSchema(t *testing.T) {
198199
compiler := jsonschema.NewCompiler()
199200

200-
require.Panics(
201+
assert.Panics(
201202
t,
202203
func() {
203204
loadReferencedSchema(compiler, "nonexistent.json", testdata.Asset)
204205
},
205206
)
206-
require.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset))
207-
require.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset))
207+
assert.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset))
208+
assert.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset))
208209
}
209210

210211
func Test_schemaID(t *testing.T) {
211212
_, err := schemaID("schema-without-id.json", testdata.Asset)
212-
require.NotNil(t, err)
213+
assert.NotNil(t, err)
213214

214215
id, err := schemaID("valid-schema.json", testdata.Asset)
215-
require.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id)
216216
require.Nil(t, err)
217+
assert.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id)
217218
}
218219

219220
func Test_validationErrorSchemaPointerValue(t *testing.T) {
@@ -227,17 +228,17 @@ func Test_validationErrorSchemaPointerValue(t *testing.T) {
227228

228229
schemaPointerValueInterface := validationErrorSchemaPointerValue(validationError)
229230
schemaPointerValue, ok := schemaPointerValueInterface.(string)
230-
require.True(t, ok)
231-
require.Equal(t, "^[a-z]+$", schemaPointerValue)
231+
assert.True(t, ok)
232+
assert.Equal(t, "^[a-z]+$", schemaPointerValue)
232233
}
233234

234235
func Test_validationErrorContextMatch(t *testing.T) {
235236
validationError := jsonschema.ValidationError{
236237
Context: nil,
237238
}
238239

239-
require.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError))
240-
require.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError))
240+
assert.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError))
241+
assert.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError))
241242

242243
validationError.Context = &jsonschema.ValidationErrorContextRequired{
243244
Missing: []string{
@@ -246,6 +247,6 @@ func Test_validationErrorContextMatch(t *testing.T) {
246247
},
247248
}
248249

249-
require.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError))
250-
require.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError))
250+
assert.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError))
251+
assert.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError))
251252
}

0 commit comments

Comments
 (0)