Skip to content

Commit

Permalink
fix: sketch name validation error messages (#2059)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikappa authored Feb 6, 2023
1 parent 2b8d6d7 commit 4c0aaa8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
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

0 comments on commit 4c0aaa8

Please sign in to comment.