Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking] fix: validate sketch name #2051

Merged
merged 6 commits into from
Jan 30, 2023
Merged

Conversation

Bikappa
Copy link
Contributor

@Bikappa Bikappa commented Jan 24, 2023

Please check if the PR fulfills these requirements

See how to contribute

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)

What kind of change does this PR introduce?

It alignes the validation of sketch names to the documentation
https://arduino.github.io/arduino-cli/dev/sketch-specification/#sketch-folders-and-files
Sketch creation request that don't respect the validation rules are rejected

What is the current behavior?

No validation is performed on the sketch name

What is the new behavior?

Sketch names that don't respect the required pattern, consisting in alphanumeric characters, dots, underscores and dashes, or that are longer
than 63 characters cause the request to be rejected.

Does this PR introduce a breaking change, and is titled accordingly?

Yes! Freeform sketch names that were in use before will not be compatible anymore.

Other information

Fixes #2043

@codecov
Copy link

codecov bot commented Jan 24, 2023

Codecov Report

Base: 36.50% // Head: 36.58% // Increases project coverage by +0.07% 🎉

Coverage data is based on head (35eec5e) compared to base (58c6bc3).
Patch coverage: 43.90% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2051      +/-   ##
==========================================
+ Coverage   36.50%   36.58%   +0.07%     
==========================================
  Files         228      228              
  Lines       19306    19339      +33     
==========================================
+ Hits         7048     7075      +27     
- Misses      11434    11436       +2     
- Partials      824      828       +4     
Flag Coverage Δ
unit 36.58% <43.90%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
internal/cli/sketch/new.go 0.00% <0.00%> (ø)
commands/sketch/new.go 69.23% <100.00%> (+69.23%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Bikappa Bikappa added the topic: code Related to content of the project itself label Jan 24, 2023
@Bikappa Bikappa changed the title fix: validate sketch name [breaking] fix: validate sketch name Jan 24, 2023
@Bikappa Bikappa marked this pull request as ready for review January 24, 2023 13:34
@Bikappa Bikappa requested review from per1234, MatteoPologruto and umbynos and removed request for per1234, MatteoPologruto and umbynos January 24, 2023 14:26
@Bikappa
Copy link
Contributor Author

Bikappa commented Jan 24, 2023

Possibly duplicate of #2049

@Bikappa Bikappa closed this Jan 24, 2023
@per1234 per1234 added conclusion: duplicate Has already been submitted type: imperfection Perceived defect in any part of project and removed conclusion: duplicate Has already been submitted labels Jan 24, 2023
@per1234
Copy link
Contributor

per1234 commented Jan 26, 2023

I'll reopen this since the PR #2049 of the prior proposal was closed by @cushonz

Copy link
Contributor

@umbynos umbynos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, l would wait for a review by @per1234 before merging, just to be sure we did not left out anything

commands/sketch/new.go Outdated Show resolved Hide resolved
@Bikappa Bikappa requested a review from per1234 January 27, 2023 14:23
@Bikappa Bikappa force-pushed the fix/sketch-name-validation branch 2 times, most recently from 6274ea5 to bc6e64c Compare January 30, 2023 08:12
Copy link
Contributor

@per1234 per1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Luca!

@Bikappa Bikappa merged commit 4b70e02 into master Jan 30, 2023
@Bikappa Bikappa deleted the fix/sketch-name-validation branch January 30, 2023 08:52
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"sketch name too long (%d characters). Maximum allowed length is %d"
This string has 2 positional parameters, you must replace %d with %[1]d and %[2]d.
This would normally not be a problem, but since it's translated into another language with tr(...), the ordering of the parameters is not guaranteed to stay the same.

sketchNameMaxLength))}
}
if !sketchNameValidationRegex.MatchString(name) {
return &arduino.CantCreateSketchError{Cause: errors.New(tr("invalid sketch name \"%s\". Required pattern %s",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error outputs: invalid sketch name "xxx". Required pattern ^[0-9a-zA-Z][0-9a-zA-Z_\.-]*$
Not everyone is able to digest a regex, maybe a little explanation is better:
invalid sketch name "xxx": the first character must be alphanumeric, the following ones can also contain "_", "-", and ".".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The sketch new command ignores the sketch folder name specification
4 participants