File: config_test.go

package info (click to toggle)
golang-step-crypto 0.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,056 kB
  • sloc: sh: 53; makefile: 28
file content (107 lines) | stat: -rw-r--r-- 2,003 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package step

import (
	"os"
	"os/user"
	"path/filepath"
	"testing"
)

func mustHome(t *testing.T) string {
	t.Helper()

	homePath = os.Getenv(HomeEnv)
	if homePath != "" {
		return homePath
	}
	usr, err := user.Current()
	if err == nil && usr.HomeDir != "" {
		return usr.HomeDir
	}
	t.Fatal("error obtaining home directory")
	return ""
}

func TestPath(t *testing.T) {
	tmp := stepPath
	home := mustHome(t)
	t.Cleanup(func() {
		stepPath = tmp
	})

	tests := []struct {
		name string
		want string
	}{
		{"default", filepath.Join(home, ".step")},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Path(); got != tt.want {
				t.Errorf("Path() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestHome(t *testing.T) {
	tests := []struct {
		name string
		want string
	}{
		{"default", mustHome(t)},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Home(); got != tt.want {
				t.Errorf("Home() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestAbs(t *testing.T) {
	home := mustHome(t)
	abs, err := filepath.Abs("./foo/bar/zar")
	if err != nil {
		t.Fatal(err)
	}

	type args struct {
		path string
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{"abs", args{"/foo/bar/zar"}, "/foo/bar/zar"},
		{"home", args{"~/foo/bar/zar"}, filepath.Join(home, "foo", "bar", "zar")},
		{"relative", args{"./foo/bar/zar"}, abs},
		{"step", args{"foo/bar/zar"}, filepath.Join(home, ".step", "foo", "bar", "zar")},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Abs(tt.args.path); got != tt.want {
				t.Errorf("Abs() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_getUserHomeDir(t *testing.T) {
	t.Skip()
	tests := []struct {
		name string
		want string
	}{
		{"ok", os.Getenv("HOME")},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := getUserHomeDir(); got != tt.want {
				t.Errorf("getUserHomeDir() = %v, want %v", got, tt.want)
			}
		})
	}
}