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

mlange-42 / modo / 13142221120

04 Feb 2025 06:22PM CUT coverage: 72.507%. Remained the same
13142221120

Pull #198

github

web-flow
Merge c2442ddf3 into 7a3cf7445
Pull Request #198: Fix template overwrite for function: add source link

1527 of 2106 relevant lines covered (72.51%)

25.57 hits per line

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

68.79
/document/util.go
1
package document
2

3
import (
4
        "errors"
5
        "fmt"
6
        "os"
7
        "path/filepath"
8
        "strings"
9
        "text/template"
10

11
        "github.com/mlange-42/modo/assets"
12
        "gopkg.in/ini.v1"
13
)
14

15
const codeFence3 = "```"
16
const codeFence4 = "````"
17

18
type fenceType uint8
19

20
const (
21
        fenceNone fenceType = iota
22
        fenceThree
23
        fenceFour
24
)
25

26
type GitInfo struct {
27
        Title    string
28
        Repo     string
29
        Pages    string
30
        GoModule string
31
        BasePath string
32
}
33

34
func getFenceType(line string) fenceType {
256✔
35
        isFence4 := strings.HasPrefix(line, codeFence4)
256✔
36
        if strings.HasPrefix(line, codeFence3) && !isFence4 {
298✔
37
                return fenceThree
42✔
38
        }
42✔
39
        if isFence4 {
218✔
40
                return fenceFour
4✔
41
        }
4✔
42
        return fenceNone
210✔
43
}
44

45
// appends to a slice, but guaranties to return a new one and not alter the original.
46
func appendNew[T any](sl []T, elems ...T) []T {
692✔
47
        sl2 := make([]T, len(sl), len(sl)+len(elems))
692✔
48
        copy(sl2, sl)
692✔
49
        sl2 = append(sl2, elems...)
692✔
50
        return sl2
692✔
51
}
692✔
52

53
func warnOrError(strict bool, pattern string, args ...any) error {
2✔
54
        if strict {
3✔
55
                return fmt.Errorf(pattern, args...)
1✔
56
        }
1✔
57
        fmt.Printf("WARNING: "+pattern+"\n", args...)
1✔
58
        return nil
1✔
59
}
60

61
func LoadTemplates(f Formatter, sourceURL string, additional ...string) (*template.Template, error) {
13✔
62
        templ := template.New("all")
13✔
63
        templ = templ.Funcs(template.FuncMap{
13✔
64
                "toLink":    f.ToLinkPath,
13✔
65
                "sourceUrl": func() string { return sourceURL },
57✔
66
        })
67
        templ, err := templ.ParseFS(assets.Templates, "templates/*.*", "templates/**/*.*")
13✔
68
        if err != nil {
13✔
69
                return nil, err
×
70
        }
×
71

72
        for _, dir := range additional {
13✔
73
                if dir == "" {
×
74
                        continue
×
75
                }
76
                exists, isDir, err := fileExists(dir)
×
77
                if err != nil {
×
78
                        return nil, err
×
79
                }
×
80
                if !exists || !isDir {
×
81
                        return nil, fmt.Errorf("template directory '%s' does not exist", dir)
×
82
                }
×
83
                moreTemplates, err := findTemplates(dir)
×
84
                if err != nil {
×
85
                        return nil, err
×
86
                }
×
87
                templ, err = templ.ParseFiles(moreTemplates...)
×
88
                if err != nil {
×
89
                        return nil, err
×
90
                }
×
91
        }
92
        return templ, nil
13✔
93
}
94

95
func fileExists(file string) (exists, isDir bool, err error) {
4✔
96
        var s os.FileInfo
4✔
97
        if s, err = os.Stat(file); err == nil {
5✔
98
                exists = true
1✔
99
                isDir = s.IsDir()
1✔
100
                return
1✔
101
        } else if !errors.Is(err, os.ErrNotExist) {
4✔
102
                return
×
103
        }
×
104
        err = nil
3✔
105
        return
3✔
106
}
107

108
func findTemplates(dir string) ([]string, error) {
×
109
        allTemplates := []string{}
×
110
        err := filepath.WalkDir(dir,
×
111
                func(path string, info os.DirEntry, err error) error {
×
112
                        if err != nil {
×
113
                                return err
×
114
                        }
×
115
                        if !info.IsDir() {
×
116
                                allTemplates = append(allTemplates, path)
×
117
                        }
×
118
                        return nil
×
119
                })
120
        if err != nil {
×
121
                return nil, err
×
122
        }
×
123
        return allTemplates, nil
×
124
}
125

126
func GetCwdName() (string, error) {
1✔
127
        cwd, err := os.Getwd()
1✔
128
        if err != nil {
1✔
129
                return cwd, err
×
130
        }
×
131
        return filepath.Base(cwd), nil
1✔
132
}
133

134
func GetGitOrigin(outDir string) (*GitInfo, error) {
2✔
135
        gitFiles := []string{
2✔
136
                ".git/config",
2✔
137
                "../.git/config",
2✔
138
        }
2✔
139

2✔
140
        var content *ini.File
2✔
141
        found := false
2✔
142
        basePath := ""
2✔
143
        for _, f := range gitFiles {
6✔
144
                exists, isDir, err := fileExists(f)
4✔
145
                if err != nil {
4✔
146
                        return nil, err
×
147
                }
×
148
                if !exists || isDir {
7✔
149
                        continue
3✔
150
                }
151
                content, err = ini.Load(f)
1✔
152
                if err != nil {
1✔
153
                        return nil, err
×
154
                }
×
155

156
                if strings.HasPrefix(f, "..") {
2✔
157
                        basePath, err = GetCwdName()
1✔
158
                        if err != nil {
1✔
159
                                return nil, err
×
160
                        }
×
161
                }
162
                found = true
1✔
163
                break
1✔
164
        }
165

166
        url := "https://github.com/your/package"
2✔
167
        ok := false
2✔
168
        if found {
3✔
169
                section := content.Section(`remote "origin"`)
1✔
170
                if section != nil {
2✔
171
                        value := section.Key("url")
1✔
172
                        if value != nil {
2✔
173
                                url = strings.TrimSuffix(value.String(), ".git")
1✔
174
                                ok = true
1✔
175
                        }
1✔
176
                }
177
        }
178
        if !ok {
3✔
179
                fmt.Printf("WARNING: No Git repository or no remote 'origin' found.\n         Using dummy %s\n", url)
1✔
180
        }
1✔
181
        title, pages := repoToTitleAndPages(url)
2✔
182
        module := strings.ReplaceAll(strings.ReplaceAll(url, "https://", ""), "http://", "")
2✔
183
        module = fmt.Sprintf("%s/%s", module, outDir)
2✔
184

2✔
185
        return &GitInfo{
2✔
186
                Title:    title,
2✔
187
                Repo:     url,
2✔
188
                Pages:    pages,
2✔
189
                GoModule: module,
2✔
190
                BasePath: basePath,
2✔
191
        }, nil
2✔
192
}
193

194
func repoToTitleAndPages(repo string) (string, string) {
4✔
195
        if !strings.HasPrefix(repo, "https://github.com/") {
5✔
196
                parts := strings.Split(repo, "/")
1✔
197
                title := parts[len(parts)-1]
1✔
198
                return title, fmt.Sprintf("https://%s.com", title)
1✔
199
        }
1✔
200
        repo = strings.TrimPrefix(repo, "https://github.com/")
3✔
201
        parts := strings.Split(repo, "/")
3✔
202
        return parts[1], fmt.Sprintf("https://%s.github.io/%s/", parts[0], parts[1])
3✔
203
}
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