|
| 1 | +package author |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func newRootWithAuthor(t *testing.T) (*cobra.Command, *bytes.Buffer) { |
| 14 | + t.Helper() |
| 15 | + root := &cobra.Command{Use: "nem", SilenceUsage: true} |
| 16 | + root.AddCommand(AuthorCmd) |
| 17 | + t.Cleanup(func() { lintCmd.Flags().Set("install", "false") }) |
| 18 | + var stdout, stderr bytes.Buffer |
| 19 | + root.SetOut(&stdout) |
| 20 | + root.SetErr(&stderr) |
| 21 | + return root, &stdout |
| 22 | +} |
| 23 | + |
| 24 | +func TestLint_CleanFixtureExitsZero(t *testing.T) { |
| 25 | + valid := filepath.Join(repoRoot(t), "internal", "linter", "testdata", "valid") |
| 26 | + root, _ := newRootWithAuthor(t) |
| 27 | + root.SetArgs([]string{"author", "lint", valid}) |
| 28 | + if err := root.Execute(); err != nil { |
| 29 | + t.Fatalf("lint clean fixture: unexpected error %v", err) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestLint_FixtureWithFindingExitsNonZero(t *testing.T) { |
| 34 | + missingName := filepath.Join(repoRoot(t), "internal", "linter", "testdata", "missing-name") |
| 35 | + root, stdout := newRootWithAuthor(t) |
| 36 | + root.SetArgs([]string{"author", "lint", missingName}) |
| 37 | + err := root.Execute() |
| 38 | + if err == nil { |
| 39 | + t.Fatalf("expected non-nil error for fixture with findings; stdout=%q", stdout.String()) |
| 40 | + } |
| 41 | + if !strings.Contains(stdout.String(), "name: must not be empty") { |
| 42 | + t.Errorf("expected finding in stdout; got %q", stdout.String()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestLint_InstallSkipsPackagesWithOfflineFindings(t *testing.T) { |
| 47 | + // Build a tiny repo dir with one package that has a malformed manifest. |
| 48 | + // With --install set, the failing-offline package contributes a finding |
| 49 | + // and the install loop must NOT attempt it (the broken manifest has no |
| 50 | + // archive/versions, so any install attempt would surface a SECOND |
| 51 | + // finding from the install path). |
| 52 | + repo := t.TempDir() |
| 53 | + bad := filepath.Join(repo, "broken") |
| 54 | + if err := os.MkdirAll(bad, 0o755); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + if err := os.WriteFile(filepath.Join(bad, "pkg.yaml"), |
| 58 | + []byte("name: broken\n"), 0o644); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + root, stdout := newRootWithAuthor(t) |
| 62 | + root.SetArgs([]string{"author", "lint", "--install", repo}) |
| 63 | + err := root.Execute() |
| 64 | + if err == nil { |
| 65 | + t.Fatalf("expected non-nil error; stdout=%q", stdout.String()) |
| 66 | + } |
| 67 | + // Exactly one finding line should appear in captured stdout: the offline |
| 68 | + // "archive must be set" (B6) finding. If --install were attempting the |
| 69 | + // broken package, additional findings would show up. |
| 70 | + findingLines := 0 |
| 71 | + for _, line := range strings.Split(strings.TrimRight(stdout.String(), "\n"), "\n") { |
| 72 | + if line != "" { |
| 73 | + findingLines++ |
| 74 | + } |
| 75 | + } |
| 76 | + if findingLines != 1 { |
| 77 | + t.Errorf("expected exactly 1 finding line (offline only); got %d: %q", findingLines, stdout.String()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// repoRoot walks up from the test working directory until it finds go.mod, |
| 82 | +// so the test can reference internal/linter/testdata regardless of which |
| 83 | +// package directory `go test` runs in. |
| 84 | +func repoRoot(t *testing.T) string { |
| 85 | + t.Helper() |
| 86 | + dir, err := os.Getwd() |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + for { |
| 91 | + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { |
| 92 | + return dir |
| 93 | + } |
| 94 | + parent := filepath.Dir(dir) |
| 95 | + if parent == dir { |
| 96 | + t.Fatal("could not locate go.mod from cwd") |
| 97 | + } |
| 98 | + dir = parent |
| 99 | + } |
| 100 | +} |
0 commit comments