internal/deps/report_test.go
78 lines · 2899 bytes
1package deps
2
3import (
4 "strings"
5 "testing"
6
7 "gitbay.org/gitbay/internal/store"
8)
9
10// The body is read as a rendered table on the web and as the plain text of
11// the notification mail, so every row has to line up on its own.
12func TestBodyAlignsColumns(t *testing.T) {
13 body := Body("main", []store.DepReport{
14 {Ecosystem: EcoGo, Name: "go.yaml.in/yaml/v3", Current: "v3.0.4", Latest: "v3.0.5"},
15 {Ecosystem: EcoGo, Name: "golang.org/x/net", Current: "v0.57.0", Latest: "v0.58.0"},
16 {Ecosystem: EcoNPM, Name: "react", Current: "18.2.0", Latest: "19.0.0"},
17 })
18 var pipes []int
19 for _, line := range strings.Split(body, "\n") {
20 if !strings.HasPrefix(line, "|") {
21 pipes = append(pipes, -1) // section break
22 continue
23 }
24 pipes = append(pipes, len(line))
25 }
26 // Within a run of table lines every line is the same length.
27 var run []int
28 for _, n := range append(pipes, -1) {
29 if n == -1 {
30 for _, w := range run {
31 if w != run[0] {
32 t.Fatalf("ragged table in:\n%s", body)
33 }
34 }
35 run = nil
36 continue
37 }
38 run = append(run, n)
39 }
40 if !strings.Contains(body, "| `golang.org/x/net` | v0.57.0 | v0.58.0 |") {
41 t.Errorf("row not padded to its column:\n%s", body)
42 }
43}
44
45func TestAdvisoryBody(t *testing.T) {
46 body := AdvisoryBody(AdvisoryReport{Repo: "krz/x", Branch: "main", Open: []store.DepAdvisory{
47 {Ecosystem: EcoGo, Name: "golang.org/x/net", Version: "v0.1.0", ID: "GO-2", Summary: "HTTP/2 reset", Severity: "", Fixed: "0.7.0"},
48 {Ecosystem: EcoPyPI, Name: "flask", Version: "2.0.0", ID: "GHSA-aaaa", Summary: "cookie leak", Severity: "high", Fixed: "2.2.5"},
49 }, Dismissed: 2, NotImported: []store.DepAdvisory{{Ecosystem: EcoGo, Name: "golang.org/x/crypto", ID: "GO-3"}}})
50 for _, want := range []string{
51 "| `golang.org/x/net` | v0.1.0 | GO-2 | | 0.7.0 |",
52 "- GHSA-aaaa: cookie leak https://osv.dev/vulnerability/GHSA-aaaa",
53 "### PyPI",
54 "Not listed: 2 dismissed; for Go packages no file here imports: GO-3 (`golang.org/x/crypto`).",
55 "repo deps dismiss krz/x <advisory>",
56 } {
57 if !strings.Contains(body, want) {
58 t.Errorf("body missing %q:\n%s", want, body)
59 }
60 }
61}
62
63// An advisory naming packages is set aside unless one of them is imported;
64// one naming none, or of another ecosystem, is left alone.
65func TestMarkNotImported(t *testing.T) {
66 matched := []store.DepAdvisory{
67 {Ecosystem: EcoGo, ID: "GO-1", Imports: []string{"golang.org/x/crypto/openpgp", "golang.org/x/crypto/openpgp/armor"}},
68 {Ecosystem: EcoGo, ID: "GO-2", Imports: []string{"golang.org/x/crypto/ssh"}},
69 {Ecosystem: EcoGo, ID: "GO-3"},
70 {Ecosystem: EcoNPM, ID: "GHSA-1", Imports: []string{"x"}},
71 }
72 MarkNotImported(matched, map[string]bool{"golang.org/x/crypto/ssh": true})
73 for i, want := range []bool{true, false, false, false} {
74 if matched[i].NotImported != want {
75 t.Errorf("%s: NotImported = %v", matched[i].ID, matched[i].NotImported)
76 }
77 }
78}