Skip to content

Commit adcccfd

Browse files
Fail with error if a reserved name is used as sketch name
1 parent e8c45cb commit adcccfd

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
@@ -40,6 +40,9 @@ void loop() {
4040
var sketchNameMaxLength = 63
4141
var sketchNameValidationRegex = regexp.MustCompile(`^[0-9a-zA-Z_][0-9a-zA-Z_\.-]*$`)
4242

43+
var invalidNames = []string{"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5",
44+
"COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
45+
4346
// NewSketch creates a new sketch via gRPC
4447
func NewSketch(ctx context.Context, req *rpc.NewSketchRequest) (*rpc.NewSketchResponse, error) {
4548
var sketchesDir string
@@ -84,5 +87,10 @@ func validateSketchName(name string) error {
8487
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 ".".`,
8588
name))}
8689
}
90+
for _, invalid := range invalidNames {
91+
if name == invalid {
92+
return &arduino.CantCreateSketchError{Cause: errors.New(tr(`sketch name cannot be the reserved name "%[1]s"`, invalid))}
93+
}
94+
}
8795
return nil
8896
}

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)