|
| 1 | +package helpdocs |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func testDocFiles() map[string]struct{} { |
| 15 | + names := []string{ |
| 16 | + "README.md", |
| 17 | + "test.md", |
| 18 | + "container.md", |
| 19 | + "container-test.md", |
| 20 | + "iac-describe.md", |
| 21 | + "redteam.md", |
| 22 | + } |
| 23 | + files := make(map[string]struct{}, len(names)) |
| 24 | + for _, name := range names { |
| 25 | + files[name] = struct{}{} |
| 26 | + } |
| 27 | + return files |
| 28 | +} |
| 29 | + |
| 30 | +func Test_helpFileName(t *testing.T) { |
| 31 | + assert.Equal(t, "container-test.md", helpFileName([]string{"container", "test"})) |
| 32 | + assert.Equal(t, "iac-describe.md", helpFileName([]string{"iac", "describe"})) |
| 33 | + assert.Equal(t, "secrets-test.md", helpFileName([]string{"secrets", "test"})) |
| 34 | +} |
| 35 | + |
| 36 | +func Test_hasUserDoc(t *testing.T) { |
| 37 | + files := testDocFiles() |
| 38 | + |
| 39 | + tests := map[string]struct { |
| 40 | + segments []string |
| 41 | + want bool |
| 42 | + }{ |
| 43 | + "empty uses readme path": {segments: []string{}, want: true}, |
| 44 | + "test command": {segments: []string{"test"}, want: true}, |
| 45 | + "container test subcommand": {segments: []string{"container", "test"}, want: true}, |
| 46 | + "iac describe subcommand": {segments: []string{"iac", "describe"}, want: true}, |
| 47 | + "unknown command": {segments: []string{"rainmaker"}, want: false}, |
| 48 | + "undocumented secrets test": {segments: []string{"secrets", "test"}, want: false}, |
| 49 | + "redteam setup walks back to parent": {segments: []string{"redteam", "setup"}, want: true}, |
| 50 | + "undocumented agent-scan": {segments: []string{"agent-scan"}, want: false}, |
| 51 | + } |
| 52 | + |
| 53 | + for name, tc := range tests { |
| 54 | + t.Run(name, func(t *testing.T) { |
| 55 | + assert.Equal(t, tc.want, hasUserDoc(files, tc.segments)) |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func Test_HasUserDoc_usesEmbeddedManifest(t *testing.T) { |
| 61 | + assert.True(t, HasUserDoc([]string{"test"})) |
| 62 | + assert.NotEmpty(t, docFiles) |
| 63 | +} |
| 64 | + |
| 65 | +func Test_manifestFileSet_stripsCRLFLineEndings(t *testing.T) { |
| 66 | + files := manifestFileSet("test.md\r\ncontainer-test.md\r\n") |
| 67 | + |
| 68 | + assert.True(t, hasUserDoc(files, []string{"test"})) |
| 69 | + assert.True(t, hasUserDoc(files, []string{"container", "test"})) |
| 70 | + assert.False(t, hasUserDoc(files, []string{"rainmaker"})) |
| 71 | +} |
| 72 | + |
| 73 | +func Test_manifestMatchesHelpCLICommands(t *testing.T) { |
| 74 | + helpDir := filepath.Join("..", "..", "..", "help", "cli-commands") |
| 75 | + entries, err := os.ReadDir(helpDir) |
| 76 | + require.NoError(t, err, "help/cli-commands must exist; run from repo root via go test ./pkg/helpdocs") |
| 77 | + |
| 78 | + var fromDisk []string |
| 79 | + for _, entry := range entries { |
| 80 | + if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".md") { |
| 81 | + continue |
| 82 | + } |
| 83 | + fromDisk = append(fromDisk, entry.Name()) |
| 84 | + } |
| 85 | + sort.Strings(fromDisk) |
| 86 | + |
| 87 | + fromManifest := manifestEntries() |
| 88 | + assert.Equal(t, fromDisk, fromManifest, |
| 89 | + "embedded manifest.txt is out of sync with help/cli-commands; run: make -C cliv2 helpdocs-manifest") |
| 90 | +} |
| 91 | + |
| 92 | +func manifestEntries() []string { |
| 93 | + entries := manifestLines(manifest) |
| 94 | + sort.Strings(entries) |
| 95 | + return entries |
| 96 | +} |
0 commit comments