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

mlange-42 / modo / 12968560435

25 Jan 2025 09:23PM CUT coverage: 61.572%. Remained the same
12968560435

push

github

web-flow
Docs for document tree processing (#115)

2 of 2 new or added lines in 1 file covered. (100.0%)

987 of 1603 relevant lines covered (61.57%)

29.41 hits per line

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

75.0
/document/processor.go
1
package document
2

3
import (
4
        "fmt"
5
        "log"
6
        "os"
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 {
10✔
38
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
19✔
39
                return os.WriteFile(file, []byte(text), 0644)
9✔
40
        })
9✔
41
}
42

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

53
// PrepareDocs processes the API docs for subsequent rendering.
54
func (proc *Processor) PrepareDocs() error {
6✔
55
        err := proc.ExtractTests()
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() 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
                err = proc.writeDocTests(proc.Config.TestOutput)
×
89
                if err != nil {
×
90
                        return err
×
91
                }
×
92
        }
93
        return nil
6✔
94
}
95

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

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

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

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

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

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