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-old", fileMode, true},
38 {"readme_legacy", fileMode, true},
39
40 {"notreadme.md", fileMode, false},
41 {"READMEISH", fileMode, false},
42 {"README.md", "", false},
43 {"README.md", "120000", false}, // symlink
44 }
45
46 for _, c := range cases {
47 t.Run(c.name+"/"+c.mode, func(t *testing.T) {
48 if got := IsReadmeFile(c.name, c.mode); got != c.want {
49 t.Errorf("IsReadmeFile(%q, %q) = %v, want %v", c.name, c.mode, got, c.want)
50 }
51 })
52 }
53}
54
55func TestFileTypePatterns(t *testing.T) {
56 cases := []struct {
57 format Format
58 filename string
59 want bool
60 }{
61 {FormatMarkdown, "x.md", true},
62 {FormatMarkdown, "x.MARKDOWN", true},
63 {FormatMarkdown, "x.mkdn", true},
64 {FormatMarkdown, "x.mkd", true},
65 {FormatMarkdown, "x.mdown", true},
66 {FormatMarkdown, "x.txt", false},
67 {FormatMarkdown, "x.rst", false},
68 }
69
70 for _, c := range cases {
71 t.Run(string(c.format)+"/"+c.filename, func(t *testing.T) {
72 p, ok := FileTypePatterns[c.format]
73 if !ok {
74 t.Fatalf("FileTypePatterns[%q] missing", c.format)
75 }
76 if got := p.MatchString(c.filename); got != c.want {
77 t.Errorf("FileTypePatterns[%q].MatchString(%q) = %v, want %v", c.format, c.filename, got, c.want)
78 }
79 })
80 }
81}
82
83func TestGetFormat(t *testing.T) {
84 cases := []struct {
85 filename string
86 want Format
87 }{
88 {"x.md", FormatMarkdown},
89 {"x.MARKDOWN", FormatMarkdown},
90 {"x.txt", FormatText},
91 {"x.rs", FormatText}, // unknown -> default
92 {"noext", FormatText},
93 }
94
95 for _, c := range cases {
96 t.Run(c.filename, func(t *testing.T) {
97 if got := GetFormat(c.filename); got != c.want {
98 t.Errorf("GetFormat(%q) = %q, want %q", c.filename, got, c.want)
99 }
100 })
101 }
102}