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 { ...@@ -75,14 +75,13 @@ func validateSketchName(name string) error {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name cannot be empty"))} return &arduino.CantCreateSketchError{Cause: errors.New(tr("sketch name cannot be empty"))}
} }
if len(name) > sketchNameMaxLength { 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), len(name),
sketchNameMaxLength))} sketchNameMaxLength))}
} }
if !sketchNameValidationRegex.MatchString(name) { if !sketchNameValidationRegex.MatchString(name) {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("invalid sketch name \"%s\". Required pattern %s", 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, name))}
sketchNameValidationRegex.String()))}
} }
return nil return nil
} }
...@@ -2,6 +2,7 @@ package sketch ...@@ -2,6 +2,7 @@ package sketch
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
...@@ -11,7 +12,6 @@ import ( ...@@ -11,7 +12,6 @@ import (
func Test_SketchNameWrongPattern(t *testing.T) { func Test_SketchNameWrongPattern(t *testing.T) {
invalidNames := []string{ invalidNames := []string{
"&", "&",
"",
".hello", ".hello",
"_hello", "_hello",
"-hello", "-hello",
...@@ -24,11 +24,9 @@ func Test_SketchNameWrongPattern(t *testing.T) { ...@@ -24,11 +24,9 @@ func Test_SketchNameWrongPattern(t *testing.T) {
SketchName: name, SketchName: name,
SketchDir: t.TempDir(), SketchDir: t.TempDir(),
}) })
require.NotNil(t, err)
require.Error(t, err, `Can't create sketch: invalid sketch name "%s". Required pattern %s`, 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, name))
sketchNameValidationRegex)
} }
} }
...@@ -38,9 +36,8 @@ func Test_SketchNameEmpty(t *testing.T) { ...@@ -38,9 +36,8 @@ func Test_SketchNameEmpty(t *testing.T) {
SketchName: emptyName, SketchName: emptyName,
SketchDir: t.TempDir(), 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) { func Test_SketchNameTooLong(t *testing.T) {
...@@ -52,11 +49,10 @@ func Test_SketchNameTooLong(t *testing.T) { ...@@ -52,11 +49,10 @@ func Test_SketchNameTooLong(t *testing.T) {
SketchName: string(tooLongName), SketchName: string(tooLongName),
SketchDir: t.TempDir(), 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), len(tooLongName),
sketchNameMaxLength) sketchNameMaxLength))
} }
func Test_SketchNameOk(t *testing.T) { 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