Unverified Commit 4c0aaa87 authored by Luca Bianconi's avatar Luca Bianconi Committed by GitHub

fix: sketch name validation error messages (#2059)

parent 2b8d6d76
......@@ -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
}
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment