testcmd

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package testcmd provides utilities for testing osv-scanner CLI commands.

Index

Constants

This section is empty.

Variables

View Source
var (
	// OnlyIDVulnsRule simplifies vulnerabilities to only their ID
	OnlyIDVulnsRule = JSONReplaceRule{
		Path: "results.#.packages.#.vulnerabilities",
		ReplaceFunc: func(toReplace gjson.Result) any {
			return toReplace.Get("#.id").Value()
		},
	}
	// GroupsAsArrayLen replaces the groups array with its length
	GroupsAsArrayLen = JSONReplaceRule{
		Path: "results.#.packages.#.groups",
		ReplaceFunc: func(toReplace gjson.Result) any {
			if toReplace.IsArray() {
				return len(toReplace.Array())
			}

			return 0
		},
	}
	// OnlyFirstBaseImage simplifies the array of base images to only the first one
	OnlyFirstBaseImage = JSONReplaceRule{
		Path: "image_metadata.base_images.#",
		ReplaceFunc: func(toReplace gjson.Result) any {
			if toReplace.IsArray() && len(toReplace.Array()) >= 1 {
				return toReplace.Array()[0].Value()
			}

			return struct{}{}
		},
	}
	// AnyDiffID truncates diff ids in image layer metadata to just `sha256:...`
	AnyDiffID = JSONReplaceRule{
		Path: "image_metadata.layer_metadata.#.diff_id",
		ReplaceFunc: func(toReplace gjson.Result) any {
			if len(toReplace.String()) > 7 {
				return toReplace.String()[:7] + "..."
			}

			return ""
		},
	}
	// ShortenHistoryCommandLength truncates COMMAND data to 28 characters
	ShortenHistoryCommandLength = JSONReplaceRule{
		Path: "image_metadata.layer_metadata.#.command",
		ReplaceFunc: func(toReplace gjson.Result) any {
			if len(toReplace.String()) > 28 {
				return toReplace.String()[:25] + "..."
			}

			return toReplace.String()
		},
	}
	// NormalizeHistoryCommand replaces COMMAND data to be consistent
	// across different versions of docker
	NormalizeHistoryCommand = JSONReplaceRule{
		Path: "image_metadata.layer_metadata.#.command",
		ReplaceFunc: func(toReplace gjson.Result) any {
			str := toReplace.String()
			nopMatcher := cachedregexp.MustCompile(`^/bin/sh -c #\(nop\)\s+`)
			runMatcher := cachedregexp.MustCompile(`^/bin/sh -c\s+`)
			str = nopMatcher.ReplaceAllLiteralString(str, "")
			str = runMatcher.ReplaceAllString(str, "RUN \\0")

			return str
		},
	}

	// NormalizeCreateDateSPDX replaces the created date with a placeholder date
	NormalizeCreateDateSPDX = JSONReplaceRule{
		Path: "creationInfo.created",
		ReplaceFunc: func(_ gjson.Result) any {
			return "2025-01-01T01:01:01Z"
		},
	}
)
View Source
var CommandsUnderTest []cmd.CommandBuilder

CommandsUnderTest should be set in TestMain by every cmd package test

Functions

func CopyFileFlagTo added in v2.2.0

func CopyFileFlagTo(t *testing.T, tc Case, flagName string, dir string) string

CopyFileFlagTo creates a copy of the file pointed to by the given flag (if present in the test case arguments) in the given directory, updating all references in the arguments before returning the new path.

Values that include "does_not_exist" are assumed to be for testing when the flag is given the path to a file or directory that does not exist, and so are ignored as if the flag was not given a value at all

func RunAndMatchSnapshots

func RunAndMatchSnapshots(t *testing.T, tc Case)

func RunAndNormalize added in v2.2.0

func RunAndNormalize(t *testing.T, tc Case) (string, string)

func SetupGitFixtures added in v2.2.2

func SetupGitFixtures() (func(), error)

Types

type Case

type Case struct {
	Name string
	Args []string
	Exit int

	// ReplaceRules are only used for JSON output
	ReplaceRules []JSONReplaceRule
}

type JSONReplaceRule

type JSONReplaceRule struct {
	Path        string
	ReplaceFunc func(toReplace gjson.Result) any
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL