github.com/yogeshkumararora/slsa-github-generator@v1.10.1-0.20240520161934-11278bd5afb4/internal/builders/go/pkg/config_test.go (about)

     1  // Copyright 2022 SLSA Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pkg
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  )
    24  
    25  func errInvalidDirectoryFunc(t *testing.T, got error) {
    26  	want := ErrInvalidDirectory
    27  	if !errors.Is(got, want) {
    28  		t.Fatalf("unexpected error: %v", cmp.Diff(got, want, cmpopts.EquateErrors()))
    29  	}
    30  }
    31  
    32  func errUnsupportedVersionFunc(t *testing.T, got error) {
    33  	want := ErrUnsupportedVersion
    34  	if !errors.Is(got, want) {
    35  		t.Fatalf("unexpected error: %v", cmp.Diff(got, want, cmpopts.EquateErrors()))
    36  	}
    37  }
    38  
    39  func errInvalidEnvironmentVariableFunc(t *testing.T, got error) {
    40  	want := ErrInvalidEnvironmentVariable
    41  	if !errors.Is(got, want) {
    42  		t.Fatalf("unexpected error: %v", cmp.Diff(got, want, cmpopts.EquateErrors()))
    43  	}
    44  }
    45  
    46  func Test_ConfigFromFile(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	tests := []struct {
    50  		name   string
    51  		path   string
    52  		err    func(*testing.T, error)
    53  		config GoReleaserConfig
    54  	}{
    55  		{
    56  			name: "valid releaser empty main",
    57  			path: "./testdata/releaser-valid-empty-main.yml",
    58  			config: GoReleaserConfig{
    59  				Goos: "linux", Goarch: "amd64",
    60  				Flags:   []string{"-trimpath", "-tags=netgo"},
    61  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
    62  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
    63  				Env: map[string]string{
    64  					"GO111MODULE": "on", "CGO_ENABLED": "0",
    65  				},
    66  			},
    67  		},
    68  		{
    69  			name: "valid releaser no main",
    70  			path: "./testdata/releaser-valid-no-main.yml",
    71  			config: GoReleaserConfig{
    72  				Goos: "linux", Goarch: "amd64",
    73  				Flags:   []string{"-trimpath", "-tags=netgo"},
    74  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
    75  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
    76  				Env: map[string]string{
    77  					"GO111MODULE": "on", "CGO_ENABLED": "0",
    78  				},
    79  			},
    80  		},
    81  		{
    82  			name: "valid releaser main",
    83  			path: "./testdata/releaser-valid-main.yml",
    84  			config: GoReleaserConfig{
    85  				Goos: "linux", Goarch: "amd64",
    86  				Flags:   []string{"-trimpath", "-tags=netgo"},
    87  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
    88  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
    89  				Env: map[string]string{
    90  					"GO111MODULE": "on", "CGO_ENABLED": "0",
    91  				},
    92  				Main: asPointer("./relative/main.go"),
    93  			},
    94  		},
    95  		{
    96  			name: "valid env var with no value",
    97  			path: "./testdata/releaser-valid-envs-no-value.yml",
    98  			config: GoReleaserConfig{
    99  				Goos: "linux", Goarch: "amd64",
   100  				Flags:   []string{"-trimpath", "-tags=netgo"},
   101  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
   102  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
   103  				Env: map[string]string{
   104  					"GO111MODULE": "on", "CGO_ENABLED": "0", "CGO_CFLAGS": "",
   105  				},
   106  			},
   107  		},
   108  		{
   109  			name: "valid env var with multiple = signs",
   110  			path: "./testdata/releaser-valid-envs-multiple-equal-signs.yml",
   111  			config: GoReleaserConfig{
   112  				Goos: "linux", Goarch: "amd64",
   113  				Flags:   []string{"-trimpath", "-tags=netgo"},
   114  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
   115  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
   116  				Env: map[string]string{
   117  					"GO111MODULE": "on", "CGO_ENABLED": "0", "CGO_CFLAGS": "a=b=c",
   118  				},
   119  			},
   120  		},
   121  		{
   122  			name: "missing version",
   123  			path: "./testdata/releaser-noversion.yml",
   124  			err:  errUnsupportedVersionFunc,
   125  		},
   126  		{
   127  			name: "invalid version",
   128  			path: "./testdata/releaser-invalid-version.yml",
   129  			err:  errUnsupportedVersionFunc,
   130  		},
   131  		{
   132  			name: "invalid envs",
   133  			path: "./testdata/releaser-invalid-envs.yml",
   134  			err:  errInvalidEnvironmentVariableFunc,
   135  		},
   136  		{
   137  			name: "invalid main",
   138  			path: "./testdata/releaser-invalid-main.yml",
   139  			err:  errInvalidDirectoryFunc,
   140  		},
   141  		{
   142  			name: "invalid main path",
   143  			path: "../testdata/releaser-invalid-main.yml",
   144  			err:  errInvalidDirectoryFunc,
   145  		},
   146  		{
   147  			name: "invalid dir path",
   148  			path: "../testdata/releaser-invalid-dir.yml",
   149  			err:  errInvalidDirectoryFunc,
   150  		},
   151  		{
   152  			name: "valid dir path",
   153  			path: "./testdata/releaser-valid-dir.yml",
   154  			config: GoReleaserConfig{
   155  				Goos: "linux", Goarch: "amd64",
   156  				Flags:   []string{"-trimpath", "-tags=netgo"},
   157  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
   158  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
   159  				Env: map[string]string{
   160  					"GO111MODULE": "on", "CGO_ENABLED": "0",
   161  				},
   162  				Dir: asPointer("./path/to/dir"),
   163  			},
   164  		},
   165  		{
   166  			name: "valid config path with dots",
   167  			// Resolves to "./testdata/releaser-valid-dir.yml".
   168  			path: "./testdata/../testdata/./releaser-valid-dir.yml",
   169  			config: GoReleaserConfig{
   170  				Goos: "linux", Goarch: "amd64",
   171  				Flags:   []string{"-trimpath", "-tags=netgo"},
   172  				Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
   173  				Binary:  "binary-{{ .OS }}-{{ .Arch }}",
   174  				Env: map[string]string{
   175  					"GO111MODULE": "on", "CGO_ENABLED": "0",
   176  				},
   177  				Dir: asPointer("./path/to/dir"),
   178  			},
   179  		},
   180  		{
   181  			name: "invalid config path with dots",
   182  			// Resolves to "../releaser-valid-dir.yml".
   183  			path: "./testdata/../testdata/./foo/../../../releaser-valid-dir.yml",
   184  			err:  errInvalidDirectoryFunc,
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		tt := tt // Re-initializing variable so it is not changed while executing the closure below
   189  		t.Run(tt.name, func(t *testing.T) {
   190  			t.Parallel()
   191  
   192  			cfg, err := ConfigFromFile(tt.path)
   193  
   194  			if tt.err != nil {
   195  				tt.err(t, err)
   196  			}
   197  			if err != nil {
   198  				return
   199  			}
   200  
   201  			if !cmp.Equal(*cfg, tt.config) {
   202  				t.Errorf(cmp.Diff(*cfg, tt.config))
   203  			}
   204  		})
   205  	}
   206  }