77 "path/filepath"
88 "strings"
99 "testing"
10- "time"
1110)
1211
1312func TestCiscoScannerCompletesWithJSONOutputFile (t * testing.T ) {
@@ -17,7 +16,7 @@ func TestCiscoScannerCompletesWithJSONOutputFile(t *testing.T) {
1716 t .Fatal (err )
1817 }
1918 const ciscoJSON = `{"scanner":"cisco","findings":[]}`
20- runner := & ciscoRecordingCommandRunner { output : ciscoJSON }
19+ runner := & reportFileCommandRunner { outputFlag : "--output" , output : ciscoJSON }
2120 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
2221 if err != nil {
2322 t .Fatal (err )
@@ -59,7 +58,7 @@ func TestCiscoScannerCompletesWithJSONOutputFile(t *testing.T) {
5958}
6059
6160func TestCiscoScannerUsesResultDirAsDockerSandboxCWD (t * testing.T ) {
62- commandRunner := & ciscoRecordingCommandRunner { output : `{"scanner":"cisco","findings":[]}` }
61+ commandRunner := & reportFileCommandRunner { outputFlag : "--output" , output : `{"scanner":"cisco","findings":[]}` }
6362 result , err := (ExternalScannerRunner {
6463 CommandRunner : commandRunner ,
6564 Env : map [string ]string {},
@@ -87,7 +86,7 @@ func TestCiscoScannerEnablesUpstreamAnalyzersFromEnv(t *testing.T) {
8786 if err := os .Mkdir (target , 0o755 ); err != nil {
8887 t .Fatal (err )
8988 }
90- runner := & ciscoRecordingCommandRunner { output : `{"scanner":"cisco","findings":[]}` }
89+ runner := & reportFileCommandRunner { outputFlag : "--output" , output : `{"scanner":"cisco","findings":[]}` }
9190 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
9291 if err != nil {
9392 t .Fatal (err )
@@ -130,7 +129,7 @@ func TestCiscoScannerOutputFileIsPreservedInArtifactBundle(t *testing.T) {
130129 }
131130 out := filepath .Join (dir , "clawscan-results" , "artifact.json" )
132131 const ciscoJSON = `{"scanner":"cisco","findings":[{"id":"network"}]}`
133- runner := & ciscoRecordingCommandRunner { output : ciscoJSON }
132+ runner := & reportFileCommandRunner { outputFlag : "--output" , output : ciscoJSON }
134133 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" , "--output" , out })
135134 if err != nil {
136135 t .Fatal (err )
@@ -164,11 +163,12 @@ func TestCiscoScannerCompletesNonZeroExitWithJSONOutputFile(t *testing.T) {
164163 }
165164 const ciscoJSON = `{"scanner":"cisco","findings":[{"id":"pipeline-risk"}]}`
166165 exitCode := 1
167- runner := & ciscoRecordingCommandRunner {
168- output : ciscoJSON ,
169- stderr : "high severity findings" ,
170- err : errors .New ("exit status 1" ),
171- exitCode : & exitCode ,
166+ runner := & reportFileCommandRunner {
167+ outputFlag : "--output" ,
168+ output : ciscoJSON ,
169+ stderr : "high severity findings" ,
170+ err : errors .New ("exit status 1" ),
171+ exitCode : & exitCode ,
172172 }
173173 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
174174 if err != nil {
@@ -202,7 +202,7 @@ func TestCiscoScannerFailsMissingOutputFile(t *testing.T) {
202202 if err := os .Mkdir (target , 0o755 ); err != nil {
203203 t .Fatal (err )
204204 }
205- runner := & ciscoRecordingCommandRunner { }
205+ runner := & reportFileCommandRunner { outputFlag : "--output" }
206206 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
207207 if err != nil {
208208 t .Fatal (err )
@@ -232,7 +232,7 @@ func TestCiscoScannerFailsInvalidJSONOutputFile(t *testing.T) {
232232 if err := os .Mkdir (target , 0o755 ); err != nil {
233233 t .Fatal (err )
234234 }
235- runner := & ciscoRecordingCommandRunner { output : "not json" }
235+ runner := & reportFileCommandRunner { outputFlag : "--output" , output : "not json" }
236236 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
237237 if err != nil {
238238 t .Fatal (err )
@@ -262,7 +262,7 @@ func TestRunDispatchesCiscoScannerInsteadOfGenericSkipped(t *testing.T) {
262262 if err := os .Mkdir (target , 0o755 ); err != nil {
263263 t .Fatal (err )
264264 }
265- runner := & ciscoRecordingCommandRunner { output : `{"ok":true}` }
265+ runner := & reportFileCommandRunner { outputFlag : "--output" , output : `{"ok":true}` }
266266 opts , err := ParseArgs ([]string {target , "--scanner" , "cisco" })
267267 if err != nil {
268268 t .Fatal (err )
@@ -283,27 +283,6 @@ func TestRunDispatchesCiscoScannerInsteadOfGenericSkipped(t *testing.T) {
283283 }
284284}
285285
286- type ciscoRecordingCommandRunner struct {
287- calls []commandCall
288- output string
289- stderr string
290- err error
291- exitCode * int
292- }
293-
294- func (r * ciscoRecordingCommandRunner ) Run (command string , args []string , cwd string , timeout time.Duration ) (CommandOutput , error ) {
295- r .calls = append (r .calls , commandCall {command : command , args : append ([]string (nil ), args ... ), cwd : cwd })
296- if r .output != "" {
297- outputPath := argValue (args , "--output" )
298- if outputPath != "" {
299- if err := os .WriteFile (outputPath , []byte (r .output ), 0o644 ); err != nil {
300- return CommandOutput {Stderr : r .stderr }, err
301- }
302- }
303- }
304- return CommandOutput {Stderr : r .stderr , ExitCode : r .exitCode }, r .err
305- }
306-
307286func argValue (args []string , name string ) string {
308287 for i , arg := range args {
309288 if arg == name && i + 1 < len (args ) {
0 commit comments