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

mlange-42 / modo / 13103058504

02 Feb 2025 10:27PM CUT coverage: 73.324%. Remained the same
13103058504

Pull #188

github

web-flow
Merge 1abfb6f1f into 7d44f5e22
Pull Request #188: Extend user guide section on templates

1509 of 2058 relevant lines covered (73.32%)

24.78 hits per line

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

56.55
/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 {
2✔
10
        t, err := LoadTemplates(form, config.TemplateDirs...)
2✔
11
        if err != nil {
2✔
12
                return err
×
13
        }
×
14
        if !config.DryRun {
3✔
15
                proc := NewProcessor(docs, form, t, config)
1✔
16
                return renderWith(config, proc, subdir)
1✔
17
        }
1✔
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 {
×
37
        caseSensitiveSystem = !config.CaseInsensitive
×
38
        t, err := LoadTemplates(form, config.TemplateDirs...)
×
39
        if err != nil {
×
40
                return err
×
41
        }
×
42
        var proc *Processor
×
43
        if config.DryRun {
×
44
                proc = NewProcessorWithWriter(docs, form, t, config, func(file, text string) error {
×
45
                        return nil
×
46
                })
×
47
        } else {
×
48
                proc = NewProcessor(docs, form, t, config)
×
49
        }
×
50
        return proc.ExtractTests(subdir)
×
51
}
52

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

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

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

6✔
74
        var missing []missingDocs
6✔
75
        var stats missingStats
6✔
76
        if config.ReportMissing {
7✔
77
                missing = proc.Docs.Decl.CheckMissing("", &stats)
1✔
78
        }
1✔
79
        if err := proc.PrepareDocs(subdir); err != nil {
6✔
80
                return err
×
81
        }
×
82
        outPath := path.Join(config.OutputDir, subdir)
6✔
83
        if err := renderPackage(proc.ExportDocs.Decl, []string{outPath}, proc); err != nil {
6✔
84
                return err
×
85
        }
×
86
        if err := proc.Formatter.WriteAuxiliary(proc.ExportDocs.Decl, outPath, proc); err != nil {
6✔
87
                return err
×
88
        }
×
89
        if config.ReportMissing {
7✔
90
                if err := reportMissing(proc.Docs.Decl.Name, missing, stats, config.Strict); err != nil {
1✔
91
                        return err
×
92
                }
×
93
        }
94
        return nil
6✔
95
}
96

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

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

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

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

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

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

146
        return nil
8✔
147
}
148

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

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

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

173
        return nil
8✔
174
}
175

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

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

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