Monorepo for Tangled
tangled.org
1package markup
2
3import "testing"
4
5func TestIsReadmeFile(t *testing.T) {
6 const (
7 fileMode = "100644"
8 execMode = "100755"
9 dirMode = "040000"
10 )
11
12 cases := []struct {
13 name string
14 mode string
15 want bool
16 }{
17 {"README.md", fileMode, true},
18 {"readme.md", fileMode, true},
19 {"ReadMe.MD", fileMode, true},
20 {"README.markdown", fileMode, true},
21 {"README.mdown", fileMode, true},
22 {"README.mkdn", fileMode, true},
23 {"README.mkd", fileMode, true},
24 {"README.txt", fileMode, true},
25 {"readme", fileMode, true},
26 {"README", execMode, true},
27
28 // regression: a directory named "readme" must not be picked up
29 // as the README blob; tree handlers used to fetch it and 503.
30 {"readme", dirMode, false},
31 {"README.md", dirMode, false},
32
33 // readme is matched by convention, not by renderable format;
34 // unsupported markup falls through to plaintext in GetFormat.
35 {"README.rst", fileMode, true},
36 {"README.org", fileMode, true},
37 {"README.asciidoc", fileMode, true},
38 {"README.foo", fileMode, true},
39
40 {"README.Music.md", fileMode, false},
41 {"readme-old", fileMode, false},
42 {"readme_legacy", fileMode, false},
43 {"notreadme.md", fileMode, false},
44 {"READMEISH", fileMode, false},
45 {"README.md", "", false},
46 {"README.md", "120000", false}, // symlink
47 }
48
49 for _, c := range cases {
50 t.Run(c.name+"/"+c.mode, func(t *testing.T) {
51 if got := IsReadmeFile(c.name, c.mode); got != c.want {
52 t.Errorf("IsReadmeFile(%q, %q) = %v, want %v", c.name, c.mode, got, c.want)
53 }
54 })
55 }
56}
57
58func TestFileTypePatterns(t *testing.T) {
59 cases := []struct {
60 format Format
61 filename string
62 want bool
63 }{
64 {FormatMarkdown, "x.md", true},
65 {FormatMarkdown, "x.MARKDOWN", true},
66 {FormatMarkdown, "x.mkdn", true},
67 {FormatMarkdown, "x.mkd", true},
68 {FormatMarkdown, "x.mdown", true},
69 {FormatMarkdown, "x.txt", false},
70 {FormatMarkdown, "x.rst", false},
71 }
72
73 for _, c := range cases {
74 t.Run(string(c.format)+"/"+c.filename, func(t *testing.T) {
75 p, ok := FileTypePatterns[c.format]
76 if !ok {
77 t.Fatalf("FileTypePatterns[%q] missing", c.format)
78 }
79 if got := p.MatchString(c.filename); got != c.want {
80 t.Errorf("FileTypePatterns[%q].MatchString(%q) = %v, want %v", c.format, c.filename, got, c.want)
81 }
82 })
83 }
84}
85
86func TestGetFormat(t *testing.T) {
87 cases := []struct {
88 filename string
89 want Format
90 }{
91 {"x.md", FormatMarkdown},
92 {"x.MARKDOWN", FormatMarkdown},
93 {"x.txt", FormatText},
94 {"x.rs", FormatText}, // unknown -> default
95 {"noext", FormatText},
96 }
97
98 for _, c := range cases {
99 t.Run(c.filename, func(t *testing.T) {
100 if got := GetFormat(c.filename); got != c.want {
101 t.Errorf("GetFormat(%q) = %q, want %q", c.filename, got, c.want)
102 }
103 })
104 }
105}