11package helpdocs
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
6- "sort"
7- "strings"
87 "testing"
98
109 "github.com/stretchr/testify/assert"
1110 "github.com/stretchr/testify/require"
1211)
1312
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" ,
13+ const cliCommandsSourceDir = "../../../help/cli-commands"
14+
15+ var testCLICommandDocFiles map [string ]struct {}
16+
17+ func TestMain (m * testing.M ) {
18+ teardown , err := setupCLICommandsForTest ()
19+ if err != nil {
20+ fmt .Fprintf (os .Stderr , "helpdocs TestMain setup: %v\n " , err )
21+ os .Exit (1 )
2222 }
23- files := make (map [string ]struct {}, len (names ))
24- for _ , name := range names {
25- files [name ] = struct {}{}
23+
24+ testCLICommandDocFiles , err = loadCLICommandDocFilesFromDisk ()
25+ if err != nil {
26+ teardown ()
27+ fmt .Fprintf (os .Stderr , "helpdocs TestMain load doc files: %v\n " , err )
28+ os .Exit (1 )
2629 }
27- return files
30+
31+ code := m .Run ()
32+ teardown ()
33+ os .Exit (code )
34+ }
35+
36+ // setupCLICommandsForTest copies GitBook-synced help markdown into the embed tree.
37+ // Teardown removes copied .md files and leaves the committed do-not-delete placeholder.
38+ func setupCLICommandsForTest () (func (), error ) {
39+ embedDir , err := filepath .Abs (cliCommandsDir )
40+ if err != nil {
41+ return nil , err
42+ }
43+
44+ sourceDir , err := filepath .Abs (cliCommandsSourceDir )
45+ if err != nil {
46+ return nil , err
47+ }
48+
49+ sourceMatches , err := filepath .Glob (filepath .Join (sourceDir , "*.md" ))
50+ if err != nil {
51+ return nil , err
52+ }
53+ if len (sourceMatches ) == 0 {
54+ return nil , fmt .Errorf ("expected .md files in %s" , sourceDir )
55+ }
56+
57+ for _ , match := range sourceMatches {
58+ data , err := os .ReadFile (match )
59+ if err != nil {
60+ return nil , err
61+ }
62+ dest := filepath .Join (embedDir , filepath .Base (match ))
63+ if err := os .WriteFile (dest , data , 0o644 ); err != nil {
64+ return nil , err
65+ }
66+ }
67+
68+ return func () {
69+ matches , err := filepath .Glob (filepath .Join (embedDir , "*.md" ))
70+ if err != nil {
71+ fmt .Fprintf (os .Stderr , "helpdocs test teardown: %v\n " , err )
72+ return
73+ }
74+ for _ , match := range matches {
75+ if err := os .Remove (match ); err != nil {
76+ fmt .Fprintf (os .Stderr , "helpdocs test teardown: remove %s: %v\n " , match , err )
77+ }
78+ }
79+ }, nil
80+ }
81+
82+ func loadCLICommandDocFilesFromDisk () (map [string ]struct {}, error ) {
83+ embedDir , err := filepath .Abs (cliCommandsDir )
84+ if err != nil {
85+ return nil , err
86+ }
87+
88+ return docFilesFromEmbed (os .DirFS (embedDir ), "." ), nil
2889}
2990
3091func Test_helpFileName (t * testing.T ) {
@@ -33,9 +94,7 @@ func Test_helpFileName(t *testing.T) {
3394 assert .Equal (t , "secrets-test.md" , helpFileName ([]string {"secrets" , "test" }))
3495}
3596
36- func Test_hasUserDoc (t * testing.T ) {
37- files := testDocFiles ()
38-
97+ func Test_HasUserDoc (t * testing.T ) {
3998 tests := map [string ]struct {
4099 segments []string
41100 want bool
@@ -52,45 +111,28 @@ func Test_hasUserDoc(t *testing.T) {
52111
53112 for name , tc := range tests {
54113 t .Run (name , func (t * testing.T ) {
55- assert .Equal (t , tc .want , hasUserDoc (files , tc .segments ))
114+ assert .Equal (t , tc .want , hasUserDoc (tc .segments , testCLICommandDocFiles ))
56115 })
57116 }
58117}
59118
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 \n container-test.md\r \n " )
119+ func Test_HasUserDoc_usesEmbeddedCLICommands (t * testing.T ) {
120+ require .NotEmpty (t , testCLICommandDocFiles )
121+ assert .Contains (t , testCLICommandDocFiles , "test.md" )
122+ assert .True (t , hasUserDoc ([]string {"test" }, testCLICommandDocFiles ))
67123
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" }))
124+ if len (docFiles ) > 0 {
125+ assert .Equal (t , testCLICommandDocFiles , docFiles )
126+ assert .True (t , HasUserDoc ([]string {"test" }))
127+ }
71128}
72129
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" )
130+ func Test_docFilesFromEmbed (t * testing.T ) {
131+ assert . True ( t , hasUserDoc ([] string { "test" }, testCLICommandDocFiles ) )
132+ assert . True ( t , hasUserDoc ([] string { "container" , "test" }, testCLICommandDocFiles ) )
133+ assert . False (t , hasUserDoc ([] string { "rainmaker" }, testCLICommandDocFiles ) )
77134
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 ())
135+ if len (docFiles ) > 0 {
136+ assert .Equal (t , testCLICommandDocFiles , docFilesFromEmbed (cliCommands , cliCommandsDir ))
84137 }
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
96138}
0 commit comments