@@ -16,7 +16,10 @@ import (
16
16
"github.com/snyk/go-application-framework/pkg/apiclients/testapi"
17
17
"github.com/snyk/go-application-framework/pkg/configuration"
18
18
gafmocks "github.com/snyk/go-application-framework/pkg/mocks"
19
+ "github.com/snyk/go-application-framework/pkg/runtimeinfo"
20
+ "github.com/snyk/go-application-framework/pkg/workflow"
19
21
22
+ "github.com/snyk/cli-extension-os-flows/internal/bundlestore"
20
23
"github.com/snyk/cli-extension-os-flows/internal/commands/ostest"
21
24
"github.com/snyk/cli-extension-os-flows/internal/errors"
22
25
"github.com/snyk/cli-extension-os-flows/internal/mocks"
@@ -28,12 +31,65 @@ import (
28
31
// pathRgxp is used for replacing the "path" in the output for snapshot consistency.
29
32
var pathRgxp = regexp .MustCompile (`\s*,?"path"\s*:\s*"[^"]*",?` )
30
33
31
- func Test_RunSbomReachabilityFlow_Success (t * testing.T ) {
32
- logger := zerolog .Nop ()
33
- ef := errors .NewErrorFactory (& logger )
34
+ var nopLogger = zerolog .Nop ()
35
+
36
+ func Test_RunSbomReachabilityFlow_JSON (t * testing.T ) {
37
+ ctrl := gomock .NewController (t )
38
+ defer ctrl .Finish ()
39
+
40
+ ctx := context .Background ()
41
+ ef := errors .NewErrorFactory (& nopLogger )
42
+ mockIctx , mockTestClient , mockBsClient , orgID , orgSlug , sbomPath , sourceCodePath := setupTest (ctx , t , ctrl , true )
43
+
44
+ // This should now succeed with proper finding data
45
+ result , err := ostest .RunSbomReachabilityFlow (ctx , mockIctx , mockTestClient , ef , & nopLogger , sbomPath , sourceCodePath , mockBsClient , orgID , orgSlug )
46
+
47
+ require .NoError (t , err )
48
+ require .NotNil (t , result )
49
+ require .Len (t , result , 2 ) // Should return legacy data + summary data
50
+ require .Contains (t , result [0 ].GetContentType (), "application/json" ) // legacy data
51
+ require .Contains (t , result [1 ].GetContentType (), "application/json; schema=test-summary" ) // summary data
52
+
53
+ legacyJSON , ok := result [0 ].GetPayload ().([]byte )
54
+ require .True (t , ok )
55
+ legacySummary , ok := result [1 ].GetPayload ().([]byte )
56
+ require .True (t , ok )
57
+ snaps .MatchJSON (t , pathRgxp .ReplaceAll (legacyJSON , nil ))
58
+ snaps .MatchJSON (t , pathRgxp .ReplaceAll (legacySummary , nil ))
59
+ }
60
+
61
+ func Test_RunSbomReachabilityFlow_HumanReadable (t * testing.T ) {
34
62
ctrl := gomock .NewController (t )
35
63
defer ctrl .Finish ()
64
+
36
65
ctx := context .Background ()
66
+ ef := errors .NewErrorFactory (& nopLogger )
67
+ mockIctx , mockTestClient , mockBsClient , orgID , orgSlug , sbomPath , sourceCodePath := setupTest (ctx , t , ctrl , false )
68
+
69
+ // This should now succeed with proper finding data
70
+ result , err := ostest .RunSbomReachabilityFlow (ctx , mockIctx , mockTestClient , ef , & nopLogger , sbomPath , sourceCodePath , mockBsClient , orgID , orgSlug )
71
+
72
+ require .NoError (t , err )
73
+ require .NotNil (t , result )
74
+ require .Len (t , result , 1 )
75
+ require .Contains (t , result [0 ].GetContentType (), "application/json; schema=test-summary" )
76
+
77
+ legacySummary , ok := result [0 ].GetPayload ().([]byte )
78
+ require .True (t , ok )
79
+ snaps .MatchJSON (t , pathRgxp .ReplaceAll (legacySummary , nil ))
80
+ }
81
+
82
+ //nolint:gocritic // Not important for tests.
83
+ func setupTest (ctx context.Context , t * testing.T , ctrl * gomock.Controller , jsonOutput bool ) (
84
+ workflow.InvocationContext ,
85
+ testapi.TestClient ,
86
+ bundlestore.Client ,
87
+ string ,
88
+ string ,
89
+ string ,
90
+ string ,
91
+ ) {
92
+ t .Helper ()
37
93
sbomPath := "./testdata/bom.json"
38
94
sourceCodePath := "./testdata/test_dir"
39
95
orgID := "test-org-id"
@@ -159,24 +215,12 @@ func Test_RunSbomReachabilityFlow_Success(t *testing.T) {
159
215
160
216
// Mock Invocation Context
161
217
mockConfig := configuration .New ()
162
- mockConfig .Set (outputworkflow .OutputConfigKeyJSON , true )
218
+ mockConfig .Set (outputworkflow .OutputConfigKeyJSON , jsonOutput )
163
219
mockConfig .Set (configuration .ORGANIZATION_SLUG , orgSlug )
164
220
mockIctx := gafmocks .NewMockInvocationContext (ctrl )
165
221
mockIctx .EXPECT ().GetConfiguration ().Return (mockConfig ).AnyTimes ()
222
+ mockIctx .EXPECT ().GetEnhancedLogger ().Return (& nopLogger ).AnyTimes ()
223
+ mockIctx .EXPECT ().GetRuntimeInfo ().Return (runtimeinfo .New ()).AnyTimes ()
166
224
167
- // This should now succeed with proper finding data
168
- result , err := ostest .RunSbomReachabilityFlow (ctx , mockIctx , mockTestClient , ef , & logger , sbomPath , sourceCodePath , mockBsClient , orgID , orgSlug )
169
-
170
- require .NoError (t , err )
171
- require .NotNil (t , result )
172
- require .Len (t , result , 2 ) // Should return legacy data + summary data
173
- require .Contains (t , result [0 ].GetContentType (), "application/json" ) // legacy data
174
- require .Contains (t , result [1 ].GetContentType (), "application/json; schema=test-summary" ) // summary data
175
-
176
- legacyJSON , ok := result [0 ].GetPayload ().([]byte )
177
- require .True (t , ok )
178
- legacySummary , ok := result [1 ].GetPayload ().([]byte )
179
- require .True (t , ok )
180
- snaps .MatchJSON (t , pathRgxp .ReplaceAll (legacyJSON , nil ))
181
- snaps .MatchJSON (t , pathRgxp .ReplaceAll (legacySummary , nil ))
225
+ return mockIctx , mockTestClient , mockBsClient , orgID , orgSlug , sbomPath , sourceCodePath
182
226
}
0 commit comments