Skip to content

Commit 7959b91

Browse files
Fail with error if a reserved name is used as sketch name
1 parent 478a3a4 commit 7959b91

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: commands/sketch/new.go

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void loop() {
3939
var sketchNameMaxLength = 63
4040
var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z_](?:[0-9a-zA-Z_\.-]*[0-9a-zA-Z_-]|)$`)
4141

42+
var invalidNames = []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5",
43+
"COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
44+
4245
// NewSketch creates a new sketch via gRPC
4346
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
4447
var sketchesDir string
@@ -83,5 +86,10 @@ func validateSketchName(name string) error {
8386
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`invalid sketch name "%[1]s": the first character must be alphanumeric or "_", the following ones can also contain "-" and ".". The last one cannot be ".".`,
8487
name))}
8588
}
89+
for _, invalid := range invalidNames {
90+
if name == invalid {
91+
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`sketch name cannot be the reserved name "%[1]s"`, invalid))}
92+
}
93+
}
8694
return nil
8795
}

Diff for: commands/sketch/new_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,15 @@ func Test_SketchNameOk(t *testing.T) {
9191
require.Nil(t, err)
9292
}
9393
}
94+
95+
func Test_SketchNameReserved(t *testing.T) {
96+
invalidNames := []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5",
97+
"COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
98+
for _, name := range invalidNames {
99+
_, err := NewSketch(context.Background(), &commands.NewSketchRequest{
100+
SketchName: name,
101+
SketchDir: t.TempDir(),
102+
})
103+
require.EqualError(t, err, fmt.Sprintf(`Can't create sketch: sketch name cannot be the reserved name "%s"`, name))
104+
}
105+
}

0 commit comments

Comments
 (0)