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

mlange-42 / modo / 13043300400

30 Jan 2025 12:42AM CUT coverage: 67.728%. Remained the same
13043300400

Pull #167

github

web-flow
Merge d23125652 into 443450e82
Pull Request #167: Collapse packages in file trees, use 4 columns

1213 of 1791 relevant lines covered (67.73%)

28.13 hits per line

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

22.73
/document/util.go
1
package document
2

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

10
        "github.com/mlange-42/modo/assets"
11
)
12

13
// appends to a slice, but guaranties to return a new one and not alter the original.
14
func appendNew[T any](sl []T, elems ...T) []T {
692✔
15
        sl2 := make([]T, len(sl), len(sl)+len(elems))
692✔
16
        copy(sl2, sl)
692✔
17
        sl2 = append(sl2, elems...)
692✔
18
        return sl2
692✔
19
}
692✔
20

21
func warnOrError(strict bool, pattern string, args ...any) error {
×
22
        if strict {
×
23
                return fmt.Errorf(pattern, args...)
×
24
        }
×
25
        fmt.Printf("WARNING: "+pattern+"\n", args...)
×
26
        return nil
×
27
}
28

29
func LoadTemplates(f Formatter, additional ...string) (*template.Template, error) {
11✔
30
        templ := template.New("all")
11✔
31
        templ = templ.Funcs(template.FuncMap{
11✔
32
                "toLink": f.ToLinkPath,
11✔
33
        })
11✔
34
        templ, err := templ.ParseFS(assets.Templates, "templates/*.*", "templates/**/*.*")
11✔
35
        if err != nil {
11✔
36
                return nil, err
×
37
        }
×
38

39
        for _, dir := range additional {
11✔
40
                if dir == "" {
×
41
                        continue
×
42
                }
43
                exists, isDir, err := fileExists(dir)
×
44
                if err != nil {
×
45
                        return nil, err
×
46
                }
×
47
                if !exists || !isDir {
×
48
                        return nil, fmt.Errorf("template directory '%s' does not exist", dir)
×
49
                }
×
50
                moreTemplates, err := findTemplates(dir)
×
51
                if err != nil {
×
52
                        return nil, err
×
53
                }
×
54
                templ, err = templ.ParseFiles(moreTemplates...)
×
55
                if err != nil {
×
56
                        return nil, err
×
57
                }
×
58
        }
59
        return templ, nil
11✔
60
}
61

62
func fileExists(file string) (exists, isDir bool, err error) {
×
63
        var s os.FileInfo
×
64
        if s, err = os.Stat(file); err == nil {
×
65
                exists = true
×
66
                isDir = s.IsDir()
×
67
                return
×
68
        } else if !errors.Is(err, os.ErrNotExist) {
×
69
                return
×
70
        }
×
71
        err = nil
×
72
        return
×
73
}
74

75
func findTemplates(dir string) ([]string, error) {
×
76
        allTemplates := []string{}
×
77
        err := filepath.WalkDir(dir,
×
78
                func(path string, info os.DirEntry, err error) error {
×
79
                        if err != nil {
×
80
                                return err
×
81
                        }
×
82
                        if !info.IsDir() {
×
83
                                allTemplates = append(allTemplates, path)
×
84
                        }
×
85
                        return nil
×
86
                })
87
        if err != nil {
×
88
                return nil, err
×
89
        }
×
90
        return allTemplates, nil
×
91
}
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