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

mlange-42 / modo / 13180577064

06 Feb 2025 02:07PM CUT coverage: 73.089% (-1.0%) from 74.08%
13180577064

Pull #212

github

web-flow
Merge 98d3dc552 into 1566231bf
Pull Request #212: Add tests for CLI commands

36 of 54 new or added lines in 6 files covered. (66.67%)

2314 of 3166 relevant lines covered (73.09%)

40.88 hits per line

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

68.42
/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() (*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
                        start := time.Now()
1✔
46

1✔
47
                        cliArgs, err := document.ConfigFromViper(v)
1✔
48
                        if err != nil {
1✔
49
                                return err
×
50
                        }
×
51
                        if err := runTest(cliArgs); err != nil {
1✔
52
                                return err
×
53
                        }
×
54
                        if watch {
1✔
55
                                return watchAndRun(cliArgs, runTest)
×
56
                        }
×
57

58
                        if err := os.Chdir(cwd); err != nil {
1✔
NEW
59
                                return err
×
NEW
60
                        }
×
61

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

67
        root.Flags().StringVarP(&config, "config", "c", defaultConfigFile, "Config file in the working directory to use")
3✔
68
        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✔
69
        root.Flags().StringP("tests", "t", "", "Target folder to extract doctests for 'mojo test'")
3✔
70
        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✔
71
        root.Flags().BoolP("strict", "S", false, "Strict mode. Errors instead of warnings")
3✔
72
        root.Flags().BoolP("dry-run", "D", false, "Dry-run without any file output. Disables post-processing scripts")
3✔
73
        root.Flags().BoolP("bare", "B", false, "Don't run pre- and post-processing scripts")
3✔
74
        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✔
75
        root.Flags().StringSliceP("templates", "T", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository")
3✔
76

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

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

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

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

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

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

111
        return nil
1✔
112
}
113

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

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

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