■ ■ ■ ■ ■ ■
test/cli/all_formats_convertible_test.go
1 | 1 | | package cli |
2 | 2 | | |
3 | 3 | | import ( |
| 4 | + | "fmt" |
4 | 5 | | "os" |
5 | 6 | | "path/filepath" |
6 | 7 | | "strings" |
| skipped 1 lines |
8 | 9 | | |
9 | 10 | | "github.com/stretchr/testify/require" |
10 | 11 | | ) |
11 | | - | |
12 | | - | type conversion struct { |
13 | | - | To string |
14 | | - | From string |
15 | | - | } |
16 | 12 | | |
17 | 13 | | func TestConvertCmdFlags(t *testing.T) { |
18 | | - | commonAssertions := []traitAssertion{ |
| 14 | + | assertions := []traitAssertion{ |
19 | 15 | | func(tb testing.TB, stdout, _ string, _ int) { |
20 | 16 | | tb.Helper() |
21 | 17 | | if len(stdout) < 1000 { |
| skipped 4 lines |
26 | 22 | | } |
27 | 23 | | |
28 | 24 | | tests := []struct { |
29 | | - | name string |
30 | | - | conversions []conversion |
31 | | - | env map[string]string |
32 | | - | assertions []traitAssertion |
| 25 | + | to string |
| 26 | + | from string |
| 27 | + | template string |
| 28 | + | env map[string]string |
33 | 29 | | }{ |
34 | | - | { |
35 | | - | name: "syft-format convertable to spdx-json", |
36 | | - | conversions: []conversion{ |
37 | | - | {To: "syft-json", From: "spdx-json"}, |
38 | | - | {To: "syft-json", From: "cyclonedx-json"}, |
39 | | - | {To: "spdx-json", From: "syft-json"}, |
40 | | - | {To: "spdx-json", From: "cyclonedx-json"}, |
41 | | - | {To: "cyclonedx-json", From: "syft-json"}, |
42 | | - | {To: "cyclonedx-json", From: "spdx-json"}, |
43 | | - | }, |
44 | | - | assertions: commonAssertions, |
45 | | - | }, |
| 30 | + | {to: "syft-json", from: "spdx-json"}, |
| 31 | + | {to: "syft-json", from: "cyclonedx-json"}, |
| 32 | + | {to: "spdx-json", from: "syft-json"}, |
| 33 | + | {to: "template", from: "syft-json", template: "test-fixtures/csv.template"}, |
| 34 | + | {to: "spdx-json", from: "cyclonedx-json"}, |
| 35 | + | {to: "cyclonedx-json", from: "syft-json"}, |
| 36 | + | {to: "cyclonedx-json", from: "spdx-json"}, |
46 | 37 | | } |
47 | 38 | | |
48 | 39 | | for _, test := range tests { |
49 | | - | t.Run(test.name, func(t *testing.T) { |
50 | | - | for _, c := range test.conversions { |
51 | | - | sbomArgs := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", c.From} |
52 | | - | cmd, stdout, stderr := runSyft(t, test.env, sbomArgs...) |
53 | | - | if cmd.ProcessState.ExitCode() != 0 { |
54 | | - | t.Fatalf("failure executing syft creating an sbom") |
55 | | - | t.Log("STDOUT:\n", stdout) |
56 | | - | t.Log("STDERR:\n", stderr) |
57 | | - | t.Log("COMMAND:", strings.Join(cmd.Args, " ")) |
58 | | - | return |
59 | | - | } |
| 40 | + | t.Run(fmt.Sprintf("from %s to %s", test.from, test.to), func(t *testing.T) { |
| 41 | + | sbomArgs := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", test.from} |
| 42 | + | cmd, stdout, stderr := runSyft(t, test.env, sbomArgs...) |
| 43 | + | if cmd.ProcessState.ExitCode() != 0 { |
| 44 | + | t.Log("STDOUT:\n", stdout) |
| 45 | + | t.Log("STDERR:\n", stderr) |
| 46 | + | t.Log("COMMAND:", strings.Join(cmd.Args, " ")) |
| 47 | + | t.Fatalf("failure executing syft creating an sbom") |
| 48 | + | return |
| 49 | + | } |
60 | 50 | | |
61 | | - | tempDir := t.TempDir() |
62 | | - | sbomFile := filepath.Join(tempDir, "sbom.json") |
63 | | - | require.NoError(t, os.WriteFile(sbomFile, []byte(stdout), 0666)) |
| 51 | + | tempDir := t.TempDir() |
| 52 | + | sbomFile := filepath.Join(tempDir, "sbom.json") |
| 53 | + | require.NoError(t, os.WriteFile(sbomFile, []byte(stdout), 0666)) |
64 | 54 | | |
65 | | - | convertArgs := []string{"convert", sbomFile, "-o", c.To} |
66 | | - | cmd, stdout, stderr = runSyft(t, test.env, convertArgs...) |
67 | | - | for _, traitFn := range test.assertions { |
68 | | - | traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode()) |
69 | | - | } |
70 | | - | if t.Failed() { |
71 | | - | t.Log("STDOUT:\n", stdout) |
72 | | - | t.Log("STDERR:\n", stderr) |
73 | | - | t.Log("COMMAND:", strings.Join(cmd.Args, " ")) |
74 | | - | } |
| 55 | + | convertArgs := []string{"convert", sbomFile, "-o", test.to} |
| 56 | + | if test.template != "" { |
| 57 | + | convertArgs = append(convertArgs, "--template", test.template) |
| 58 | + | } |
| 59 | + | cmd, stdout, stderr = runSyft(t, test.env, convertArgs...) |
| 60 | + | for _, traitFn := range assertions { |
| 61 | + | traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode()) |
| 62 | + | } |
| 63 | + | if t.Failed() { |
| 64 | + | t.Log("STDOUT:\n", stdout) |
| 65 | + | t.Log("STDERR:\n", stderr) |
| 66 | + | t.Log("COMMAND:", strings.Join(cmd.Args, " ")) |
75 | 67 | | } |
76 | 68 | | }) |
77 | 69 | | } |
| skipped 2 lines |