From 325b663da56b3293e807e43bd7c208044bd12c54 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Tue, 25 Jan 2022 11:19:05 +0000 Subject: [PATCH] test: Enhance tests for platform parsing --- cmd/create/imagebundle/platforms_flag_test.go | 59 ++++++++++++++++--- flags/platform.go | 4 -- 2 files changed, 52 insertions(+), 11 deletions(-) delete mode 100644 flags/platform.go diff --git a/cmd/create/imagebundle/platforms_flag_test.go b/cmd/create/imagebundle/platforms_flag_test.go index 7e0bd9e4..de0e7ac2 100644 --- a/cmd/create/imagebundle/platforms_flag_test.go +++ b/cmd/create/imagebundle/platforms_flag_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/spf13/pflag" + "github.com/stretchr/testify/require" ) const argfmt = "--ps=%s" @@ -247,10 +248,7 @@ func TestPSAsSliceValue(t *testing.T) { in := []string{"linux/amd64", "darwin/arm64/v8"} arg1 := fmt.Sprintf(argfmt, in[0]) arg2 := fmt.Sprintf(argfmt, in[1]) - err := f.Parse([]string{arg1, arg2}) - if err != nil { - t.Fatal("expected no error; got", err) - } + require.NoError(t, f.Parse([]string{arg1, arg2}), "error parsing flags") f.VisitAll(func(f *pflag.Flag) { if val, ok := f.Value.(pflag.SliceValue); ok { @@ -258,7 +256,54 @@ func TestPSAsSliceValue(t *testing.T) { } }) expectedPlatform := platform{os: "windows", arch: "arm", variant: "v7"} - if len(ps) != 1 || ps[0] != expectedPlatform { - t.Fatalf("Expected ps to be overwritten with 'windows/arm/v7', but got: %s", ps) - } + require.ElementsMatch(t, []platform{expectedPlatform}, ps, "Expected ps to be overwritten with 'windows/arm/v7'") +} + +func TestPSGetSlice(t *testing.T) { + t.Parallel() + var ps []platform + f := setUpPSFlagSet(&ps) + + in := []string{"linux/amd64", "darwin/arm64/v8"} + arg1 := fmt.Sprintf(argfmt, in[0]) + arg2 := fmt.Sprintf(argfmt, in[1]) + require.NoError(t, f.Parse([]string{arg1, arg2}), "error parsing flags") + + require.ElementsMatch(t, + in, + f.Lookup("ps").Value.(pflag.SliceValue).GetSlice(), + "incorrect platforms", + ) +} + +func TestPSAppend(t *testing.T) { + t.Parallel() + var ps []platform + f := setUpPSFlagSet(&ps) + + in := []string{"linux/amd64", "darwin/arm64/v8"} + arg1 := fmt.Sprintf(argfmt, in[0]) + arg2 := fmt.Sprintf(argfmt, in[1]) + require.NoError(t, f.Parse([]string{arg1, arg2}), "error parsing flags") + + require.NoError(t, f.Lookup("ps").Value.(pflag.SliceValue).Append("windows/i386"), "error appending to platforms") + require.ElementsMatch(t, + append(in, "windows/i386"), + f.Lookup("ps").Value.(pflag.SliceValue).GetSlice(), + "incorrect platforms", + ) +} + +func TestPSInvalidPlatform(t *testing.T) { + t.Parallel() + var ps []platform + f := setUpPSFlagSet(&ps) + + in := []string{"wibble"} + arg1 := fmt.Sprintf(argfmt, in[0]) + require.EqualError(t, f.Parse([]string{arg1}), + `invalid argument "wibble" for "--ps" flag: invalid platform specification: `+ + `wibble (required format: /[/]`, + "expected error parsing flags", + ) } diff --git a/flags/platform.go b/flags/platform.go deleted file mode 100644 index 04b8bc53..00000000 --- a/flags/platform.go +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright 2021 D2iQ, Inc. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package flags