|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRunMissingFileFlag(t *testing.T) { |
| 12 | + var stdout, stderr bytes.Buffer |
| 13 | + code := run([]string{}, &stdout, &stderr) |
| 14 | + if code != 2 { |
| 15 | + t.Fatalf("expected exit code 2, got %d", code) |
| 16 | + } |
| 17 | + if !strings.Contains(stderr.String(), "requires -f") { |
| 18 | + t.Fatalf("expected usage message on stderr, got %q", stderr.String()) |
| 19 | + } |
| 20 | + if stdout.Len() != 0 { |
| 21 | + t.Fatalf("expected empty stdout, got %q", stdout.String()) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func TestRunInvalidFlag(t *testing.T) { |
| 26 | + var stdout, stderr bytes.Buffer |
| 27 | + code := run([]string{"--nonexistent"}, &stdout, &stderr) |
| 28 | + if code != 2 { |
| 29 | + t.Fatalf("expected exit code 2, got %d", code) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestRunNonexistentFile(t *testing.T) { |
| 34 | + var stdout, stderr bytes.Buffer |
| 35 | + code := run([]string{"-f", "/nonexistent/results.yaml"}, &stdout, &stderr) |
| 36 | + if code != 1 { |
| 37 | + t.Fatalf("expected exit code 1, got %d", code) |
| 38 | + } |
| 39 | + if !strings.Contains(stderr.String(), "read results file") { |
| 40 | + t.Fatalf("expected file error on stderr, got %q", stderr.String()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestRunInvalidYAML(t *testing.T) { |
| 45 | + filePath := writeTestFile(t, "not valid yaml: [") |
| 46 | + var stdout, stderr bytes.Buffer |
| 47 | + code := run([]string{"-f", filePath}, &stdout, &stderr) |
| 48 | + if code != 1 { |
| 49 | + t.Fatalf("expected exit code 1, got %d", code) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestRunValidResults(t *testing.T) { |
| 54 | + filePath := writeTestFile(t, minimalResultsYAML()) |
| 55 | + var stdout, stderr bytes.Buffer |
| 56 | + code := run([]string{"-f", filePath, "-badge", "baseline-1"}, &stdout, &stderr) |
| 57 | + if code != 0 { |
| 58 | + t.Fatalf("expected exit code 0, got %d; stderr: %s", code, stderr.String()) |
| 59 | + } |
| 60 | + output := stdout.String() |
| 61 | + if !strings.Contains(output, "bestpractices.dev") { |
| 62 | + t.Fatalf("expected bestpractices.dev URL in stdout, got %q", output) |
| 63 | + } |
| 64 | + if !strings.Contains(stderr.String(), "Open this link") { |
| 65 | + t.Fatalf("expected user instruction on stderr, got %q", stderr.String()) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestRunNoJustifications(t *testing.T) { |
| 70 | + filePath := writeTestFile(t, minimalResultsYAML()) |
| 71 | + var stdout, stderr bytes.Buffer |
| 72 | + code := run([]string{"-f", filePath, "-justifications=false"}, &stdout, &stderr) |
| 73 | + if code != 0 { |
| 74 | + t.Fatalf("expected exit code 0, got %d; stderr: %s", code, stderr.String()) |
| 75 | + } |
| 76 | + if strings.Contains(stdout.String(), "justification=") { |
| 77 | + t.Fatalf("expected no justifications in URL, got %q", stdout.String()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestRunInvalidBadge(t *testing.T) { |
| 82 | + filePath := writeTestFile(t, minimalResultsYAML()) |
| 83 | + var stdout, stderr bytes.Buffer |
| 84 | + code := run([]string{"-f", filePath, "-badge", "gold"}, &stdout, &stderr) |
| 85 | + if code != 1 { |
| 86 | + t.Fatalf("expected exit code 1, got %d", code) |
| 87 | + } |
| 88 | + if !strings.Contains(stderr.String(), "invalid badge") { |
| 89 | + t.Fatalf("expected invalid badge error, got %q", stderr.String()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func writeTestFile(t *testing.T, content string) string { |
| 94 | + t.Helper() |
| 95 | + filePath := filepath.Join(t.TempDir(), "results.yaml") |
| 96 | + if err := os.WriteFile(filePath, []byte(content), 0o600); err != nil { |
| 97 | + t.Fatalf("write test file: %v", err) |
| 98 | + } |
| 99 | + return filePath |
| 100 | +} |
| 101 | + |
| 102 | +func minimalResultsYAML() string { |
| 103 | + return `payload: |
| 104 | + config: |
| 105 | + vars: |
| 106 | + owner: acme |
| 107 | + repo: rocket |
| 108 | +evaluation-suites: |
| 109 | + - control-evaluations: |
| 110 | + evaluations: |
| 111 | + - assessment-logs: |
| 112 | + - requirement: |
| 113 | + entry-id: OSPS-AC-01.01 |
| 114 | + result: Passed |
| 115 | + message: MFA required |
| 116 | + applicability: |
| 117 | + - Maturity Level 1 |
| 118 | +` |
| 119 | +} |
0 commit comments