121 lines
4.0 KiB
Go
121 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func kinds(r LockfileReport) []string {
|
|
var out []string
|
|
for _, f := range r.Findings {
|
|
out = append(out, f.Kind+":"+f.Manifest)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Mutant: treat "no lockfile" as a pass -> this test fails.
|
|
func TestPoetryWithoutLockIsMissing(t *testing.T) {
|
|
r := CheckLockfiles(map[string]string{"pyproject.toml": "[tool.poetry]\nname='x'\n"}, nil)
|
|
if r.Outcome != "ran" || len(r.Findings) != 1 || r.Findings[0].Kind != "lockfile_missing" {
|
|
t.Fatalf("got %+v", r)
|
|
}
|
|
}
|
|
|
|
// Mutant: report "no ecosystem" as could_not_run -> this test fails.
|
|
func TestStaticSiteIsNotApplicable(t *testing.T) {
|
|
r := CheckLockfiles(map[string]string{"index.html": "<html/>", "css/site.css": ""}, nil)
|
|
if r.Outcome != "not_applicable" || len(r.Findings) != 0 {
|
|
t.Fatalf("got %+v", r)
|
|
}
|
|
}
|
|
|
|
func TestLockedEcosystemsPass(t *testing.T) {
|
|
files := map[string]string{
|
|
"pyproject.toml": "[tool.uv]\n", "uv.lock": "",
|
|
"go.mod": "module x\nrequire github.com/a/b v1.0.0\n", "go.sum": "",
|
|
"Cargo.toml": "", "Cargo.lock": "",
|
|
"web/package.json": `{"dependencies":{"react":"18.3.1"}}`,
|
|
"web/package-lock.json": `{"packages":{"":{"dependencies":{"react":"18.3.1"}}}}`,
|
|
}
|
|
if r := CheckLockfiles(files, nil); len(r.Findings) != 0 {
|
|
t.Fatalf("unexpected findings %v", kinds(r))
|
|
}
|
|
}
|
|
|
|
func TestNpmLockOutOfSync(t *testing.T) {
|
|
files := map[string]string{
|
|
"package.json": `{"dependencies":{"react":"18.3.1","axios":"1.7.0"}}`,
|
|
"package-lock.json": `{"packages":{"":{"dependencies":{"react":"18.3.1"}}}}`,
|
|
}
|
|
r := CheckLockfiles(files, nil)
|
|
if len(r.Findings) != 1 || r.Findings[0].Kind != "lockfile_out_of_sync" || !strings.Contains(r.Findings[0].Detail, "axios") {
|
|
t.Fatalf("got %+v", r.Findings)
|
|
}
|
|
}
|
|
|
|
func TestLockChangedWithoutManifest(t *testing.T) {
|
|
files := map[string]string{"composer.json": "{}", "composer.lock": "{}"}
|
|
r := CheckLockfiles(files, []string{"composer.lock"})
|
|
if len(r.Findings) != 1 || r.Findings[0].Kind != "lockfile_changed_without_manifest" {
|
|
t.Fatalf("got %+v", r.Findings)
|
|
}
|
|
if r := CheckLockfiles(files, []string{"composer.lock", "composer.json"}); len(r.Findings) != 0 {
|
|
t.Fatalf("both changed is in sync: %+v", r.Findings)
|
|
}
|
|
}
|
|
|
|
func TestVendoredManifestsAreIgnored(t *testing.T) {
|
|
r := CheckLockfiles(map[string]string{"node_modules/x/package.json": "{}"}, nil)
|
|
if r.Outcome != "not_applicable" {
|
|
t.Fatalf("got %+v", r)
|
|
}
|
|
}
|
|
|
|
func TestGoModWithoutRequiresNeedsNoSum(t *testing.T) {
|
|
if r := CheckLockfiles(map[string]string{"go.mod": "module x\n\ngo 1.25\n"}, nil); len(r.Findings) != 0 {
|
|
t.Fatalf("got %+v", r.Findings)
|
|
}
|
|
}
|
|
|
|
func TestPinning(t *testing.T) {
|
|
files := map[string]string{
|
|
"Dockerfile": "FROM golang:1.25 AS build\nFROM build AS test\nFROM gcr.io/distroless/static@sha256:" +
|
|
strings.Repeat("a", 64) + "\nFROM scratch\nFROM ${BASE}\n",
|
|
".github/workflows/ci.yml": "steps:\n - uses: actions/checkout@v4\n - uses: actions/setup-go@" +
|
|
strings.Repeat("b", 40) + "\n - uses: ./local-action\n",
|
|
"package.json": `{"dependencies":{"react":"^18.3.1","axios":"1.7.0","x":"workspace:*"}}`,
|
|
"requirements.txt": "requests==2.32.3\nflask>=3\n# comment\n-r other.txt\n",
|
|
}
|
|
got := map[string]bool{}
|
|
for _, f := range CheckPinning(files) {
|
|
got[f.Kind+" "+f.Ref] = true
|
|
}
|
|
want := []string{
|
|
"unpinned_from golang:1.25",
|
|
"unpinned_uses actions/checkout@v4",
|
|
"floating_range react@^18.3.1",
|
|
"floating_range flask>=3",
|
|
}
|
|
for _, w := range want {
|
|
if !got[w] {
|
|
t.Errorf("missing finding %q in %v", w, got)
|
|
}
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Errorf("unexpected findings: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestSpectralCounts(t *testing.T) {
|
|
c, err := spectralCounts(`[{"severity":0},{"severity":1},{"severity":3}]`)
|
|
if err != nil || c != [3]int{1, 1, 1} {
|
|
t.Fatalf("got %v %v", c, err)
|
|
}
|
|
if c, err := spectralCounts("[]"); err != nil || c != [3]int{} {
|
|
t.Fatalf("clean: %v %v", c, err)
|
|
}
|
|
if _, err := spectralCounts("Error: spectral crashed"); err == nil {
|
|
t.Fatal("an unreadable report must be could_not_run, not a clean lint")
|
|
}
|
|
}
|