1
0
mirror of https://github.com/ko-build/ko.git synced 2025-07-06 23:36:54 +02:00

Validate KO_CONFIG_PATH (#471)

* Validate KO_CONFIG_PATH to avoid ignored options

* Remove dup
This commit is contained in:
Nghia Tran
2022-02-08 11:28:53 -08:00
committed by GitHub
parent 1425e4be74
commit 3e5ee5b718
3 changed files with 54 additions and 2 deletions

View File

@ -15,6 +15,8 @@
package options
import (
"os"
"strings"
"testing"
"github.com/google/ko/pkg/build"
@ -97,3 +99,41 @@ func TestAddBuildOptionsSetsDefaultsForNonFlagOptions(t *testing.T) {
t.Error("expected Trimpath=true")
}
}
func TestOverrideConfigPath(t *testing.T) {
const envName = "KO_CONFIG_PATH"
bo := &BuildOptions{}
for _, tc := range []struct {
name string
koConfigPath string
err string
}{{
name: ".ko.yaml does not exist",
koConfigPath: "testdata",
err: "testdata/.ko.yaml: no such file or directory",
}, {
name: ".ko.yaml is dir",
koConfigPath: "testdata/bad-config",
err: "testdata/bad-config/.ko.yaml is not a regular file",
}, {
name: "good",
koConfigPath: "testdata/config",
}} {
t.Run(tc.name, func(t *testing.T) {
oldEnv := os.Getenv(envName)
defer os.Setenv(envName, oldEnv)
os.Setenv(envName, tc.koConfigPath)
err := bo.LoadConfig()
if err == nil {
if tc.err == "" {
return
}
t.Fatalf("expected error %q, saw nil", tc.err)
}
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("expected error to contain %q, saw: %v", tc.err, err)
}
})
}
}