• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 13184787090

06 Feb 2025 05:48PM CUT coverage: 74.117%. Remained the same
13184787090

Pull #215

github

web-flow
Merge c7aae58fb into c7515e1a8
Pull Request #215: Add guide section on Modo's doc-testing vs. `mojo test`

2351 of 3172 relevant lines covered (74.12%)

48.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.5
/internal/document/render.go
1
package document
2

3
import (
4
        "fmt"
5
        "path"
6
        "strings"
7
)
8

9
func Render(docs *Docs, config *Config, form Formatter, subdir string) error {
5✔
10
        t, err := LoadTemplates(form, config.SourceURLs[strings.ToLower(docs.Decl.Name)], config.TemplateDirs...)
5✔
11
        if err != nil {
5✔
12
                return err
×
13
        }
×
14
        if !config.DryRun {
9✔
15
                proc := NewProcessor(docs, form, t, config)
4✔
16
                return renderWith(config, proc, subdir)
4✔
17
        }
4✔
18

19
        files := []string{}
1✔
20
        proc := NewProcessorWithWriter(docs, form, t, config, func(file, text string) error {
10✔
21
                files = append(files, file)
9✔
22
                return nil
9✔
23
        })
9✔
24
        err = renderWith(config, proc, subdir)
1✔
25
        if err != nil {
1✔
26
                return err
×
27
        }
×
28

29
        fmt.Println("Dry-run. Would write these files:")
1✔
30
        for _, f := range files {
10✔
31
                fmt.Println(f)
9✔
32
        }
9✔
33
        return nil
1✔
34
}
35

36
func ExtractTests(docs *Docs, config *Config, form Formatter, subdir string) error {
1✔
37
        caseSensitiveSystem = !config.CaseInsensitive
1✔
38
        t, err := LoadTemplates(form, config.SourceURLs[strings.ToLower(docs.Decl.Name)], config.TemplateDirs...)
1✔
39
        if err != nil {
1✔
40
                return err
×
41
        }
×
42
        var proc *Processor
1✔
43
        if config.DryRun {
1✔
44
                proc = NewProcessorWithWriter(docs, form, t, config, func(file, text string) error {
×
45
                        return nil
×
46
                })
×
47
        } else {
1✔
48
                proc = NewProcessor(docs, form, t, config)
1✔
49
        }
1✔
50
        return proc.ExtractTests(subdir)
1✔
51
}
52

53
func ExtractTestsMarkdown(config *Config, form Formatter, baseDir string, build bool) error {
6✔
54
        caseSensitiveSystem = !config.CaseInsensitive
6✔
55

6✔
56
        t, err := LoadTemplates(form, "", config.TemplateDirs...)
6✔
57
        if err != nil {
6✔
58
                return err
×
59
        }
×
60
        var proc *Processor
6✔
61
        if config.DryRun {
6✔
62
                proc = NewProcessorWithWriter(nil, form, t, config, func(file, text string) error {
×
63
                        return nil
×
64
                })
×
65
        } else {
6✔
66
                proc = NewProcessor(nil, form, t, config)
6✔
67
        }
6✔
68
        return proc.extractDocTestsMarkdown(baseDir, build)
6✔
69
}
70

71
func renderWith(config *Config, proc *Processor, subdir string) error {
9✔
72
        caseSensitiveSystem = !config.CaseInsensitive
9✔
73

9✔
74
        if err := proc.PrepareDocs(subdir); err != nil {
9✔
75
                return err
×
76
        }
×
77
        var missing []missingDocs
9✔
78
        var stats missingStats
9✔
79
        if config.ReportMissing {
13✔
80
                missing = proc.Docs.Decl.CheckMissing("", &stats)
4✔
81
        }
4✔
82

83
        outPath := path.Join(config.OutputDir, subdir)
9✔
84
        if err := renderPackage(proc.ExportDocs.Decl, []string{outPath}, proc); err != nil {
9✔
85
                return err
×
86
        }
×
87
        if err := proc.Formatter.WriteAuxiliary(proc.ExportDocs.Decl, outPath, proc); err != nil {
9✔
88
                return err
×
89
        }
×
90
        if config.ReportMissing {
13✔
91
                if err := reportMissing(proc.Docs.Decl.Name, missing, stats, config.Strict); err != nil {
4✔
92
                        return err
×
93
                }
×
94
        }
95
        return nil
9✔
96
}
97

98
func renderElement(data interface {
99
        Named
100
        Kinded
101
}, proc *Processor) (string, error) {
62✔
102
        b := strings.Builder{}
62✔
103
        err := proc.Template.ExecuteTemplate(&b, data.GetKind()+".md", data)
62✔
104
        if err != nil {
62✔
105
                return "", err
×
106
        }
×
107
        return proc.Formatter.ProcessMarkdown(data, b.String(), proc)
62✔
108
}
109

110
func renderPackage(p *Package, dir []string, proc *Processor) error {
14✔
111
        newDir := appendNew(dir, p.GetFileName())
14✔
112
        pkgPath := path.Join(newDir...)
14✔
113
        if err := proc.mkDirs(pkgPath); err != nil {
14✔
114
                return err
×
115
        }
×
116

117
        for _, pkg := range p.Packages {
19✔
118
                if err := renderPackage(pkg, newDir, proc); err != nil {
5✔
119
                        return err
×
120
                }
×
121
        }
122

123
        for _, mod := range p.Modules {
28✔
124
                if err := renderModule(mod, newDir, proc); err != nil {
14✔
125
                        return err
×
126
                }
×
127
        }
128

129
        if err := renderList(p.Structs, newDir, proc); err != nil {
14✔
130
                return err
×
131
        }
×
132
        if err := renderList(p.Traits, newDir, proc); err != nil {
14✔
133
                return err
×
134
        }
×
135
        if err := renderList(p.Functions, newDir, proc); err != nil {
14✔
136
                return err
×
137
        }
×
138

139
        text, err := renderElement(p, proc)
14✔
140
        if err != nil {
14✔
141
                return err
×
142
        }
×
143
        if err := linkAndWrite(text, newDir, len(newDir), "package", proc); err != nil {
14✔
144
                return err
×
145
        }
×
146

147
        return nil
14✔
148
}
149

150
func renderModule(mod *Module, dir []string, proc *Processor) error {
14✔
151
        newDir := appendNew(dir, mod.GetFileName())
14✔
152
        if err := proc.mkDirs(path.Join(newDir...)); err != nil {
14✔
153
                return err
×
154
        }
×
155

156
        if err := renderList(mod.Structs, newDir, proc); err != nil {
14✔
157
                return err
×
158
        }
×
159
        if err := renderList(mod.Traits, newDir, proc); err != nil {
14✔
160
                return err
×
161
        }
×
162
        if err := renderList(mod.Functions, newDir, proc); err != nil {
14✔
163
                return err
×
164
        }
×
165

166
        text, err := renderElement(mod, proc)
14✔
167
        if err != nil {
14✔
168
                return err
×
169
        }
×
170
        if err := linkAndWrite(text, newDir, len(newDir), "module", proc); err != nil {
14✔
171
                return err
×
172
        }
×
173

174
        return nil
14✔
175
}
176

177
func renderList[T interface {
178
        Named
179
        Kinded
180
}](list []T, dir []string, proc *Processor) error {
84✔
181
        for _, elem := range list {
116✔
182
                newDir := appendNew(dir, elem.GetFileName())
32✔
183
                text, err := renderElement(elem, proc)
32✔
184
                if err != nil {
32✔
185
                        return err
×
186
                }
×
187
                if err := linkAndWrite(text, newDir, len(dir), elem.GetKind(), proc); err != nil {
32✔
188
                        return err
×
189
                }
×
190
        }
191
        return nil
84✔
192
}
193

194
func linkAndWrite(text string, dir []string, modElems int, kind string, proc *Processor) error {
60✔
195
        text, err := proc.ReplacePlaceholders(text, dir[1:], modElems-1)
60✔
196
        if err != nil {
60✔
197
                return err
×
198
        }
×
199
        outFile := proc.Formatter.ToFilePath(path.Join(dir...), kind)
60✔
200
        return proc.WriteFile(outFile, text)
60✔
201
}
202

203
func reportMissing(pkg string, missing []missingDocs, stats missingStats, strict bool) error {
4✔
204
        if len(missing) == 0 {
4✔
205
                fmt.Printf("Docstring coverage of package %s: 100%%\n", pkg)
×
206
                return nil
×
207
        }
×
208
        for _, m := range missing {
56✔
209
                fmt.Printf("WARNING: missing %s in %s\n", m.What, m.Who)
52✔
210
        }
52✔
211
        fmt.Printf("Docstring coverage package %s: %.1f%%\n", pkg, 100.0*float64(stats.Total-stats.Missing)/float64(stats.Total))
4✔
212
        if strict {
4✔
213
                return fmt.Errorf("missing docstrings in strict mode")
×
214
        }
×
215
        return nil
4✔
216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc