github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/lintcmd/config_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package lintcmd
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  var buildConfigTests = []struct {
    11  	in      string
    12  	name    string
    13  	envs    []string
    14  	flags   []string
    15  	invalid bool
    16  }{
    17  	{
    18  		`some_name: ENV1=foo ENV_2=bar ENV3="foo bar baz" ENV4=foo"bar" -flag1 -flag2= -flag3=value -flag4="some value" -flag5=some" value "test "-flag6=1"`,
    19  		"some_name",
    20  		[]string{"ENV1=foo", "ENV_2=bar", "ENV3=foo bar baz", "ENV4=foobar"},
    21  		[]string{"-flag1", "-flag2=", "-flag3=value", "-flag4=some value", "-flag5=some value test", "-flag6=1"},
    22  		false,
    23  	},
    24  	{
    25  		`some_name: ENV1=foo -tags bar baz=meow`,
    26  		"some_name",
    27  		[]string{"ENV1=foo"},
    28  		[]string{"-tags", "bar", "baz=meow"},
    29  		false,
    30  	},
    31  	{
    32  		"some_name:",
    33  		"some_name",
    34  		nil,
    35  		nil,
    36  		false,
    37  	},
    38  	{
    39  		"some name:",
    40  		"",
    41  		nil,
    42  		nil,
    43  		true,
    44  	},
    45  	{
    46  		"",
    47  		"",
    48  		nil,
    49  		nil,
    50  		true,
    51  	},
    52  }
    53  
    54  func FuzzParseBuildConfig(f *testing.F) {
    55  	equal := func(a, b []string) bool {
    56  		if len(a) != len(b) {
    57  			return false
    58  		}
    59  		for i := range a {
    60  			if a[i] != b[i] {
    61  				return false
    62  			}
    63  		}
    64  		return true
    65  	}
    66  
    67  	for _, tt := range buildConfigTests {
    68  		f.Add(tt.in)
    69  
    70  		name, envs, flags, err := parseBuildConfig(tt.in)
    71  		if err != nil {
    72  			if tt.invalid {
    73  				continue
    74  			}
    75  			f.Fatalf("input %q failed to parse: %s", tt.in, err)
    76  		}
    77  		if tt.invalid {
    78  			f.Fatalf("expected input %q to fail but it didn't", tt.in)
    79  		}
    80  
    81  		if name != tt.name {
    82  			f.Fatalf("got name %q, want %q", name, tt.name)
    83  		}
    84  		if !equal(envs, tt.envs) {
    85  			f.Fatalf("got environment %#v, want %#v", envs, tt.envs)
    86  		}
    87  		if !equal(flags, tt.flags) {
    88  			f.Fatalf("got flags %#v, want %#v", flags, tt.flags)
    89  		}
    90  	}
    91  
    92  	f.Fuzz(func(t *testing.T, in string) {
    93  		parseBuildConfig(in)
    94  	})
    95  }