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

mlange-42 / modo / 13008083301

28 Jan 2025 10:08AM CUT coverage: 65.591%. Remained the same
13008083301

Pull #135

github

web-flow
Merge 28763d9be into 0ab644518
Pull Request #135: Remove templates that are no longer needed

1098 of 1674 relevant lines covered (65.59%)

31.25 hits per line

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

76.83
/document/processor.go
1
package document
2

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

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

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

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

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

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

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

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

6✔
81
        // Extract doc tests.
6✔
82
        err := proc.extractDocTests()
6✔
83
        if err != nil {
6✔
84
                return err
×
85
        }
×
86
        if proc.Config.TestOutput != "" {
6✔
87
                outPath := path.Join(proc.Config.TestOutput, subdir, proc.Docs.Decl.Name)
×
88
                err = proc.writeDocTests(outPath)
×
89
                if err != nil {
×
90
                        return err
×
91
                }
×
92
        }
93
        return nil
6✔
94
}
95

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

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

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

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

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

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