Files
integraci-dagger-modules/sast-scan/semgrep_args_test.go
T

90 lines
2.7 KiB
Go

package main
import (
"strings"
"testing"
)
func hasArg(args []string, want string) bool {
for _, a := range args {
if a == want {
return true
}
}
return false
}
func TestPackRunNeverFetchesRegistryRules(t *testing.T) {
args := semgrepArgs("/tmp/out.sarif", true, false)
for _, a := range args {
if strings.Contains(a, "auto") {
t.Fatalf("a pack run must never use the registry config, got %q in %v", a, args)
}
}
if !hasArg(args, "--config=/rules") {
t.Fatalf("a pack run reads the mounted rules, got %v", args)
}
}
func TestPackRunAlwaysTurnsMetricsOff(t *testing.T) {
args := semgrepArgs("/tmp/out.sarif", true, false)
if !hasArg(args, "--metrics=off") {
t.Fatalf("--metrics=off missing from %v", args)
}
env := map[string]string{}
for _, kv := range semgrepEnv(true) {
env[kv[0]] = kv[1]
}
if env["SEMGREP_SEND_METRICS"] != "off" || env["SEMGREP_ENABLE_VERSION_CHECK"] != "0" {
t.Fatalf("pack run env must disable metrics and the version check, got %v", env)
}
}
func TestLegacyRunKeepsAutoWithoutMetricsOff(t *testing.T) {
// semgrep exits 2 on `--config=auto --metrics=off`; the legacy path must not
// carry the pair (the tightening mutant: always add --metrics=off).
args := semgrepArgs("/tmp/out.sarif", false, false)
if !hasArg(args, "--config=auto") {
t.Fatalf("legacy run lost --config=auto: %v", args)
}
if hasArg(args, "--metrics=off") {
t.Fatalf("semgrep refuses --metrics=off with --config=auto: %v", args)
}
if len(semgrepEnv(false)) != 0 {
t.Fatalf("legacy run must not set the metrics env either")
}
}
func TestBothModesWriteSarifAndKeepFindingsAsExitCode(t *testing.T) {
for _, pack := range []bool{true, false} {
for _, record := range []bool{true, false} {
args := semgrepArgs("/tmp/x.sarif", pack, record)
if args[0] != "semgrep" || args[1] != "scan" || args[len(args)-1] != "." {
t.Fatalf("unexpected shape %v", args)
}
for _, want := range []string{"--sarif", "--error", "/tmp/x.sarif"} {
if !hasArg(args, want) {
t.Fatalf("%s missing from %v", want, args)
}
}
}
}
}
// The scan of record ignores in-repo suppressions in BOTH rule modes, and no
// other run does (ADR-0093 §4, WP-05).
func TestScanOfRecordDisablesNosemInBothModes(t *testing.T) {
for _, pack := range []bool{true, false} {
args := semgrepArgs("/tmp/x.sarif", pack, true)
for _, want := range []string{"--disable-nosem", "--no-git-ignore"} {
if !hasArg(args, want) {
t.Fatalf("scan of record (pack=%v) lost %s: %v", pack, want, args)
}
}
other := semgrepArgs("/tmp/x.sarif", pack, false)
if hasArg(other, "--disable-nosem") || hasArg(other, "--no-git-ignore") {
t.Fatalf("a non-record run (pack=%v) must keep nosemgrep: %v", pack, other)
}
}
}