Skip to content

Commit c3ef632

Browse files
authored
Merge pull request #36 from wata727/advance_test_cli
CLI Test By Interface
2 parents 430deb7 + 982099f commit c3ef632

31 files changed

+592
-193
lines changed

cli.go

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ type CLI struct {
2424
// outStream and errStream are the stdout and stderr
2525
// to write message from the CLI.
2626
outStream, errStream io.Writer
27+
loader loader.LoaderIF
28+
detector detector.DetectorIF
29+
printer printer.PrinterIF
2730
testMode bool
2831
TestCLIOptions TestCLIOptions
2932
}
3033

3134
type TestCLIOptions struct {
3235
Config *config.Config
33-
Format string
34-
LoadFile string
3536
ConfigFile string
3637
}
3738

@@ -116,45 +117,52 @@ func (cli *CLI) Run(args []string) int {
116117
if ignoreRule != "" {
117118
c.SetIgnoreRule(ignoreRule)
118119
}
119-
120+
// If enabled test mode, set config infomation
120121
if cli.testMode {
121122
cli.TestCLIOptions = TestCLIOptions{
122123
Config: c,
123-
Format: format,
124-
LoadFile: flags.Arg(0),
125124
ConfigFile: configFile,
126125
}
126+
}
127+
128+
// Main function
129+
var err error
130+
// If disabled test mode, generates real loader
131+
if !cli.testMode {
132+
cli.loader = loader.NewLoader(c.Debug)
133+
}
134+
if flags.NArg() > 0 {
135+
err = cli.loader.LoadFile(flags.Arg(0))
127136
} else {
128-
// Main function
129-
var err error
130-
l := loader.NewLoader(c.Debug)
131-
if flags.NArg() > 0 {
132-
err = l.LoadFile(flags.Arg(0))
133-
} else {
134-
err = l.LoadAllFile(".")
135-
}
136-
if err != nil {
137-
fmt.Fprintln(cli.errStream, err)
138-
return ExitCodeError
139-
}
137+
err = cli.loader.LoadAllFile(".")
138+
}
139+
if err != nil {
140+
fmt.Fprintln(cli.errStream, err)
141+
return ExitCodeError
142+
}
140143

141-
d, err := detector.NewDetector(l.ListMap, c)
142-
if err != nil {
143-
fmt.Fprintln(cli.errStream, err)
144-
return ExitCodeError
145-
}
146-
issues := d.Detect()
147-
if d.Error {
148-
fmt.Fprintln(cli.errStream, "ERROR: error occurred in detecting. Please run with --debug options for details.")
149-
return ExitCodeError
150-
}
144+
// If disabled test mode, generates real detector
145+
if !cli.testMode {
146+
cli.detector, err = detector.NewDetector(cli.loader.DumpFiles(), c)
147+
}
148+
if err != nil {
149+
fmt.Fprintln(cli.errStream, err)
150+
return ExitCodeError
151+
}
152+
issues := cli.detector.Detect()
153+
if cli.detector.HasError() {
154+
fmt.Fprintln(cli.errStream, "ERROR: error occurred in detecting. Please run with --debug options for details.")
155+
return ExitCodeError
156+
}
151157

152-
p := printer.NewPrinter(cli.outStream, cli.errStream)
153-
p.Print(issues, format)
158+
// If disabled test mode, generates real printer
159+
if !cli.testMode {
160+
cli.printer = printer.NewPrinter(cli.outStream, cli.errStream)
161+
}
162+
cli.printer.Print(issues, format)
154163

155-
if errorWithIssues && len(issues) > 0 {
156-
return ExitCodeIssuesFound
157-
}
164+
if errorWithIssues && len(issues) > 0 {
165+
return ExitCodeIssuesFound
158166
}
159167

160168
return ExitCodeOK

0 commit comments

Comments
 (0)