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

mlange-42 / modo / 13103549969

02 Feb 2025 11:37PM CUT coverage: 73.186% (-0.1%) from 73.324%
13103549969

Pull #189

github

web-flow
Merge 45bd4a4ee into 85ef42e0b
Pull Request #189: Parse and register exports-as renames

21 of 32 new or added lines in 1 file covered. (65.63%)

1 existing line in 1 file now uncovered.

1523 of 2081 relevant lines covered (73.19%)

24.73 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
        linkExportsRename  map[string]string
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 {
14✔
39
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
27✔
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 {
19✔
45
        return &Processor{
19✔
46
                Config:    config,
19✔
47
                Template:  t,
19✔
48
                Formatter: f,
19✔
49
                Docs:      docs,
19✔
50
                writer:    writer,
19✔
51
        }
19✔
52
}
19✔
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✔
89
                fmt.Printf("Extracted %d test(s) from package %s.\n", len(proc.docTests), proc.Docs.Decl.Name)
×
90
                outPath := path.Join(proc.Config.TestOutput, subdir, proc.Docs.Decl.Name)
×
91
                err = proc.writeDocTests(outPath)
×
92
                if err != nil {
×
93
                        return err
×
94
                }
×
95
        }
96
        return nil
6✔
97
}
98

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

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

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

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

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

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