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

mlange-42 / modo / 13180131511

06 Feb 2025 01:42PM CUT coverage: 71.952% (-2.1%) from 74.08%
13180131511

Pull #212

github

web-flow
Merge 3f3f63e16 into 1566231bf
Pull Request #212: Add tests for CLI commands

30 of 46 new or added lines in 5 files covered. (65.22%)

2278 of 3166 relevant lines covered (71.95%)

40.24 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 {
4✔
10
        t, err := LoadTemplates(form, config.SourceURLs[strings.ToLower(docs.Decl.Name)], config.TemplateDirs...)
4✔
11
        if err != nil {
4✔
12
                return err
×
13
        }
×
14
        if !config.DryRun {
7✔
15
                proc := NewProcessor(docs, form, t, config)
3✔
16
                return renderWith(config, proc, subdir)
3✔
17
        }
3✔
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 {
5✔
54
        caseSensitiveSystem = !config.CaseInsensitive
5✔
55

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

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

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

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

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

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

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

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

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

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

147
        return nil
12✔
148
}
149

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

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

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

174
        return nil
12✔
175
}
176

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

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

203
func reportMissing(pkg string, missing []missingDocs, stats missingStats, strict bool) error {
3✔
204
        if len(missing) == 0 {
3✔
205
                fmt.Printf("Docstring coverage of package %s: 100%%\n", pkg)
×
206
                return nil
×
207
        }
×
208
        for _, m := range missing {
42✔
209
                fmt.Printf("WARNING: missing %s in %s\n", m.What, m.Who)
39✔
210
        }
39✔
211
        fmt.Printf("Docstring coverage package %s: %.1f%%\n", pkg, 100.0*float64(stats.Total-stats.Missing)/float64(stats.Total))
3✔
212
        if strict {
3✔
213
                return fmt.Errorf("missing docstrings in strict mode")
×
214
        }
×
215
        return nil
3✔
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