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

mlange-42 / modo / 13179358125

06 Feb 2025 01:00PM CUT coverage: 74.08% (+2.0%) from 72.118%
13179358125

push

github

web-flow
More unit tests, utilities package (#211)

31 of 37 new or added lines in 6 files covered. (83.78%)

1772 of 2392 relevant lines covered (74.08%)

27.67 hits per line

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

75.28
/internal/document/processor.go
1
package document
2

3
import (
4
        "fmt"
5
        "os"
6
        "path"
7
        "strings"
8
        "text/template"
9
)
10

11
type Processor struct {
12
        Config             *Config
13
        Template           *template.Template
14
        Formatter          Formatter
15
        Docs               *Docs
16
        ExportDocs         *Docs
17
        allPaths           map[string]Named        // Full paths of all original members. Used to check whether all re-exports could be found.
18
        linkTargets        map[string]elemPath     // Mapping from full (new) member paths to link strings.
19
        linkExports        map[string]string       // Mapping from original to new member paths.
20
        linkExportsReverse map[string]*exportError // Used to check for name collisions through re-exports.
21
        renameExports      map[string]string       // Mapping from short to renamed member paths.
22
        docTests           []*docTest
23
        writer             func(file, text string) error
24
}
25

26
type exportError struct {
27
        NewPath  string
28
        OldPaths []string
29
}
30

31
type docTest struct {
32
        Name   string
33
        Path   []string
34
        Code   []string
35
        Global []string
36
}
37

38
func NewProcessor(docs *Docs, f Formatter, t *template.Template, config *Config) *Processor {
15✔
39
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
28✔
40
                return os.WriteFile(file, []byte(text), 0644)
13✔
41
        })
13✔
42
}
43

44
func NewProcessorWithWriter(docs *Docs, f Formatter, t *template.Template, config *Config, writer func(file, text string) error) *Processor {
20✔
45
        return &Processor{
20✔
46
                Config:    config,
20✔
47
                Template:  t,
20✔
48
                Formatter: f,
20✔
49
                Docs:      docs,
20✔
50
                writer:    writer,
20✔
51
        }
20✔
52
}
20✔
53

54
// PrepareDocs processes the API docs for subsequent rendering.
55
func (proc *Processor) PrepareDocs(subdir string) error {
6✔
56
        err := proc.ExtractTests(subdir)
6✔
57
        if err != nil {
6✔
58
                return err
×
59
        }
×
60
        // Re-structure according to exports.
61
        err = proc.filterPackages()
6✔
62
        if err != nil {
6✔
63
                return err
×
64
        }
×
65
        // Collect all link target paths.
66
        proc.collectPaths()
6✔
67
        if !proc.Config.UseExports {
9✔
68
                for k := range proc.linkTargets {
16✔
69
                        proc.linkExports[k] = k
13✔
70
                }
13✔
71
        }
72
        // Replaces cross-refs by placeholders.
73
        if err := proc.processLinks(proc.Docs); err != nil {
6✔
74
                return err
×
75
        }
×
76

77
        if err := proc.processTranscludes(proc.Docs); err != nil {
6✔
78
                return err
×
79
        }
×
80

81
        if proc.Config.UseExports {
9✔
82
                proc.renameAll(proc.ExportDocs.Decl)
3✔
83
        }
3✔
84

85
        return nil
6✔
86
}
87

88
func (proc *Processor) ExtractTests(subdir string) error {
6✔
89
        // Collect the paths of all (sub)-elements in the original structure.
6✔
90
        proc.collectElementPaths()
6✔
91

6✔
92
        // Extract doc tests.
6✔
93
        err := proc.extractDocTests()
6✔
94
        if err != nil {
6✔
95
                return err
×
96
        }
×
97
        if proc.Config.TestOutput != "" {
6✔
98
                fmt.Printf("Extracted %d test(s) from package %s.\n", len(proc.docTests), proc.Docs.Decl.Name)
×
99
                outPath := path.Join(proc.Config.TestOutput, subdir, proc.Docs.Decl.Name)
×
100
                err = proc.writeDocTests(outPath)
×
101
                if err != nil {
×
102
                        return err
×
103
                }
×
104
        }
105
        return nil
6✔
106
}
107

108
func (proc *Processor) WriteFile(file, text string) error {
37✔
109
        return proc.writer(file, text)
37✔
110
}
37✔
111

112
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
113
        return warnOrError(proc.Config.Strict, pattern, args...)
×
114
}
×
115

116
func (proc *Processor) addLinkExport(oldPath, newPath []string) {
80✔
117
        pNew := strings.Join(newPath, ".")
80✔
118
        pOld := strings.Join(oldPath, ".")
80✔
119
        if present, ok := proc.linkExportsReverse[pNew]; ok {
80✔
120
                present.OldPaths = append(present.OldPaths, pOld)
×
121
        } else {
80✔
122
                proc.linkExportsReverse[pNew] = &exportError{
80✔
123
                        NewPath:  pNew,
80✔
124
                        OldPaths: []string{pOld},
80✔
125
                }
80✔
126
        }
80✔
127
        proc.linkExports[pOld] = pNew
80✔
128
}
129

130
func (proc *Processor) addLinkTarget(elem Named, elPath, filePath []string, kind string, isSection bool) {
86✔
131
        proc.linkTargets[strings.Join(elPath, ".")] = elemPath{Elements: filePath, Kind: kind, IsSection: isSection}
86✔
132
}
86✔
133

134
func (proc *Processor) addElementPath(elem Named, elPath, filePath []string, kind string, isSection bool) {
98✔
135
        if isSection && kind != "package" && kind != "module" { // actually, we are want to let aliases pass
131✔
136
                return
33✔
137
        }
33✔
138
        proc.allPaths[strings.Join(elPath, ".")] = elem
65✔
139
        _ = filePath
65✔
140
}
141

142
func (proc *Processor) mkDirs(path string) error {
20✔
143
        if proc.Config.DryRun {
24✔
144
                return nil
4✔
145
        }
4✔
146
        if err := os.MkdirAll(path, os.ModePerm); err != nil && !os.IsExist(err) {
16✔
147
                return err
×
148
        }
×
149
        return nil
16✔
150
}
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