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

mlange-42 / modo / 13606558454

01 Mar 2025 04:12PM CUT coverage: 74.172%. Remained the same
13606558454

push

github

web-flow
Fix CI for repo changes (#225)

2375 of 3202 relevant lines covered (74.17%)

48.18 hits per line

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

68.7
/internal/cmd/test.go
1
package cmd
2

3
import (
4
        "fmt"
5
        "os"
6
        "time"
7

8
        "github.com/mlange-42/modo/internal/document"
9
        "github.com/mlange-42/modo/internal/format"
10
        "github.com/spf13/cobra"
11
        "github.com/spf13/viper"
12
)
13

14
func testCommand(stopWatch chan struct{}) (*cobra.Command, error) {
3✔
15
        v := viper.New()
3✔
16
        var config string
3✔
17
        var watch bool
3✔
18

3✔
19
        var cwd string
3✔
20

3✔
21
        root := &cobra.Command{
3✔
22
                Use:   "test [PATH]",
3✔
23
                Short: "Extract doc-tests from 'mojo doc' JSON",
3✔
24
                Long: `Extract doc-tests from 'mojo doc' JSON.
3✔
25

3✔
26
Extracts tests based on the 'modo.yaml' file in the current directory if no path is given.
3✔
27
The flags listed below overwrite the settings from that file.
3✔
28

3✔
29
Complete documentation at https://mlange-42.github.io/modo/`,
3✔
30
                Example: `  modo init hugo                 # set up a project, e.g. for Hugo
3✔
31
  modo test                      # extract doc-tests`,
3✔
32
                Args:         cobra.MaximumNArgs(1),
3✔
33
                SilenceUsage: true,
3✔
34
                PreRunE: func(cmd *cobra.Command, args []string) error {
4✔
35
                        var err error
1✔
36
                        if err = checkConfigFile(config); err != nil {
1✔
37
                                return err
×
38
                        }
×
39
                        if cwd, err = mountProject(v, config, args); err != nil {
1✔
40
                                return err
×
41
                        }
×
42
                        return nil
1✔
43
                },
44
                RunE: func(cmd *cobra.Command, args []string) error {
1✔
45
                        defer func() {
2✔
46
                                if err := os.Chdir(cwd); err != nil {
1✔
47
                                        fmt.Println(err)
×
48
                                }
×
49
                        }()
50

51
                        start := time.Now()
1✔
52

1✔
53
                        cliArgs, err := document.ConfigFromViper(v)
1✔
54
                        if err != nil {
1✔
55
                                return err
×
56
                        }
×
57
                        if err := runTest(cliArgs); err != nil {
1✔
58
                                return err
×
59
                        }
×
60
                        if watch {
1✔
61
                                return watchAndRun(cliArgs, runTest, stopWatch)
×
62
                        }
×
63

64
                        fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
1✔
65
                        return nil
1✔
66
                },
67
        }
68

69
        root.Flags().StringVarP(&config, "config", "c", defaultConfigFile, "Config file in the working directory to use")
3✔
70
        root.Flags().StringSliceP("input", "i", []string{}, "'mojo doc' JSON file to process. Reads from STDIN if not specified.\nIf a single directory is given, it is processed recursively")
3✔
71
        root.Flags().StringP("tests", "t", "", "Target folder to extract doctests for 'mojo test'")
3✔
72
        root.Flags().BoolP("case-insensitive", "C", false, "Build for systems that are not case-sensitive regarding file names.\nAppends hyphen (-) to capitalized file names")
3✔
73
        root.Flags().BoolP("strict", "S", false, "Strict mode. Errors instead of warnings")
3✔
74
        root.Flags().BoolP("dry-run", "D", false, "Dry-run without any file output. Disables post-processing scripts")
3✔
75
        root.Flags().BoolP("bare", "B", false, "Don't run pre- and post-processing scripts")
3✔
76
        root.Flags().BoolVarP(&watch, "watch", "W", false, "Re-run on changes of sources and documentation files.\nDisables post-processing scripts after running them once")
3✔
77
        root.Flags().StringSliceP("templates", "T", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository")
3✔
78

3✔
79
        root.Flags().SortFlags = false
3✔
80
        root.MarkFlagFilename("config", "yaml")
3✔
81
        root.MarkFlagFilename("input", "json")
3✔
82
        root.MarkFlagDirname("tests")
3✔
83
        root.MarkFlagDirname("templates")
3✔
84

3✔
85
        err := bindFlags(v, root.Flags())
3✔
86
        if err != nil {
3✔
87
                return nil, err
×
88
        }
×
89
        return root, nil
3✔
90
}
91

92
func runTest(args *document.Config) error {
1✔
93
        if args.TestOutput == "" {
1✔
94
                return fmt.Errorf("no output path for tests given")
×
95
        }
×
96

97
        if !args.Bare {
2✔
98
                if err := runPreTestCommands(args); err != nil {
1✔
99
                        return err
×
100
                }
×
101
        }
102

103
        if err := runFilesOrDir(runTestOnce, args, nil); err != nil {
1✔
104
                return err
×
105
        }
×
106

107
        if !args.Bare && !args.DryRun {
2✔
108
                if err := runPostTestCommands(args); err != nil {
1✔
109
                        return err
×
110
                }
×
111
        }
112

113
        return nil
1✔
114
}
115

116
func runTestOnce(file string, args *document.Config, _ document.Formatter, subdir string, isFile, isDir bool) error {
2✔
117
        if isDir {
3✔
118
                if err := document.ExtractTestsMarkdown(args, &format.Plain{}, file, false); err != nil {
1✔
119
                        return err
×
120
                }
×
121
                return runDir(file, args, nil, runTestOnce)
1✔
122
        }
123
        docs, err := readDocs(file)
1✔
124
        if err != nil {
1✔
125
                return err
×
126
        }
×
127
        if err := document.ExtractTests(docs, args, &format.Plain{}, subdir); err != nil {
1✔
128
                return err
×
129
        }
×
130
        return nil
1✔
131
}
132

133
func runPreTestCommands(cfg *document.Config) error {
1✔
134
        if err := runCommands(cfg.PreRun); err != nil {
1✔
135
                return commandError("pre-run", err)
×
136
        }
×
137
        if err := runCommands(cfg.PreTest); err != nil {
1✔
138
                return commandError("pre-test", err)
×
139
        }
×
140
        return nil
1✔
141
}
142

143
func runPostTestCommands(cfg *document.Config) error {
1✔
144
        if err := runCommands(cfg.PostTest); err != nil {
1✔
145
                return commandError("post-test", err)
×
146
        }
×
147
        if err := runCommands(cfg.PostRun); err != nil {
1✔
148
                return commandError("post-run", err)
×
149
        }
×
150
        return nil
1✔
151
}
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