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

mlange-42 / modo / 13157872700

05 Feb 2025 12:57PM CUT coverage: 72.707%. Remained the same
13157872700

Pull #204

github

web-flow
Merge 23e2e88a4 into 8c386a1e1
Pull Request #204: Move all code to package `internal`

1649 of 2268 relevant lines covered (72.71%)

26.89 hits per line

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

76.74
/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]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
        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 proc.Config.UseExports {
9✔
78
                proc.renameAll(proc.ExportDocs.Decl)
3✔
79
        }
3✔
80

81
        return nil
6✔
82
}
83

84
func (proc *Processor) ExtractTests(subdir string) error {
6✔
85
        // Collect the paths of all (sub)-elements in the original structure.
6✔
86
        proc.collectElementPaths()
6✔
87

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

104
func (proc *Processor) WriteFile(file, text string) error {
37✔
105
        return proc.writer(file, text)
37✔
106
}
37✔
107

108
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
109
        return warnOrError(proc.Config.Strict, pattern, args...)
×
110
}
×
111

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

126
func (proc *Processor) addLinkTarget(elPath, filePath []string, kind string, isSection bool) {
82✔
127
        proc.linkTargets[strings.Join(elPath, ".")] = elemPath{Elements: filePath, Kind: kind, IsSection: isSection}
82✔
128
}
82✔
129

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

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