aboutsummaryrefslogtreecommitdiff
path: root/test/readme_test.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2020-01-31 12:37:24 +0100
committerGravatar GitHub <noreply@github.com> 2020-01-31 11:37:24 +0000
commitf77c6e55bf735ab3e08feaa8dbbf4a995512a992 (patch)
tree4673f537d44dc33d3ad8645dd5963cf2536605de /test/readme_test.go
parent510f2c503da2d8a428c656f5240481b2f2f4b718 (diff)
downloadcoredns-f77c6e55bf735ab3e08feaa8dbbf4a995512a992.tar.gz
coredns-f77c6e55bf735ab3e08feaa8dbbf4a995512a992.tar.zst
coredns-f77c6e55bf735ab3e08feaa8dbbf4a995512a992.zip
presubmit: test README.md sections (#3644)
Automatically submitted.
Diffstat (limited to 'test/readme_test.go')
-rw-r--r--test/readme_test.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/test/readme_test.go b/test/readme_test.go
index b857def12..38fbcc0e2 100644
--- a/test/readme_test.go
+++ b/test/readme_test.go
@@ -2,10 +2,12 @@ package test
import (
"bufio"
+ "fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
+ "strings"
"testing"
"github.com/coredns/coredns/core/dnsserver"
@@ -39,6 +41,9 @@ PrivateKey: f03VplaIEA+KHI9uizlemUSbUJH86hPBPjmcUninPoM=
// # check-this-please
// }
// ~~~
+//
+// While we're at it - we also check the README.md itself. It should at least have the sections:
+// Name, Description, Syntax and Examples. See plugin.md for more details.
func TestReadme(t *testing.T) {
port := 30053
caddy.Quiet = true
@@ -59,6 +64,10 @@ func TestReadme(t *testing.T) {
readme := filepath.Join(middle, d.Name())
readme = filepath.Join(readme, "README.md")
+ if err := sectionsFromReadme(readme); err != nil {
+ t.Fatal(err)
+ }
+
inputs, err := corefileFromReadme(readme)
if err != nil {
continue
@@ -118,6 +127,47 @@ func corefileFromReadme(readme string) ([]*Input, error) {
return input, nil
}
+// sectionsFromReadme returns an error if the readme doesn't contains all
+// mandatory sections. The check is basic, as we match each line, this mostly
+// works, because markdown is such a simple format.
+// We want: Name, Description, Syntax, Examples - in this order.
+func sectionsFromReadme(readme string) error {
+ f, err := os.Open(readme)
+ if err != nil {
+ return nil // don't error when we can read the file
+ }
+ defer f.Close()
+
+ section := 0
+ s := bufio.NewScanner(f)
+ for s.Scan() {
+ line := s.Text()
+ switch section {
+ case 0:
+ if strings.HasPrefix(line, "## Name") {
+ section++
+ }
+ case 1:
+ if strings.HasPrefix(line, "## Description") {
+ section++
+ }
+ case 2:
+ if strings.HasPrefix(line, "## Syntax") {
+ section++
+ }
+ case 3:
+ if strings.HasPrefix(line, "## Examples") {
+ section++
+ }
+ }
+ }
+ if section != 4 {
+ return fmt.Errorf("Sections incomplete or ordered wrong: %q, want (at least): Name, Descripion, Syntax and Examples", readme)
+ }
+ return nil
+
+}
+
func create(c map[string]string) {
for name, content := range c {
ioutil.WriteFile(name, []byte(content), 0644)