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

mlange-42 / modo / 13011891146

28 Jan 2025 01:49PM CUT coverage: 67.178%. Remained the same
13011891146

Pull #138

github

web-flow
Merge 1f947523f into f6d920347
Pull Request #138: Change code block style to github-dark

1226 of 1825 relevant lines covered (67.18%)

28.99 hits per line

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

75.9
/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]bool         // 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
        docTests           []*docTest
22
        writer             func(file, text string) error
23
}
24

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

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

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

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

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

78
func (proc *Processor) ExtractTests(subdir string) error {
6✔
79
        // Collect the paths of all (sub)-elements in the original structure.
6✔
80
        proc.collectElementPaths()
6✔
81

6✔
82
        // Extract doc tests.
6✔
83
        err := proc.extractDocTests()
6✔
84
        if err != nil {
6✔
85
                return err
×
86
        }
×
87
        if proc.Config.TestOutput != "" {
6✔
88
                fmt.Printf("Extracted %d test(s) from package %s.\n", len(proc.docTests), proc.Docs.Decl.Name)
×
89
                outPath := path.Join(proc.Config.TestOutput, subdir, proc.Docs.Decl.Name)
×
90
                err = proc.writeDocTests(outPath)
×
91
                if err != nil {
×
92
                        return err
×
93
                }
×
94
        }
95
        return nil
6✔
96
}
97

98
func (proc *Processor) WriteFile(file, text string) error {
37✔
99
        return proc.writer(file, text)
37✔
100
}
37✔
101

102
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
103
        return warnOrError(proc.Config.Strict, pattern, args...)
×
104
}
×
105

106
func (proc *Processor) addLinkExport(oldPath, newPath []string) {
74✔
107
        pNew := strings.Join(newPath, ".")
74✔
108
        pOld := strings.Join(oldPath, ".")
74✔
109
        if present, ok := proc.linkExportsReverse[pNew]; ok {
74✔
110
                present.OldPaths = append(present.OldPaths, pOld)
×
111
        } else {
74✔
112
                proc.linkExportsReverse[pNew] = &exportError{
74✔
113
                        NewPath:  pNew,
74✔
114
                        OldPaths: []string{pOld},
74✔
115
                }
74✔
116
        }
74✔
117
        proc.linkExports[pOld] = pNew
74✔
118
}
119

120
func (proc *Processor) addLinkTarget(elPath, filePath []string, kind string, isSection bool) {
80✔
121
        proc.linkTargets[strings.Join(elPath, ".")] = elemPath{Elements: filePath, Kind: kind, IsSection: isSection}
80✔
122
}
80✔
123

124
func (proc *Processor) addElementPath(elPath, filePath []string, kind string, isSection bool) {
94✔
125
        if isSection && kind != "package" && kind != "module" { // actually, we are want to let aliases pass
123✔
126
                return
29✔
127
        }
29✔
128
        proc.allPaths[strings.Join(elPath, ".")] = true
65✔
129
        _ = filePath
65✔
130
}
131

132
func (proc *Processor) mkDirs(path string) error {
20✔
133
        if proc.Config.DryRun {
24✔
134
                return nil
4✔
135
        }
4✔
136
        if err := os.MkdirAll(path, os.ModePerm); err != nil && !os.IsExist(err) {
16✔
137
                return err
×
138
        }
×
139
        return nil
16✔
140
}
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