/
setup_check_test.go
131 lines (115 loc) · 3.71 KB
/
setup_check_test.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2020 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"os"
"strings"
"testing"
"sigs.k8s.io/krew/internal/environment"
"sigs.k8s.io/krew/internal/testutil"
)
const environmentPath = "/home/../home/user//./.krew"
func TestIsBinDirInPATH_firstRun(t *testing.T) {
tempDir := testutil.NewTempDir(t)
paths := environment.NewPaths(tempDir.Path("does-not-exist"))
res := IsBinDirInPATH(paths)
if res == false {
t.Errorf("expected positive result on first run")
}
}
func TestIsBinDirInPATH_secondRun(t *testing.T) {
tempDir := testutil.NewTempDir(t)
paths := environment.NewPaths(tempDir.Root())
res := IsBinDirInPATH(paths)
if res == true {
t.Errorf("expected negative result on second run")
}
}
// TestIsBinDirInPATH_NonNormalized case when PATH content is not well normalized
func TestIsBinDirInPATH_NonNormalized(t *testing.T) {
tempDir := testutil.NewTempDir(t)
// backup and restore initial PATH value
defer func(backupPath string) {
err := os.Setenv("PATH", backupPath)
if err != nil {
t.Fatalf(`os.Setenv("PATH", %s) failed with %v`, backupPath, err)
}
}(os.Getenv("PATH"))
// set PATH with non-normalized folder path
err := os.Setenv("PATH", tempDir.Path(environmentPath+"/bin"))
if err != nil {
t.Fatalf(`os.Setenv("PATH", %s/bin) failed with %v`, environmentPath, err)
}
paths := environment.NewPaths(tempDir.Path(environmentPath))
err = os.MkdirAll(paths.BasePath(), os.ModePerm)
if err != nil {
t.Fatalf("os.MkdirAll(%s) failed with %v", paths.BasePath(), err)
}
got := IsBinDirInPATH(paths)
if got == false {
t.Errorf("IsBinDirPATH(%v) = %t, want true", paths, got)
}
}
func TestSetupInstructions_windows(t *testing.T) {
const instructionsContain = `USERPROFILE`
os.Setenv("KREW_OS", "windows")
defer os.Unsetenv("KREW_OS")
instructions := SetupInstructions()
if !strings.Contains(instructions, instructionsContain) {
t.Errorf("expected %q\nto contain %q", instructions, instructionsContain)
}
}
func TestSetupInstructions_unix(t *testing.T) {
tests := []struct {
name string
shell string
instructionsContain string
}{
{
name: "When the shell is zsh",
shell: "/bin/zsh",
instructionsContain: "~/.zshrc",
},
{
name: "When the shell is bash",
shell: "/bin/bash",
instructionsContain: "~/.bash_profile or ~/.bashrc",
},
{
name: "When the shell is fish",
shell: "/bin/fish",
instructionsContain: "config.fish",
},
{
name: "When the shell is unknown",
shell: "other",
instructionsContain: "~/.bash_profile, ~/.bashrc, or ~/.zshrc",
},
}
// always set KREW_OS, so that tests succeed on windows
os.Setenv("KREW_OS", "linux")
defer func(origShell string) {
os.Unsetenv("KREW_OS")
os.Setenv("SHELL", origShell)
}(os.Getenv("SHELL"))
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
os.Setenv("SHELL", test.shell)
instructions := SetupInstructions()
if !strings.Contains(instructions, test.instructionsContain) {
tt.Errorf("expected %q\nto contain %q", instructions, test.instructionsContain)
}
})
}
}