From 88f96c56547dd6780a14bb54545b316731012e8c Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 10 Jan 2021 22:02:54 -0800 Subject: [PATCH] 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. --- internal/result/result_test.go | 4 +- internal/rule/schema/schema_test.go | 93 +++++++++++++++-------------- 2 files changed, 49 insertions(+), 48 deletions(-) diff --git a/internal/result/result_test.go b/internal/result/result_test.go index 09ceaa394..3cae12294 100644 --- a/internal/result/result_test.go +++ b/internal/result/result_test.go @@ -297,12 +297,12 @@ func TestWriteReport(t *testing.T) { require.Nil(t, err) flags.Set("report-file", reportFilePath.Join("report-file.json").String()) - assert.Nil(t, configuration.Initialize(flags, projectPaths)) + require.Nil(t, configuration.Initialize(flags, projectPaths)) assert.Error(t, Results.WriteReport(), "Parent folder creation should fail due to a collision with an existing file at that path") reportFilePath = reportFolderPath.Join("report-file-subfolder", "report-file-subsubfolder", "report-file.json") flags.Set("report-file", reportFilePath.String()) - assert.Nil(t, configuration.Initialize(flags, projectPaths)) + require.Nil(t, configuration.Initialize(flags, projectPaths)) assert.NoError(t, Results.WriteReport(), "Creation of multiple levels of parent folders") reportFile, err := reportFilePath.Open() diff --git a/internal/rule/schema/schema_test.go b/internal/rule/schema/schema_test.go index 6c37ebe93..fd660a135 100644 --- a/internal/rule/schema/schema_test.go +++ b/internal/rule/schema/schema_test.go @@ -23,6 +23,7 @@ import ( "github.com/arduino/arduino-lint/internal/rule/schema/testdata" "github.com/arduino/go-properties-orderedmap" "github.com/ory/jsonschema/v3" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -50,27 +51,27 @@ func init() { } func TestCompile(t *testing.T) { - require.Panics(t, func() { + assert.Panics(t, func() { Compile("valid-schema-with-references.json", []string{"nonexistent.json"}, testdata.Asset) }) - require.Panics(t, func() { + assert.Panics(t, func() { Compile("valid-schema-with-references.json", []string{"schema-without-id.json"}, testdata.Asset) }) - require.Panics(t, func() { + assert.Panics(t, func() { Compile("invalid-schema.json", []string{}, testdata.Asset) }) - require.Panics(t, func() { + assert.Panics(t, func() { Compile("valid-schema-with-references.json", []string{}, testdata.Asset) }) - require.NotPanics(t, func() { + assert.NotPanics(t, func() { Compile("valid-schema.json", []string{}, testdata.Asset) }) - require.NotPanics(t, func() { + assert.NotPanics(t, func() { Compile( "valid-schema-with-references.json", []string{ @@ -86,134 +87,134 @@ func TestValidate(t *testing.T) { schemaObject := Compile("valid-schema.json", []string{}, testdata.Asset) propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject) - require.Nil(t, validationResult.Result) + assert.Nil(t, validationResult.Result) validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.Nil(t, validationResult.Result) + assert.Nil(t, validationResult.Result) propertiesMap.Set("property1", "a") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), schemaObject) - require.Equal(t, "#/property1", validationResult.Result.InstancePtr) - require.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr) + assert.Equal(t, "#/property1", validationResult.Result.InstancePtr) + assert.Equal(t, "#/properties/property1/minLength", validationResult.Result.SchemaPtr) } func TestRequiredPropertyMissing(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, RequiredPropertyMissing("property1", validationResult)) + assert.False(t, RequiredPropertyMissing("property1", validationResult)) propertiesMap.Remove("property1") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, RequiredPropertyMissing("property1", validationResult)) + assert.True(t, RequiredPropertyMissing("property1", validationResult)) } func TestPropertyPatternMismatch(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, PropertyPatternMismatch("property2", validationResult)) + assert.False(t, PropertyPatternMismatch("property2", validationResult)) propertiesMap.Set("property2", "fOo") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, PropertyPatternMismatch("property2", validationResult)) + assert.True(t, PropertyPatternMismatch("property2", validationResult)) - require.False(t, PropertyPatternMismatch("property1", validationResult)) + assert.False(t, PropertyPatternMismatch("property1", validationResult)) } func TestPropertyLessThanMinLength(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, PropertyLessThanMinLength("property1", validationResult)) + assert.False(t, PropertyLessThanMinLength("property1", validationResult)) propertiesMap.Set("property1", "a") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, PropertyLessThanMinLength("property1", validationResult)) + assert.True(t, PropertyLessThanMinLength("property1", validationResult)) } func TestPropertyGreaterThanMaxLength(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, PropertyGreaterThanMaxLength("property1", validationResult)) + assert.False(t, PropertyGreaterThanMaxLength("property1", validationResult)) propertiesMap.Set("property1", "12345") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, PropertyGreaterThanMaxLength("property1", validationResult)) + assert.True(t, PropertyGreaterThanMaxLength("property1", validationResult)) } func TestPropertyEnumMismatch(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, PropertyEnumMismatch("property3", validationResult)) + assert.False(t, PropertyEnumMismatch("property3", validationResult)) propertiesMap.Set("property3", "invalid") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, PropertyEnumMismatch("property3", validationResult)) + assert.True(t, PropertyEnumMismatch("property3", validationResult)) } func TestPropertyDependenciesMissing(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, PropertyDependenciesMissing("dependentProperty", validationResult)) + assert.False(t, PropertyDependenciesMissing("dependentProperty", validationResult)) propertiesMap.Remove("dependencyProperty") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, PropertyDependenciesMissing("dependentProperty", validationResult)) + assert.True(t, PropertyDependenciesMissing("dependentProperty", validationResult)) } func TestMisspelledOptionalPropertyFound(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, MisspelledOptionalPropertyFound(validationResult)) + assert.False(t, MisspelledOptionalPropertyFound(validationResult)) propertiesMap.Set("porperties", "foo") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, MisspelledOptionalPropertyFound(validationResult)) + assert.True(t, MisspelledOptionalPropertyFound(validationResult)) } func TestValidationErrorMatch(t *testing.T) { propertiesMap := properties.NewFromHashmap(validMap) validationResult := Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, ValidationErrorMatch("", "", "", "", validationResult)) + assert.False(t, ValidationErrorMatch("", "", "", "", validationResult)) propertiesMap.Set("property2", "fOo") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult)) - require.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult)) - require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult)) - require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult)) - require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult)) - require.True(t, ValidationErrorMatch("", "", "", "", validationResult)) + assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult)) + assert.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult)) + assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult)) + assert.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult)) + assert.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult)) + assert.True(t, ValidationErrorMatch("", "", "", "", validationResult)) propertiesMap.Set("property3", "bAz") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword") + assert.True(t, ValidationErrorMatch("^#/property3$", "/pattern$", "", "", validationResult), "Match pointer below logic inversion keyword") propertiesMap = properties.NewFromHashmap(validMap) propertiesMap.Remove("property1") validationResult = Validate(general.PropertiesToMap(propertiesMap, 0), validSchemaWithReferences) - require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult)) - require.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult)) + assert.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult)) + assert.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult)) } func Test_loadReferencedSchema(t *testing.T) { compiler := jsonschema.NewCompiler() - require.Panics( + assert.Panics( t, func() { loadReferencedSchema(compiler, "nonexistent.json", testdata.Asset) }, ) - require.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset)) - require.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset)) + assert.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", testdata.Asset)) + assert.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", testdata.Asset)) } func Test_schemaID(t *testing.T) { _, err := schemaID("schema-without-id.json", testdata.Asset) - require.NotNil(t, err) + assert.NotNil(t, err) id, err := schemaID("valid-schema.json", testdata.Asset) - require.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id) require.Nil(t, err) + assert.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-lint/main/internal/rule/schema/testdata/input/valid-schema.json", id) } func Test_validationErrorSchemaPointerValue(t *testing.T) { @@ -227,8 +228,8 @@ func Test_validationErrorSchemaPointerValue(t *testing.T) { schemaPointerValueInterface := validationErrorSchemaPointerValue(validationError) schemaPointerValue, ok := schemaPointerValueInterface.(string) - require.True(t, ok) - require.Equal(t, "^[a-z]+$", schemaPointerValue) + assert.True(t, ok) + assert.Equal(t, "^[a-z]+$", schemaPointerValue) } func Test_validationErrorContextMatch(t *testing.T) { @@ -236,8 +237,8 @@ func Test_validationErrorContextMatch(t *testing.T) { Context: nil, } - require.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError)) - require.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError)) + assert.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError)) + assert.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError)) validationError.Context = &jsonschema.ValidationErrorContextRequired{ Missing: []string{ @@ -246,6 +247,6 @@ func Test_validationErrorContextMatch(t *testing.T) { }, } - require.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError)) - require.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError)) + assert.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError)) + assert.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError)) }