-
Notifications
You must be signed in to change notification settings - Fork 12
Add simple cli to validate expressions #136
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
Closed
+380
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/github/go-spdx/v2/spdxexp" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var filePath string | ||
|
|
||
| var rootCmd = &cobra.Command{ | ||
| Use: "spdx-validate", | ||
| Short: "Validate SPDX license expressions", | ||
| Long: `spdx-validate reads newline-separated SPDX license expressions and validates them. | ||
| It reads from stdin by default, or from a file specified with -f/--file. | ||
| Blank lines are skipped. Exits 0 if all expressions are valid, or 1 if any | ||
| are invalid. | ||
| Examples: | ||
| echo "MIT" | spdx-validate | ||
| printf "MIT\nApache-2.0\n" | spdx-validate | ||
| spdx-validate -f licenses.txt`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| var r io.Reader = os.Stdin | ||
| if filePath != "" { | ||
| f, err := os.Open(filePath) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to open file: %w", err) | ||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| defer f.Close() | ||
| r = f | ||
| } | ||
| ok, err := validateExpressions(r, os.Stderr) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !ok { | ||
| os.Exit(1) | ||
| } | ||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| }, | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.Flags().StringVarP(&filePath, "file", "f", "", "path to a newline-separated file of SPDX expressions") | ||
| } | ||
|
|
||
| // validateExpressions reads newline-separated SPDX expressions from r, | ||
| // validates each one, and writes error messages to w for any that are invalid. | ||
| // Returns (true, nil) when all are valid, (false, nil) when any are invalid, or | ||
| // (false, err) on read errors or when no expressions are found. | ||
| func validateExpressions(r io.Reader, w io.Writer) (bool, error) { | ||
| scanner := bufio.NewScanner(r) | ||
| lineNum := 0 | ||
| failures := 0 | ||
|
|
||
| for scanner.Scan() { | ||
| lineNum++ | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" { | ||
| continue | ||
| } | ||
| valid, _ := spdxexp.ValidateLicenses([]string{line}) | ||
| if !valid { | ||
| failures++ | ||
| fmt.Fprintf(w, "line %d: invalid SPDX expression: %q\n", lineNum, line) | ||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return false, fmt.Errorf("error reading file: %w", err) | ||
| } | ||
|
|
||
| if lineNum == 0 || (lineNum > 0 && failures == lineNum) { | ||
| return false, fmt.Errorf("no valid expressions found") | ||
| } | ||
|
|
||
| if failures > 0 { | ||
| fmt.Fprintf(w, "%d of %d expressions failed validation\n", failures, lineNum) | ||
| return false, nil | ||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return true, nil | ||
| } | ||
|
|
||
| func main() { | ||
| if err := rootCmd.Execute(); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // --- Tests for single expression on stdin --- | ||
|
|
||
| func TestValidateExpressions_SingleValid(t *testing.T) { | ||
| tests := []string{ | ||
| "MIT", | ||
| "Apache-2.0", | ||
| "BSD-3-Clause", | ||
| "Apache-2.0 OR MIT", | ||
| "MIT AND ISC", | ||
| "GPL-3.0-only WITH Classpath-exception-2.0", | ||
| } | ||
| for _, expr := range tests { | ||
| t.Run(expr, func(t *testing.T) { | ||
| r := strings.NewReader(expr + "\n") | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !ok { | ||
| t.Errorf("expected valid, got invalid; stderr: %s", w.String()) | ||
| } | ||
| if w.Len() != 0 { | ||
| t.Errorf("expected no stderr output, got: %s", w.String()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_SingleInvalid(t *testing.T) { | ||
| tests := []string{ | ||
| "BOGUS-LICENSE", | ||
| "NOT-A-REAL-ID", | ||
| "MIT ANDOR Apache-2.0", | ||
| } | ||
| for _, expr := range tests { | ||
| t.Run(expr, func(t *testing.T) { | ||
| r := strings.NewReader(expr + "\n") | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err == nil { | ||
| t.Fatal("expected error for single invalid expression, got nil") | ||
| } | ||
| if ok { | ||
| t.Error("expected invalid, got valid") | ||
| } | ||
| if !strings.Contains(w.String(), "invalid SPDX expression") { | ||
| t.Errorf("expected error message in output, got: %s", w.String()) | ||
| } | ||
| if !strings.Contains(w.String(), expr) { | ||
| t.Errorf("expected expression %q in output, got: %s", expr, w.String()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for multiple expressions --- | ||
|
|
||
| func TestValidateExpressions_AllValid(t *testing.T) { | ||
| input := "MIT\nApache-2.0\nBSD-3-Clause OR MIT\n" | ||
| r := strings.NewReader(input) | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !ok { | ||
| t.Errorf("expected all valid, got invalid; stderr: %s", w.String()) | ||
| } | ||
| if w.Len() != 0 { | ||
| t.Errorf("expected no stderr output, got: %s", w.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_SomeInvalid(t *testing.T) { | ||
| input := "MIT\nNOT-A-LICENSE\nApache-2.0\nALSO-BOGUS\n" | ||
| r := strings.NewReader(input) | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if ok { | ||
| t.Error("expected invalid result, got valid") | ||
| } | ||
| output := w.String() | ||
| if !strings.Contains(output, `"NOT-A-LICENSE"`) { | ||
| t.Errorf("expected NOT-A-LICENSE in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, `"ALSO-BOGUS"`) { | ||
| t.Errorf("expected ALSO-BOGUS in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "line 2:") { | ||
| t.Errorf("expected 'line 2:' in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "line 4:") { | ||
| t.Errorf("expected 'line 4:' in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "2 of 4 expressions failed") { | ||
| t.Errorf("expected summary in output, got: %s", output) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_AllInvalid(t *testing.T) { | ||
| input := "BOGUS-1\nBOGUS-2\n" | ||
| r := strings.NewReader(input) | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err == nil { | ||
| t.Fatal("expected error when all expressions are invalid, got nil") | ||
| } | ||
| if ok { | ||
| t.Error("expected ok=false") | ||
| } | ||
| if !strings.Contains(err.Error(), "no valid expressions found") { | ||
| t.Errorf("expected 'no valid expressions found' error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_EmptyFile(t *testing.T) { | ||
| r := strings.NewReader("") | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err == nil { | ||
| t.Fatal("expected error for empty file, got nil") | ||
| } | ||
| if ok { | ||
| t.Error("expected ok=false for empty file") | ||
| } | ||
| if !strings.Contains(err.Error(), "no valid expressions found") { | ||
| t.Errorf("expected 'no valid expressions found' error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_SkipsBlankLines(t *testing.T) { | ||
| input := "\nMIT\n\n\nApache-2.0\n\n" | ||
| r := strings.NewReader(input) | ||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(r, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !ok { | ||
| t.Errorf("expected all valid, got invalid; stderr: %s", w.String()) | ||
| } | ||
| } | ||
|
|
||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // --- Integration test using a temp file --- | ||
|
|
||
| func TestValidateExpressions_FromTempFile(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "licenses.txt") | ||
|
|
||
| content := "MIT\nApache-2.0\nBSD-2-Clause\n" | ||
| if err := os.WriteFile(path, []byte(content), 0600); err != nil { | ||
| t.Fatalf("failed to write temp file: %v", err) | ||
| } | ||
|
|
||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| t.Fatalf("failed to open temp file: %v", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(f, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !ok { | ||
| t.Errorf("expected all valid from file, got invalid; stderr: %s", w.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateExpressions_FromTempFileWithFailures(t *testing.T) { | ||
| dir := t.TempDir() | ||
| path := filepath.Join(dir, "licenses.txt") | ||
|
|
||
| content := "MIT\nINVALID-1\nApache-2.0\nINVALID-2\nBSD-2-Clause\n" | ||
| if err := os.WriteFile(path, []byte(content), 0600); err != nil { | ||
| t.Fatalf("failed to write temp file: %v", err) | ||
| } | ||
|
|
||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| t.Fatalf("failed to open temp file: %v", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var w bytes.Buffer | ||
| ok, err := validateExpressions(f, &w) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if ok { | ||
| t.Error("expected invalid result from file with bad entries") | ||
| } | ||
| output := w.String() | ||
| if !strings.Contains(output, `"INVALID-1"`) { | ||
| t.Errorf("expected INVALID-1 in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, `"INVALID-2"`) { | ||
| t.Errorf("expected INVALID-2 in output, got: %s", output) | ||
| } | ||
| if !strings.Contains(output, "2 of 5 expressions failed") { | ||
| t.Errorf("expected summary in output, got: %s", output) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.