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

mlange-42 / modo / 12899273857

22 Jan 2025 01:27AM UTC coverage: 62.587% (-0.9%) from 63.46%
12899273857

Pull #91

github

web-flow
Merge 4d4abc2cd into 74accf922
Pull Request #91: CLI rework for sub-commands

19 of 44 new or added lines in 5 files covered. (43.18%)

5 existing lines in 1 file now uncovered.

987 of 1577 relevant lines covered (62.59%)

29.89 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 Config struct {
12
        InputFiles      []string `mapstructure:"input" yaml:"input"`
13
        OutputDir       string   `mapstructure:"output" yaml:"output"`
14
        TestOutput      string   `mapstructure:"tests" yaml:"tests"`
15
        RenderFormat    string   `mapstructure:"format" yaml:"format"`
16
        UseExports      bool     `mapstructure:"exports" yaml:"exports"`
17
        ShortLinks      bool     `mapstructure:"short-links" yaml:"short-links"`
18
        Strict          bool     `mapstructure:"strict" yaml:"strict"`
19
        DryRun          bool     `mapstructure:"dry-run" yaml:"dry-run"`
20
        CaseInsensitive bool     `mapstructure:"case-insensitive" yaml:"case-insensitive"`
21
        Bare            bool     `mapstructure:"bare"`
22
        TemplateDirs    []string `mapstructure:"templates" yaml:"templates"`
23
        PreRun          []string `mapstructure:"pre-run" yaml:"pre-run"`
24
        PreBuild        []string `mapstructure:"pre-build" yaml:"pre-build"`
25
        PreTest         []string `mapstructure:"pre-test" yaml:"pre-test"`
26
        PostTest        []string `mapstructure:"post-test" yaml:"post-test"`
27
        PostBuild       []string `mapstructure:"post-build" yaml:"post-build"`
28
        PostRun         []string `mapstructure:"post-run" yaml:"post-run"`
29
}
30

31
type Processor struct {
32
        Config             *Config
33
        Template           *template.Template
34
        Formatter          Formatter
35
        Docs               *Docs
36
        ExportDocs         *Docs
37
        allPaths           map[string]bool
38
        linkTargets        map[string]elemPath
39
        linkExports        map[string]string
40
        linkExportsReverse map[string]*exportError
41
        docTests           []*docTest
42
        writer             func(file, text string) error
43
}
44

45
type exportError struct {
46
        NewPath  string
47
        OldPaths []string
48
}
49

50
type docTest struct {
51
        Name   string
52
        Path   []string
53
        Code   []string
54
        Global []string
55
}
56

57
func NewProcessor(docs *Docs, f Formatter, t *template.Template, config *Config) *Processor {
10✔
58
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
19✔
59
                return os.WriteFile(file, []byte(text), 0644)
9✔
60
        })
9✔
61
}
62

63
func NewProcessorWithWriter(docs *Docs, f Formatter, t *template.Template, config *Config, writer func(file, text string) error) *Processor {
15✔
64
        return &Processor{
15✔
65
                Config:    config,
15✔
66
                Template:  t,
15✔
67
                Formatter: f,
15✔
68
                Docs:      docs,
15✔
69
                writer:    writer,
15✔
70
        }
15✔
71
}
15✔
72

73
// PrepareDocs processes the API docs for subsequent rendering.
74
func (proc *Processor) PrepareDocs() error {
6✔
75
        err := proc.ExtractTests()
6✔
76
        if err != nil {
6✔
77
                return err
×
78
        }
×
79

80
        // Re-structure according to exports.
81
        err = proc.filterPackages()
6✔
82
        if err != nil {
6✔
83
                return err
×
84
        }
×
85
        // Collect all link target paths.
86
        proc.collectPaths()
6✔
87
        if !proc.Config.UseExports {
9✔
88
                for k := range proc.linkTargets {
16✔
89
                        proc.linkExports[k] = k
13✔
90
                }
13✔
91
        }
92
        // Replaces cross-refs by placeholders.
93
        if err := proc.processLinks(proc.Docs); err != nil {
6✔
94
                return err
×
95
        }
×
96
        return nil
6✔
97
}
98

99
func (proc *Processor) ExtractTests() error {
6✔
100
        // Collect the paths of all (sub)-elements in the original structure.
6✔
101
        proc.collectElementPaths()
6✔
102

6✔
103
        // Extract doc tests.
6✔
104
        err := proc.extractDocTests()
6✔
105
        if err != nil {
6✔
NEW
106
                return err
×
NEW
107
        }
×
108
        if proc.Config.TestOutput != "" {
6✔
NEW
109
                err = proc.writeDocTests(proc.Config.TestOutput)
×
NEW
110
                if err != nil {
×
NEW
111
                        return err
×
NEW
112
                }
×
113
        }
114
        return nil
6✔
115
}
116

117
func (proc *Processor) WriteFile(file, text string) error {
33✔
118
        return proc.writer(file, text)
33✔
119
}
33✔
120

121
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
122
        if proc.Config.Strict {
×
123
                return fmt.Errorf(pattern, args...)
×
124
        }
×
125
        log.Printf("WARNING: "+pattern+"\n", args...)
×
126
        return nil
×
127
}
128

129
func (proc *Processor) addLinkExport(oldPath, newPath []string) {
70✔
130
        pNew := strings.Join(newPath, ".")
70✔
131
        pOld := strings.Join(oldPath, ".")
70✔
132
        if present, ok := proc.linkExportsReverse[pNew]; ok {
70✔
133
                present.OldPaths = append(present.OldPaths, pOld)
×
134
        } else {
70✔
135
                proc.linkExportsReverse[pNew] = &exportError{
70✔
136
                        NewPath:  pNew,
70✔
137
                        OldPaths: []string{pOld},
70✔
138
                }
70✔
139
        }
70✔
140
        proc.linkExports[pOld] = pNew
70✔
141
}
142

143
func (proc *Processor) addLinkTarget(elPath, filePath []string, kind string, isSection bool) {
76✔
144
        proc.linkTargets[strings.Join(elPath, ".")] = elemPath{Elements: filePath, Kind: kind, IsSection: isSection}
76✔
145
}
76✔
146

147
func (proc *Processor) addElementPath(elPath, filePath []string, kind string, isSection bool) {
90✔
148
        if isSection && kind != "package" && kind != "module" { // actually, we are catching aliases here
117✔
149
                return
27✔
150
        }
27✔
151
        proc.allPaths[strings.Join(elPath, ".")] = true
63✔
152
        _, _ = filePath, kind
63✔
153
}
154

155
func (proc *Processor) mkDirs(path string) error {
16✔
156
        if proc.Config.DryRun {
20✔
157
                return nil
4✔
158
        }
4✔
159
        if err := os.MkdirAll(path, os.ModePerm); err != nil && !os.IsExist(err) {
12✔
160
                return err
×
161
        }
×
162
        return nil
12✔
163
}
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