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

mlange-42 / modo / 12969829563

26 Jan 2025 12:39AM CUT coverage: 60.763% (-0.8%) from 61.534%
12969829563

Pull #118

github

web-flow
Merge 1f680c59c into 3f25a97de
Pull Request #118: Allow a folder as input, to process recursively

11 of 41 new or added lines in 5 files covered. (26.83%)

3 existing lines in 3 files now uncovered.

988 of 1626 relevant lines covered (60.76%)

28.99 hits per line

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

74.12
/document/processor.go
1
package document
2

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

12
type Processor struct {
13
        Config             *Config
14
        Template           *template.Template
15
        Formatter          Formatter
16
        Docs               *Docs
17
        ExportDocs         *Docs
18
        allPaths           map[string]bool         // Full paths of all original members. Used to check whether all re-exports could be found.
19
        linkTargets        map[string]elemPath     // Mapping from full (new) member paths to link strings.
20
        linkExports        map[string]string       // Mapping from original to new member paths.
21
        linkExportsReverse map[string]*exportError // Used to check for name collisions through re-exports.
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 {
10✔
39
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
19✔
40
                return os.WriteFile(file, []byte(text), 0644)
9✔
41
        })
9✔
42
}
43

44
func NewProcessorWithWriter(docs *Docs, f Formatter, t *template.Template, config *Config, writer func(file, text string) error) *Processor {
15✔
45
        return &Processor{
15✔
46
                Config:    config,
15✔
47
                Template:  t,
15✔
48
                Formatter: f,
15✔
49
                Docs:      docs,
15✔
50
                writer:    writer,
15✔
51
        }
15✔
52
}
15✔
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
        return nil
6✔
77
}
78

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

6✔
83
        // Extract doc tests.
6✔
84
        err := proc.extractDocTests()
6✔
85
        if err != nil {
6✔
86
                return err
×
87
        }
×
88
        if proc.Config.TestOutput != "" {
6✔
NEW
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 {
33✔
99
        return proc.writer(file, text)
33✔
100
}
33✔
101

102
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
103
        if proc.Config.Strict {
×
104
                return fmt.Errorf(pattern, args...)
×
105
        }
×
106
        log.Printf("WARNING: "+pattern+"\n", args...)
×
107
        return nil
×
108
}
109

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

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

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

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