-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
82 lines (72 loc) · 2.26 KB
/
Copy pathconfig_test.go
File metadata and controls
82 lines (72 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2021 Jeffrey M Hodges.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"os"
"path/filepath"
"testing"
)
func TestLoadConfig(t *testing.T) {
t.Run("missing config file returns defaults", func(t *testing.T) {
dir := t.TempDir()
t.Setenv("GRAB_HOME", "")
cfg, err := loadConfigFrom(dir)
if err != nil {
t.Fatal(err)
}
if len(cfg.SSHPreferredHosts) != 0 {
t.Errorf("expected no ssh hosts, got %v", cfg.SSHPreferredHosts)
}
// Home should default to ~/src.
homeDir, _ := os.UserHomeDir()
want := filepath.Join(homeDir, "src")
if cfg.Home != want {
t.Errorf("Home = %q, want %q", cfg.Home, want)
}
})
t.Run("config file parsed", func(t *testing.T) {
dir := t.TempDir()
grabDir := filepath.Join(dir, "grab")
os.MkdirAll(grabDir, 0755)
os.WriteFile(filepath.Join(grabDir, "config.toml"), []byte("home = \"/tmp/src\"\nssh_preferred_hosts = [\"github.com\"]\n"), 0644)
t.Setenv("GRAB_HOME", "")
cfg, err := loadConfigFrom(dir)
if err != nil {
t.Fatal(err)
}
if cfg.Home != "/tmp/src" {
t.Errorf("Home = %q, want /tmp/src", cfg.Home)
}
if len(cfg.SSHPreferredHosts) != 1 || cfg.SSHPreferredHosts[0] != "github.com" {
t.Errorf("SSHHosts = %v, want [github.com]", cfg.SSHPreferredHosts)
}
})
t.Run("env var overrides home", func(t *testing.T) {
dir := t.TempDir()
grabDir := filepath.Join(dir, "grab")
os.MkdirAll(grabDir, 0755)
os.WriteFile(filepath.Join(grabDir, "config.toml"), []byte("home = \"/tmp/src\"\nssh_preferred_hosts = [\"github.com\"]\n"), 0644)
t.Setenv("GRAB_HOME", "/override/src")
cfg, err := loadConfigFrom(dir)
if err != nil {
t.Fatal(err)
}
if cfg.Home != "/override/src" {
t.Errorf("Home = %q, want /override/src", cfg.Home)
}
if len(cfg.SSHPreferredHosts) != 1 || cfg.SSHPreferredHosts[0] != "github.com" {
t.Errorf("SSHHosts = %v, want [github.com]", cfg.SSHPreferredHosts)
}
})
t.Run("malformed toml returns error", func(t *testing.T) {
dir := t.TempDir()
grabDir := filepath.Join(dir, "grab")
os.MkdirAll(grabDir, 0755)
os.WriteFile(filepath.Join(grabDir, "config.toml"), []byte(`[bad toml`), 0644)
t.Setenv("GRAB_HOME", "")
_, err := loadConfigFrom(dir)
if err == nil {
t.Fatal("expected error for malformed TOML, got nil")
}
})
}