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

feat: check screenshot image type #278

Merged
merged 2 commits into from
Nov 20, 2024
Merged

feat: check screenshot image type #278

merged 2 commits into from
Nov 20, 2024

Conversation

s4kh
Copy link
Contributor

@s4kh s4kh commented Nov 13, 2024

Fixes #277

@s4kh s4kh requested review from a team as code owners November 13, 2024 18:02
@s4kh s4kh requested review from oshirohugo, wbrowne, xnyo, academo, a team and andresmgot and removed request for a team and xnyo November 13, 2024 18:02
if strings.TrimSpace(screenshot.Path) == "" {
reportCount++
pass.ReportResult(pass.AnalyzerName, screenshots, fmt.Sprintf("plugin.json: invalid empty screenshot path: %q", screenshot.Name), "The screenshot path must not be empty.")
} else if !validImageType(screenshot.Path) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it better to do this after invalid empty screenshot in a separate loop? Added here so we can use the same loop.

@s4kh s4kh force-pushed the feat-validate-screenshot-ext branch 2 times, most recently from accf0c3 to d629f7d Compare November 13, 2024 20:19
@s4kh s4kh force-pushed the feat-validate-screenshot-ext branch from d629f7d to 2cb7977 Compare November 13, 2024 20:27
pass.ReportResult(pass.AnalyzerName, screenshots, fmt.Sprintf("plugin.json: invalid empty screenshot path: %q", screenshot.Name), "The screenshot path must not be empty.")
} else if !validImageType(filepath.Join(archiveDir, screenshot.Path)) {
reportCount++
pass.ReportResult(pass.AnalyzerName, screenshotsType, fmt.Sprintf("invalid screenshot image type: %q. Accepted image types: %q", screenshot.Path, acceptedImageTypes), "The screenshot image type invalid.")
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 can be confusing for users if for example they have a typo in their plugin json or the wrong path. We'll be telling them the image type is wrong but in reality the image path is wrong.

Let's have an error if the image path can't be found and a separate one for the image type.

func validImageType(imgPath string) bool {
file, err := os.Open(imgPath)
if err != nil {
fmt.Printf("cannot open file: %v\n", err)
Copy link
Member

Choose a reason for hiding this comment

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

don't output to stdout it'll break the validator output. use logme.DebugFln

// https://pkg.go.dev/net/http#DetectContentType
buffer := make([]byte, 512)
if _, err := file.Read(buffer); err != nil {
fmt.Printf("cannot read file: %v\n", err)
Copy link
Member

Choose a reason for hiding this comment

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

don't output to stdout use our logging lib

@@ -62,18 +65,19 @@ func TestNoScreenshots(t *testing.T) {
func TestInvalidScreenshotPath(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

add a test case for a path that doesn't exist.

Copy link
Member

@academo academo left a comment

Choose a reason for hiding this comment

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

nit: there's no need to put 120kb images for testing data, we can use a small 1kb image (a grafana icon maybe?)

Since we are at that, can we have a table test with a test case for each valid image type?

@s4kh
Copy link
Contributor Author

s4kh commented Nov 14, 2024

nit: there's no need to put 120kb images for testing data, we can use a small 1kb image (a grafana icon maybe?)

Since we are at that, can we have a table test with a test case for each valid image type?

With the table test, I was able to catch an SVG validation error, thanks @academo. 💯

Copy link
Member

@xnyo xnyo left a comment

Choose a reason for hiding this comment

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

LGTM, awesome work! Only one small nit:

@s4kh s4kh requested a review from academo November 15, 2024 13:03
@s4kh s4kh force-pushed the feat-validate-screenshot-ext branch from 90fadc5 to d90bdd0 Compare November 15, 2024 13:21
@s4kh s4kh merged commit aeea69a into main Nov 20, 2024
2 checks passed
@s4kh s4kh deleted the feat-validate-screenshot-ext branch November 20, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Validate screenshot mime types
3 participants