| 1 |
package handlers_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"codeberg.org/miekg/dns/cmd/atomdns/atomtest" |
| 13 |
) |
| 14 |
|
| 15 |
// TestReadme parses all README.mds of the handler and checks if every example Conffile. |
| 16 |
// actually works. Each conffile snippet is only used if the language is set to 'conffile': |
| 17 |
// |
| 18 |
// ~~~ conffile |
| 19 |
// |
| 20 |
// . { |
| 21 |
// # check-this-please |
| 22 |
// } |
| 23 |
// |
| 24 |
// ~~~ |
| 25 |
// To skip the check use txt as the language. |
| 26 |
func TestReadme(t *testing.T) { |
| 27 |
dirs, err := os.ReadDir(".") |
| 28 |
if err != nil { |
| 29 |
t.Fatal(err) |
| 30 |
} |
| 31 |
for _, d := range dirs { |
| 32 |
if !d.IsDir() { |
| 33 |
continue |
| 34 |
} |
| 35 |
readme := filepath.Join(d.Name(), "README.md") |
| 36 |
t.Logf("Testing %s", readme) |
| 37 |
|
| 38 |
if err := seeAlso(readme); err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
|
| 42 |
confs, err := confFromReadme(readme) |
| 43 |
if err != nil { |
| 44 |
continue |
| 45 |
} |
| 46 |
|
| 47 |
t.Logf("Testing %s: %d snippets found", readme, len(confs)) |
| 48 |
for _, conf := range confs { |
| 49 |
_, cancel, err := atomtest.New(conf) |
| 50 |
if err != nil { |
| 51 |
if strings.Contains(err.Error(), "no such file or directory") { |
| 52 |
// parsing went far enough to this this, think we're ok |
| 53 |
continue |
| 54 |
} |
| 55 |
if strings.Contains(err.Error(), "failed to find routable IP") { |
| 56 |
// same |
| 57 |
continue |
| 58 |
} |
| 59 |
t.Errorf("Failed to start server with %s, for input %q:\n%s", readme, err, conf) |
| 60 |
continue |
| 61 |
} |
| 62 |
cancel() |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// confFromReadme parses a readme and returns all fragments that |
| 68 |
// have ~~~ conffile (or ``` conffile), it's a pretty crude parser. |
| 69 |
func confFromReadme(readme string) ([]string, error) { |
| 70 |
f, err := os.Open(readme) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
defer f.Close() |
| 75 |
|
| 76 |
s := bufio.NewScanner(f) |
| 77 |
conffile := false |
| 78 |
temp := "" |
| 79 |
confs := []string{} |
| 80 |
|
| 81 |
for s.Scan() { |
| 82 |
line := s.Text() |
| 83 |
if line == "~~~conffile" || line == "```conffile" { |
| 84 |
conffile = true |
| 85 |
continue |
| 86 |
} |
| 87 |
if line == "~~~ conffile" || line == "``` conffile" { |
| 88 |
conffile = true |
| 89 |
continue |
| 90 |
} |
| 91 |
|
| 92 |
if conffile && (line == "~~~" || line == "```") { |
| 93 |
// last line |
| 94 |
confs = append(confs, temp) |
| 95 |
temp = "" |
| 96 |
conffile = false |
| 97 |
continue |
| 98 |
} |
| 99 |
|
| 100 |
if conffile { |
| 101 |
temp += line + "\n" // read newline stripped by s.Text() |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if err := s.Err(); err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
return confs, nil |
| 109 |
} |
| 110 |
|
| 111 |
func seeAlso(readme string) error { |
| 112 |
conffile, err := os.ReadFile(readme) |
| 113 |
if err != nil { |
| 114 |
return err |
| 115 |
} |
| 116 |
if bytes.Contains(conffile, []byte("# Also See")) { |
| 117 |
return fmt.Errorf("use 'See Also' instead of 'Also See'") |
| 118 |
} |
| 119 |
return nil |
| 120 |
} |