Skip to content

Commit 91ffe2e

Browse files
authored
extract method finalizeRun() (#4181)
there's no reason for it to be a persistent hook, "run" does not have subcommands
1 parent d472a2f commit 91ffe2e

1 file changed

Lines changed: 98 additions & 93 deletions

File tree

  • cmd/crowdsec-cli/clihubtest

cmd/crowdsec-cli/clihubtest/run.go

Lines changed: 98 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,101 @@ func (cli *cliHubTest) run(ctx context.Context, all bool, nucleiTargetHost strin
7474
return eg.Wait()
7575
}
7676

77+
func (cli *cliHubTest) finalizeRun(noClean bool, forceClean bool, reportSuccess bool) error {
78+
cfg := cli.cfg()
79+
80+
success := true
81+
testMap := make(map[string]*hubtest.HubTestItem)
82+
83+
for _, test := range hubPtr.Tests {
84+
if test.AutoGen && !isAppsecTest {
85+
if test.ParserAssert.AutoGenAssert {
86+
log.Warningf("Assert file '%s' is empty, generating assertion:", test.ParserAssert.File)
87+
fmt.Fprintln(os.Stdout)
88+
fmt.Fprintln(os.Stdout, test.ParserAssert.AutoGenAssertData)
89+
}
90+
91+
if test.ScenarioAssert.AutoGenAssert {
92+
log.Warningf("Assert file '%s' is empty, generating assertion:", test.ScenarioAssert.File)
93+
fmt.Fprintln(os.Stdout)
94+
fmt.Fprintln(os.Stdout, test.ScenarioAssert.AutoGenAssertData)
95+
}
96+
97+
if !noClean {
98+
test.Clean()
99+
}
100+
101+
return fmt.Errorf("please fill your assert file(s) for test '%s', exiting", test.Name)
102+
}
103+
104+
testMap[test.Name] = test
105+
106+
if test.Success {
107+
if !noClean {
108+
test.Clean()
109+
}
110+
} else {
111+
success = false
112+
cleanTestEnv := false
113+
114+
if cfg.Cscli.Output == "human" {
115+
printParserFailures(test)
116+
printScenarioFailures(test)
117+
118+
if !forceClean && !noClean {
119+
prompt := &survey.Confirm{
120+
Message: fmt.Sprintf("Do you want to remove runtime and result folder for '%s'?", test.Name),
121+
Default: true,
122+
}
123+
if err := survey.AskOne(prompt, &cleanTestEnv); err != nil {
124+
return fmt.Errorf("unable to ask to remove runtime folder: %w", err)
125+
}
126+
}
127+
}
128+
129+
if cleanTestEnv || forceClean {
130+
test.Clean()
131+
}
132+
}
133+
}
134+
135+
switch cfg.Cscli.Output {
136+
case "human":
137+
hubTestResultTable(color.Output, cfg.Cscli.Color, testMap, reportSuccess)
138+
case "json":
139+
jsonResult := make(map[string][]string, 0)
140+
jsonResult["success"] = make([]string, 0)
141+
jsonResult["fail"] = make([]string, 0)
142+
143+
for testName, test := range testMap {
144+
if test.Success {
145+
jsonResult["success"] = append(jsonResult["success"], testName)
146+
} else {
147+
jsonResult["fail"] = append(jsonResult["fail"], testName)
148+
}
149+
}
150+
151+
jsonStr, err := json.Marshal(jsonResult)
152+
if err != nil {
153+
return fmt.Errorf("unable to json test result: %w", err)
154+
}
155+
156+
fmt.Fprintln(os.Stdout, string(jsonStr))
157+
default:
158+
return errors.New("only human/json output modes are supported")
159+
}
160+
161+
if !success {
162+
if reportSuccess {
163+
return errors.New("some tests failed")
164+
}
165+
166+
return errors.New("some tests failed, use --report-success to show them all")
167+
}
168+
169+
return nil
170+
}
171+
77172
func printParserFailures(test *hubtest.HubTestItem) {
78173
if len(test.ParserAssert.Fails) == 0 {
79174
return
@@ -135,101 +230,11 @@ func (cli *cliHubTest) newRunCmd() *cobra.Command {
135230
fmt.Fprintf(os.Stdout, "Running all tests (max_jobs: %d)\n", maxJobs)
136231
}
137232

138-
return cli.run(cmd.Context(), all, nucleiTargetHost, appSecHost, args, maxJobs)
139-
},
140-
PersistentPostRunE: func(_ *cobra.Command, _ []string) error {
141-
cfg := cli.cfg()
142-
143-
success := true
144-
testMap := make(map[string]*hubtest.HubTestItem)
145-
146-
for _, test := range hubPtr.Tests {
147-
if test.AutoGen && !isAppsecTest {
148-
if test.ParserAssert.AutoGenAssert {
149-
log.Warningf("Assert file '%s' is empty, generating assertion:", test.ParserAssert.File)
150-
fmt.Fprintln(os.Stdout)
151-
fmt.Fprintln(os.Stdout, test.ParserAssert.AutoGenAssertData)
152-
}
153-
154-
if test.ScenarioAssert.AutoGenAssert {
155-
log.Warningf("Assert file '%s' is empty, generating assertion:", test.ScenarioAssert.File)
156-
fmt.Fprintln(os.Stdout)
157-
fmt.Fprintln(os.Stdout, test.ScenarioAssert.AutoGenAssertData)
158-
}
159-
160-
if !noClean {
161-
test.Clean()
162-
}
163-
164-
return fmt.Errorf("please fill your assert file(s) for test '%s', exiting", test.Name)
165-
}
166-
167-
testMap[test.Name] = test
168-
169-
if test.Success {
170-
if !noClean {
171-
test.Clean()
172-
}
173-
} else {
174-
success = false
175-
cleanTestEnv := false
176-
177-
if cfg.Cscli.Output == "human" {
178-
printParserFailures(test)
179-
printScenarioFailures(test)
180-
181-
if !forceClean && !noClean {
182-
prompt := &survey.Confirm{
183-
Message: fmt.Sprintf("Do you want to remove runtime and result folder for '%s'?", test.Name),
184-
Default: true,
185-
}
186-
if err := survey.AskOne(prompt, &cleanTestEnv); err != nil {
187-
return fmt.Errorf("unable to ask to remove runtime folder: %w", err)
188-
}
189-
}
190-
}
191-
192-
if cleanTestEnv || forceClean {
193-
test.Clean()
194-
}
195-
}
196-
}
197-
198-
switch cfg.Cscli.Output {
199-
case "human":
200-
hubTestResultTable(color.Output, cfg.Cscli.Color, testMap, reportSuccess)
201-
case "json":
202-
jsonResult := make(map[string][]string, 0)
203-
jsonResult["success"] = make([]string, 0)
204-
jsonResult["fail"] = make([]string, 0)
205-
206-
for testName, test := range testMap {
207-
if test.Success {
208-
jsonResult["success"] = append(jsonResult["success"], testName)
209-
} else {
210-
jsonResult["fail"] = append(jsonResult["fail"], testName)
211-
}
212-
}
213-
214-
jsonStr, err := json.Marshal(jsonResult)
215-
if err != nil {
216-
return fmt.Errorf("unable to json test result: %w", err)
217-
}
218-
219-
fmt.Fprintln(os.Stdout, string(jsonStr))
220-
default:
221-
return errors.New("only human/json output modes are supported")
222-
}
223-
224-
if !success {
225-
if reportSuccess {
226-
return errors.New("some tests failed")
227-
}
228-
229-
return errors.New("some tests failed, use --report-success to show them all")
233+
if err := cli.run(cmd.Context(), all, nucleiTargetHost, appSecHost, args, maxJobs); err != nil {
234+
return err
230235
}
231236

232-
return nil
237+
return cli.finalizeRun(noClean, forceClean, reportSuccess)
233238
},
234239
}
235240

0 commit comments

Comments
 (0)