Skip to content

fix: sketch name validation error messages #2059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions commands/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 6 additions & 10 deletions commands/sketch/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sketch

import (
"context"
"fmt"
"testing"

"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -11,7 +12,6 @@ import (
func Test_SketchNameWrongPattern(t *testing.T) {
invalidNames := []string{
"&",
"",
".hello",
"_hello",
"-hello",
Expand All @@ -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))
}
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down