From 3ee14bbcb814a0b09ed62c4b054b097d0f9655c8 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Tue, 12 Mar 2024 17:07:14 -0600 Subject: [PATCH] use table driven tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/download/download_test.go | 71 +++++++++---------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/pkg/cmd/attestation/download/download_test.go b/pkg/cmd/attestation/download/download_test.go index aea74aaf0..817a9395c 100644 --- a/pkg/cmd/attestation/download/download_test.go +++ b/pkg/cmd/attestation/download/download_test.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "os" + "path" "testing" "github.com/cli/cli/v2/pkg/cmd/attestation/api" @@ -112,46 +113,42 @@ func TestRunDownload(t *testing.T) { func TestCreateJSONLinesFilePath(t *testing.T) { tempDir := t.TempDir() + artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512") + require.NoError(t, err) - t.Run("with output path", func(t *testing.T) { - artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512") - require.NoError(t, err) - path := createJSONLinesFilePath(artifact.DigestWithAlg(), tempDir) + outputFileName := fmt.Sprintf("%s.jsonl", artifact.DigestWithAlg()) - expectedPath := fmt.Sprintf("%s/%s.jsonl", tempDir, artifact.DigestWithAlg()) - require.Equal(t, expectedPath, path) - }) + testCases := []struct { + name string + outputPath string + expected string + }{ + { + name: "with output path", + outputPath: tempDir, + expected: path.Join(tempDir, outputFileName), + }, + { + name: "with nested output path", + outputPath: path.Join(tempDir, "subdir"), + expected: path.Join(tempDir, "subdir", outputFileName), + }, + { + name: "with output path with beginning slash", + outputPath: path.Join("/", tempDir, "subdir"), + expected: path.Join("/", tempDir, "subdir", outputFileName), + }, + { + name: "without output path", + outputPath: "", + expected: outputFileName, + }, + } - t.Run("with nested output path", func(t *testing.T) { - artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512") - require.NoError(t, err) - - nestedPath := fmt.Sprintf("%s/subdir", tempDir) - path := createJSONLinesFilePath(artifact.DigestWithAlg(), nestedPath) - - expectedPath := fmt.Sprintf("%s/subdir/%s.jsonl", tempDir, artifact.DigestWithAlg()) - require.Equal(t, expectedPath, path) - }) - - t.Run("with output path with beginning slash", func(t *testing.T) { - artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512") - require.NoError(t, err) - - nestedPath := fmt.Sprintf("/%s/subdir", tempDir) - path := createJSONLinesFilePath(artifact.DigestWithAlg(), nestedPath) - - expectedPath := fmt.Sprintf("/%s/subdir/%s.jsonl", tempDir, artifact.DigestWithAlg()) - require.Equal(t, expectedPath, path) - }) - - t.Run("without output path", func(t *testing.T) { - artifact, err := artifact.NewDigestedArtifact(oci.MockClient{}, "../test/data/sigstore-js-2.1.0.tgz", "sha512") - require.NoError(t, err) - path := createJSONLinesFilePath(artifact.DigestWithAlg(), "") - - expectedPath := fmt.Sprintf("%s.jsonl", artifact.DigestWithAlg()) - require.Equal(t, expectedPath, path) - }) + for _, tc := range testCases { + actualPath := createJSONLinesFilePath(artifact.DigestWithAlg(), tc.outputPath) + require.Equal(t, tc.expected, actualPath) + } } func countLines(path string) (int, error) {