diff --git a/commands/sketch/new.go b/commands/sketch/new.go index 4e8134a5b6c..5efbee8ff48 100644 --- a/commands/sketch/new.go +++ b/commands/sketch/new.go @@ -75,14 +75,13 @@ func validateSketchName(name string) error { return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name cannot be empty"))} } if len(name) > sketchNameMaxLength { - return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name too long (%d characters). Maximum allowed length is %d", + return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name too long (%[1]d characters). Maximum allowed length is %[2]d", len(name), sketchNameMaxLength))} } if !sketchNameValidationRegex.MatchString(name) { - return &arduino.CantCreateSketchError{Cause: errors.New(tr("invalid sketch name \"%s\". Required pattern %s", - name, - sketchNameValidationRegex.String()))} + return &arduino.CantCreateSketchError{Cause: errors.New(tr(`invalid sketch name "%[1]s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`, + name))} } return nil } diff --git a/commands/sketch/new_test.go b/commands/sketch/new_test.go index 88d2e426012..a0de66710ed 100644 --- a/commands/sketch/new_test.go +++ b/commands/sketch/new_test.go @@ -2,6 +2,7 @@ package sketch import ( "context" + "fmt" "testing" "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" @@ -11,7 +12,6 @@ import ( func Test_SketchNameWrongPattern(t *testing.T) { invalidNames := []string{ "&", - "", ".hello", "_hello", "-hello", @@ -24,11 +24,9 @@ func Test_SketchNameWrongPattern(t *testing.T) { SketchName: name, SketchDir: t.TempDir(), }) - require.NotNil(t, err) - require.Error(t, err, `Can't create sketch: invalid sketch name "%s". Required pattern %s`, - name, - sketchNameValidationRegex) + require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: invalid sketch name "%s": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".`, + name)) } } @@ -38,9 +36,8 @@ func Test_SketchNameEmpty(t *testing.T) { SketchName: emptyName, SketchDir: t.TempDir(), }) - require.NotNil(t, err) - require.Error(t, err, `Can't create sketch: sketch name cannot be empty`) + require.EqualError(t, err, `Can't create sketch: sketch name cannot be empty`) } func Test_SketchNameTooLong(t *testing.T) { @@ -52,11 +49,10 @@ func Test_SketchNameTooLong(t *testing.T) { SketchName: string(tooLongName), SketchDir: t.TempDir(), }) - require.NotNil(t, err) - require.Error(t, err, `Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`, + require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`, len(tooLongName), - sketchNameMaxLength) + sketchNameMaxLength)) } func Test_SketchNameOk(t *testing.T) {