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

mlange-42 / modo / 13179389384

06 Feb 2025 01:02PM CUT coverage: 56.505% (-17.6%) from 74.08%
13179389384

Pull #212

github

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

1772 of 3136 relevant lines covered (56.51%)

21.11 hits per line

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

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

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

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

13
func testCommand() (*cobra.Command, error) {
×
14
        v := viper.New()
×
15
        var config string
×
16
        var watch bool
×
17

×
18
        root := &cobra.Command{
×
19
                Use:   "test [PATH]",
×
20
                Short: "Extract doc-tests from 'mojo doc' JSON",
×
21
                Long: `Extract doc-tests from 'mojo doc' JSON.
×
22

×
23
Extracts tests based on the 'modo.yaml' file in the current directory if no path is given.
×
24
The flags listed below overwrite the settings from that file.
×
25

×
26
Complete documentation at https://mlange-42.github.io/modo/`,
×
27
                Example: `  modo init hugo                 # set up a project, e.g. for Hugo
×
28
  modo test                      # extract doc-tests`,
×
29
                Args:         cobra.MaximumNArgs(1),
×
30
                SilenceUsage: true,
×
31
                PreRunE: func(cmd *cobra.Command, args []string) error {
×
32
                        if err := checkConfigFile(config); err != nil {
×
33
                                return err
×
34
                        }
×
35
                        if err := mountProject(v, config, args); err != nil {
×
36
                                return err
×
37
                        }
×
38
                        return nil
×
39
                },
40
                RunE: func(cmd *cobra.Command, args []string) error {
×
41
                        start := time.Now()
×
42

×
43
                        cliArgs, err := document.ConfigFromViper(v)
×
44
                        if err != nil {
×
45
                                return err
×
46
                        }
×
47
                        if err := runTest(cliArgs); err != nil {
×
48
                                return err
×
49
                        }
×
50
                        if watch {
×
51
                                return watchAndRun(cliArgs, runTest)
×
52
                        }
×
53

54
                        fmt.Printf("Completed in %.1fms 🧯\n", float64(time.Since(start).Microseconds())/1000.0)
×
55
                        return nil
×
56
                },
57
        }
58

59
        root.Flags().StringVarP(&config, "config", "c", defaultConfigFile, "Config file in the working directory to use")
×
60
        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")
×
61
        root.Flags().StringP("tests", "t", "", "Target folder to extract doctests for 'mojo test'")
×
62
        root.Flags().BoolP("case-insensitive", "C", false, "Build for systems that are not case-sensitive regarding file names.\nAppends hyphen (-) to capitalized file names")
×
63
        root.Flags().BoolP("strict", "S", false, "Strict mode. Errors instead of warnings")
×
64
        root.Flags().BoolP("dry-run", "D", false, "Dry-run without any file output. Disables post-processing scripts")
×
65
        root.Flags().BoolP("bare", "B", false, "Don't run pre- and post-processing scripts")
×
66
        root.Flags().BoolVarP(&watch, "watch", "W", false, "Re-run on changes of sources and documentation files.\nDisables post-processing scripts after running them once")
×
67
        root.Flags().StringSliceP("templates", "T", []string{}, "Optional directories with templates for (partial) overwrite.\nSee folder assets/templates in the repository")
×
68

×
69
        root.Flags().SortFlags = false
×
70
        root.MarkFlagFilename("config", "yaml")
×
71
        root.MarkFlagFilename("input", "json")
×
72
        root.MarkFlagDirname("tests")
×
73
        root.MarkFlagDirname("templates")
×
74

×
75
        err := bindFlags(v, root.Flags())
×
76
        if err != nil {
×
77
                return nil, err
×
78
        }
×
79
        return root, nil
×
80
}
81

82
func runTest(args *document.Config) error {
×
83
        if args.TestOutput == "" {
×
84
                return fmt.Errorf("no output path for tests given")
×
85
        }
×
86

87
        if !args.Bare {
×
88
                if err := runPreTestCommands(args); err != nil {
×
89
                        return err
×
90
                }
×
91
        }
92

93
        if err := runFilesOrDir(runTestOnce, args, nil); err != nil {
×
94
                return err
×
95
        }
×
96

97
        if !args.Bare && !args.DryRun {
×
98
                if err := runPostTestCommands(args); err != nil {
×
99
                        return err
×
100
                }
×
101
        }
102

103
        return nil
×
104
}
105

106
func runTestOnce(file string, args *document.Config, _ document.Formatter, subdir string, isFile, isDir bool) error {
×
107
        if isDir {
×
108
                if err := document.ExtractTestsMarkdown(args, &format.Plain{}, file, false); err != nil {
×
109
                        return err
×
110
                }
×
111
                return runDir(file, args, nil, runTestOnce)
×
112
        }
113
        docs, err := readDocs(file)
×
114
        if err != nil {
×
115
                return err
×
116
        }
×
117
        if err := document.ExtractTests(docs, args, &format.Plain{}, subdir); err != nil {
×
118
                return err
×
119
        }
×
120
        return nil
×
121
}
122

123
func runPreTestCommands(cfg *document.Config) error {
×
124
        if err := runCommands(cfg.PreRun); err != nil {
×
125
                return commandError("pre-run", err)
×
126
        }
×
127
        if err := runCommands(cfg.PreTest); err != nil {
×
128
                return commandError("pre-test", err)
×
129
        }
×
130
        return nil
×
131
}
132

133
func runPostTestCommands(cfg *document.Config) error {
×
134
        if err := runCommands(cfg.PostTest); err != nil {
×
135
                return commandError("post-test", err)
×
136
        }
×
137
        if err := runCommands(cfg.PostRun); err != nil {
×
138
                return commandError("post-run", err)
×
139
        }
×
140
        return nil
×
141
}
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