From fb8634a99119e409e45d59c60f989d0cd47e4e81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Tue, 20 Feb 2024 14:27:31 +0100 Subject: [PATCH] blueprint: improve error message on missing name the error should not infer that a version is mandatory. Also the error message is now explicitly checked --- internal/blueprint/blueprint.go | 2 +- internal/client/blueprints_test.go | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index 1602ea714..836f734ab 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -105,7 +105,7 @@ func (b *Blueprint) Initialize() error { if len(p.Name) == 0 { var errMsg string if len(p.Version) == 0 { - errMsg = fmt.Sprintf("Entry #%d has neither version nor name.", i+1) + errMsg = fmt.Sprintf("Entry #%d has no name.", i+1) } else { errMsg = fmt.Sprintf("Entry #%d has version '%v' but no name.", i+1, p.Version) } diff --git a/internal/client/blueprints_test.go b/internal/client/blueprints_test.go index 18ab2026e..d4d9f9b29 100644 --- a/internal/client/blueprints_test.go +++ b/internal/client/blueprints_test.go @@ -1238,47 +1238,52 @@ func TestEmptyPackageNameBlueprintJsonV0(t *testing.T) { "packages": [{"name": "", "version": "1.32.1"}] }` - // Push a blueprint + expectedErrorPrefix := "BlueprintsError: All package entries need to contain the name of the package." + resp, err := PostJSONBlueprintV0(testState.socket, bp) require.NoError(t, err, "failed with a client error") require.False(t, resp.Status, "Negative status expected.") require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") - assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") + reasoning := "I expected an error message starting with\n" + expectedErrorPrefix + + "\nnot\n" + resp.Errors[0].String() + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), expectedErrorPrefix), reasoning) } func TestEmptyPackageNameBlueprintV0(t *testing.T) { bp := `name = "EMPTY-PACKAGE-NAME" description = "empty package name" version = "0.0.1" - modules = [] - groups = [] - distro = "test-distro" [[packages]] version = "*" ` + expectedErrorPrefix := "BlueprintsError: All package entries need to contain the name of the package." + resp, err := PostTOMLBlueprintV0(testState.socket, bp) require.NoError(t, err, "failed with a client error") require.False(t, resp.Status, "Negative status expected.") require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") - assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") + reasoning := "I expected an error message starting with\n" + expectedErrorPrefix + + "\nnot\n" + resp.Errors[0].String() + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), expectedErrorPrefix), reasoning) } func TestEmptyPackageNameAndVersionBlueprintV0(t *testing.T) { bp := `name = "EMPTY-PACKAGE-NAME" description = "empty package name" version = "0.0.1" - modules = [] - groups = [] - distro = "test-distro" [[packages]] ` + expectedErrorPrefix := "BlueprintsError: All package entries need to contain the name of the package." + resp, err := PostTOMLBlueprintV0(testState.socket, bp) require.NoError(t, err, "failed with a client error") require.False(t, resp.Status, "Negative status expected.") require.Equal(t, len(resp.Errors), 1, "There should be exactly one error") - assert.True(t, strings.HasPrefix(resp.Errors[0].String(), "BlueprintsError"), "I expect a BlueprintsError") + reasoning := "I expected an error message starting with\n" + expectedErrorPrefix + + "\nnot\n" + resp.Errors[0].String() + assert.True(t, strings.HasPrefix(resp.Errors[0].String(), expectedErrorPrefix), reasoning) }