Skip to content

Commit de7375b

Browse files
authored
Add unit tests for config path and repositories (#3190)
* Add unit tests for config path and repositories * goimports issue
1 parent b039190 commit de7375b

File tree

1 file changed

+73
-14
lines changed

1 file changed

+73
-14
lines changed

pkg/functions/config_test.go

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,88 @@ package functions_test
55
// files suffixed '_unit_test.go'.
66

77
import (
8+
"os"
9+
"path/filepath"
810
"testing"
11+
12+
"knative.dev/func/pkg/config"
913
)
1014

1115
// TestConfig_PathDefault ensures that config defaults to XDG_CONFIG_HOME/func
16+
// and that a config.yaml placed there is picked up by NewDefault.
1217
func TestConfig_PathDefault(t *testing.T) {
13-
// TODO
14-
// Set XDG_CONFIG_PATH to ./testdata/config
15-
// Confirm the config is populated from the test files.
18+
tmp := t.TempDir()
19+
20+
t.Setenv("XDG_CONFIG_HOME", tmp)
21+
22+
cfgDir := filepath.Join(tmp, "func")
23+
if err := os.MkdirAll(cfgDir, os.ModePerm); err != nil {
24+
t.Fatalf("failed to create config dir: %v", err)
25+
}
26+
27+
cfgFile := filepath.Join(cfgDir, config.Filename)
28+
content := "builder: test-builder\nregistry: quay.io/test\n"
29+
if err := os.WriteFile(cfgFile, []byte(content), 0o644); err != nil {
30+
t.Fatalf("failed to write config file: %v", err)
31+
}
32+
33+
cfg, err := config.NewDefault()
34+
if err != nil {
35+
t.Fatalf("NewDefault returned error: %v", err)
36+
}
37+
if cfg.Builder != "test-builder" {
38+
t.Fatalf("expected Builder 'test-builder', got %q", cfg.Builder)
39+
}
40+
if cfg.Registry != "quay.io/test" {
41+
t.Fatalf("expected Registry 'quay.io/test', got %q", cfg.Registry)
42+
}
1643
}
1744

18-
// TestConfigPath ensure that the config path provided via the WithConfig
19-
// option is respected.
45+
// TestConfig_Path ensures that the config file specified via FUNC_CONFIG_FILE
46+
// is respected by NewDefault / File().
2047
func TestConfig_Path(t *testing.T) {
21-
// TODO
22-
// Create a client specifying ./testdata/config
23-
// Confirm the config is populated from the test files.
48+
tmp := t.TempDir()
49+
cfgFile := filepath.Join(tmp, "myconfig.yaml")
50+
content := "builder: explicit-builder\n"
51+
if err := os.WriteFile(cfgFile, []byte(content), 0o644); err != nil {
52+
t.Fatalf("failed to write config file: %v", err)
53+
}
54+
55+
t.Setenv("FUNC_CONFIG_FILE", cfgFile)
56+
57+
if got := config.File(); got != cfgFile {
58+
t.Fatalf("expected config.File() to be %q, got %q", cfgFile, got)
59+
}
60+
61+
cfg, err := config.NewDefault()
62+
if err != nil {
63+
t.Fatalf("NewDefault returned error: %v", err)
64+
}
65+
if cfg.Builder != "explicit-builder" {
66+
t.Fatalf("expected Builder 'explicit-builder', got %q", cfg.Builder)
67+
}
2468
}
2569

26-
// TestConfigRepositoriesPath ensures that the repositories directory within
27-
// the effective config path is created if it does not already exist.
70+
// TestConfig_RepositoriesPath ensures that CreatePaths creates the
71+
// repositories directory under the effective config path.
2872
func TestConfig_RepositoriesPath(t *testing.T) {
29-
// TODO
30-
// Create a temporary directory
31-
// Specify this directory as the config path when instantiating a client.
32-
// Confirm that the repositories directory is created.
73+
tmp := t.TempDir()
74+
t.Setenv("XDG_CONFIG_HOME", tmp)
75+
76+
reposPath := filepath.Join(tmp, "func", config.Repositories)
77+
if _, err := os.Stat(reposPath); !os.IsNotExist(err) {
78+
_ = os.RemoveAll(reposPath)
79+
}
80+
81+
if err := config.CreatePaths(); err != nil {
82+
t.Fatalf("CreatePaths returned error: %v", err)
83+
}
84+
85+
fi, err := os.Stat(reposPath)
86+
if err != nil {
87+
t.Fatalf("expected repositories path to exist, stat error: %v", err)
88+
}
89+
if !fi.IsDir() {
90+
t.Fatalf("expected repositories path to be a directory: %s", reposPath)
91+
}
3392
}

0 commit comments

Comments
 (0)