Skip to content

Commit 4f48c4d

Browse files
committed
fix: add no instruction tests, change formatting
1 parent 303557a commit 4f48c4d

File tree

4 files changed

+141
-14
lines changed

4 files changed

+141
-14
lines changed

internal/presenters/__snapshots__/presenter_unified_finding_test.snap

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ License issues: 1
1515
✗ [MEDIUM] LGPL-3.0 license
1616
Finding ID: snyk:lic:npm:web3-core:LGPL-3.0
1717
Info: https://snyk.io/vuln/snyk:lic:npm:web3-core:LGPL-3.0
18-
Instructions: LGPL-3.0: This license requires source code disclosure when modified.
18+
Legal instructions:
19+
for LGPL-3.0: This license requires source code disclosure when modified.
1920

2021

2122
╭─────────────────────────────────────────────────────────╮
@@ -49,8 +50,9 @@ License issues: 1
4950
✗ [HIGH] GPL-3.0 OR MIT license
5051
Finding ID: snyk:lic:npm:dual-pkg:GPL-3.0-OR-MIT
5152
Info: https://snyk.io/vuln/snyk:lic:npm:dual-pkg:GPL-3.0-OR-MIT
52-
Instructions: GPL-3.0: Strong copyleft license. Requires source code disclosure for modifications.
53-
MIT: Permissive license. Must include original copyright notice.
53+
Legal instructions:
54+
for GPL-3.0: Strong copyleft license. Requires source code disclosure for modifications.
55+
for MIT: Permissive license. Must include original copyright notice.
5456

5557

5658
╭─────────────────────────────────────────────────────────╮
@@ -70,3 +72,32 @@ License issues: 1
7072

7173

7274
---
75+
76+
[TestUnifiedFindingPresenter_CliOutput/snapshot_test_with_license_without_instructions - 1]
77+
78+
Testing ...
79+
80+
License issues: 1
81+
82+
✗ [MEDIUM] Apache-2.0 license
83+
Finding ID: snyk:lic:npm:test-pkg:Apache-2.0
84+
Info: https://snyk.io/vuln/snyk:lic:npm:test-pkg:Apache-2.0
85+
86+
87+
╭─────────────────────────────────────────────────────────╮
88+
Test Summary
89+
│ │
90+
Organization: │
91+
Test type: open-source
92+
Project path: │
93+
│ │
94+
Total license issues: 1
95+
Ignored: 0 [ 0 CRITICAL 0 HIGH 0 MEDIUM 0 LOW ] │
96+
Open : 1 [ 0 CRITICAL 0 HIGH 1 MEDIUM 0 LOW ] │
97+
╰─────────────────────────────────────────────────────────╯
98+
💡 Tip
99+
100+
To view ignored issues, use the --include-ignores option.
101+
102+
103+
---

internal/presenters/funcs.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717

1818
const (
1919
notApplicable = "N/A"
20-
// Template field indentation: 3 spaces + "Instructions: " = 17 spaces for continuation.
21-
instructionsContinuationIndent = " "
20+
bulletPoint = "○"
2221
)
2322

2423
// add returns the sum of two integers.
@@ -316,8 +315,6 @@ func isLicenseFinding(finding testapi.FindingData) bool {
316315
}
317316

318317
// getLicenseInstructions returns license instructions for a license finding.
319-
// For packages with multiple licenses, instructions are formatted as "License: instruction"
320-
// on separate lines.
321318
func getLicenseInstructions(finding testapi.FindingData) string {
322319
if finding.Attributes == nil {
323320
return ""
@@ -344,22 +341,21 @@ func getLicenseInstructions(finding testapi.FindingData) string {
344341

345342
instructions := buildInstructionsList(p.Instructions)
346343
if len(instructions) > 0 {
347-
return strings.Join(instructions, "\n"+instructionsContinuationIndent)
344+
return "\n" + strings.Join(instructions, "\n")
348345
}
349346
}
350347
return ""
351348
}
352349

353-
// buildInstructionsList formats license instructions for display.
354-
// Each instruction is prefixed with its license name for clarity.
350+
// buildInstructionsList formats license instructions prefixing with a bullet point and license name.
355351
func buildInstructionsList(instructionsList []testapi.SnykvulndbLicenseInstructions) []string {
356352
instructions := make([]string, 0, len(instructionsList))
357353

358354
for _, inst := range instructionsList {
359355
if inst.Content == "" {
360356
continue
361357
}
362-
instructions = append(instructions, fmt.Sprintf("%s: %s", inst.License, inst.Content))
358+
instructions = append(instructions, fmt.Sprintf(" %s for %s: %s", bulletPoint, inst.License, inst.Content))
363359
}
364360
return instructions
365361
}

internal/presenters/presenter_unified_finding_test.go

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,60 @@ func TestUnifiedFindingPresenter_CliOutput(t *testing.T) {
318318
snaps.MatchSnapshot(t, buffer.String())
319319
})
320320

321+
t.Run("snapshot test with license without instructions", func(t *testing.T) {
322+
config := configuration.New()
323+
buffer := &bytes.Buffer{}
324+
lipgloss.SetColorProfile(termenv.Ascii)
325+
326+
// Create a license finding without instructions
327+
licenseFinding := testapi.FindingData{
328+
Id: util.Ptr(uuid.MustParse("55555555-5555-5555-5555-555555555555")),
329+
Type: util.Ptr(testapi.Findings),
330+
Attributes: &testapi.FindingAttributes{
331+
Title: "Apache-2.0 license",
332+
Rating: testapi.Rating{
333+
Severity: testapi.Severity("medium"),
334+
},
335+
Problems: func() []testapi.Problem {
336+
var p testapi.Problem
337+
err := p.FromSnykLicenseProblem(testapi.SnykLicenseProblem{
338+
Id: "snyk:lic:npm:test-pkg:Apache-2.0",
339+
License: "Apache-2.0",
340+
Instructions: []testapi.SnykvulndbLicenseInstructions{}, // No instructions
341+
})
342+
assert.NoError(t, err)
343+
return []testapi.Problem{p}
344+
}(),
345+
},
346+
}
347+
348+
projectResult := &presenters.UnifiedProjectResult{
349+
Findings: []testapi.FindingData{licenseFinding},
350+
Summary: &json_schemas.TestSummary{
351+
Type: "open-source",
352+
Path: "test/path",
353+
SeverityOrderAsc: []string{"low", "medium", "high", "critical"},
354+
Results: []json_schemas.TestSummaryResult{
355+
{
356+
Severity: "medium",
357+
Open: 1,
358+
Total: 1,
359+
},
360+
},
361+
},
362+
}
363+
364+
presenter := presenters.NewUnifiedFindingsRenderer(
365+
[]*presenters.UnifiedProjectResult{projectResult},
366+
config,
367+
buffer,
368+
)
369+
370+
err := presenter.RenderTemplate(presenters.DefaultTemplateFiles, presenters.DefaultMimeType)
371+
assert.NoError(t, err)
372+
snaps.MatchSnapshot(t, buffer.String())
373+
})
374+
321375
// summary shows security only when there are vulnerability findings and no license findings
322376
t.Run("summary shows only security when no license issues", func(t *testing.T) {
323377
config := configuration.New()
@@ -553,6 +607,52 @@ func TestUnifiedFindingPresenter_LicenseInstructions(t *testing.T) {
553607
assert.NoError(t, err)
554608

555609
out := buffer.String()
556-
assert.Contains(t, out, "Instructions:")
557-
assert.Contains(t, out, "This license requires you to disclose source code changes.")
610+
assert.Contains(t, out, "Legal instructions:")
611+
assert.Contains(t, out, "○ for LGPL-3.0: This license requires you to disclose source code changes.")
612+
}
613+
614+
// TestUnifiedFindingPresenter_LicenseWithoutInstructions verifies that license findings without instructions don't show the instructions field.
615+
func TestUnifiedFindingPresenter_LicenseWithoutInstructions(t *testing.T) {
616+
config := configuration.New()
617+
buffer := &bytes.Buffer{}
618+
lipgloss.SetColorProfile(termenv.Ascii)
619+
620+
licenseFinding := testapi.FindingData{
621+
Id: util.Ptr(uuid.New()),
622+
Type: util.Ptr(testapi.Findings),
623+
Attributes: &testapi.FindingAttributes{
624+
Title: "MIT license",
625+
Rating: testapi.Rating{
626+
Severity: testapi.Severity("low"),
627+
},
628+
Problems: func() []testapi.Problem {
629+
var p testapi.Problem
630+
err := p.FromSnykLicenseProblem(testapi.SnykLicenseProblem{
631+
Id: "snyk:lic:npm:test-pkg:MIT",
632+
License: "MIT",
633+
Instructions: []testapi.SnykvulndbLicenseInstructions{},
634+
})
635+
assert.NoError(t, err)
636+
return []testapi.Problem{p}
637+
}(),
638+
},
639+
}
640+
641+
projectResult := &presenters.UnifiedProjectResult{
642+
Findings: []testapi.FindingData{licenseFinding},
643+
Summary: &json_schemas.TestSummary{
644+
Type: "open-source",
645+
Path: "test/path",
646+
SeverityOrderAsc: []string{"low", "medium", "high", "critical"},
647+
Results: []json_schemas.TestSummaryResult{{Severity: "low", Open: 1, Total: 1}},
648+
},
649+
}
650+
651+
presenter := presenters.NewUnifiedFindingsRenderer([]*presenters.UnifiedProjectResult{projectResult}, config, buffer)
652+
err := presenter.RenderTemplate(presenters.DefaultTemplateFiles, presenters.DefaultMimeType)
653+
assert.NoError(t, err)
654+
655+
out := buffer.String()
656+
assert.NotContains(t, out, "Legal instructions:")
657+
assert.Contains(t, out, "MIT license")
558658
}

internal/presenters/templates/unified_finding.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
{{- if isLicenseFinding . }}
2828
{{- with (getLicenseInstructions .) }}
29-
Instructions: {{ . }}
29+
Legal instructions:{{ . }}
3030
{{- end }}
3131
{{- end }}
3232

0 commit comments

Comments
 (0)