-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_test.go
More file actions
129 lines (119 loc) · 2.97 KB
/
env_test.go
File metadata and controls
129 lines (119 loc) · 2.97 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
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
package tinygo
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGetRoot_SystemInstall(t *testing.T) {
opts := []Option{
withLookPath(func(name string) (string, error) {
return "/usr/bin/tinygo", nil
}),
}
root := GetRoot(opts...)
if root != "" {
t.Errorf("expected empty root for system install, got %q", root)
}
}
func TestGetRoot_LocalInstall(t *testing.T) {
tmpDir := t.TempDir()
opts := []Option{
withLookPath(func(name string) (string, error) {
return "", fmt.Errorf("not found")
}),
WithInstallDir(tmpDir),
}
root := GetRoot(opts...)
expected := filepath.Join(tmpDir, "tinygo")
if root != expected {
t.Errorf("expected %q, got %q", expected, root)
}
}
func TestGetEnv_SystemInstall(t *testing.T) {
opts := []Option{
withLookPath(func(name string) (string, error) {
return "/usr/bin/tinygo", nil
}),
}
env := GetEnv(opts...)
expected := os.Environ()
if len(env) != len(expected) {
t.Errorf("expected %d env vars, got %d", len(expected), len(env))
}
for i := range env {
if env[i] != expected[i] {
t.Errorf("mismatch at index %d: expected %q, got %q", i, expected[i], env[i])
}
}
}
func TestGetEnv_LocalInstall(t *testing.T) {
tmpDir := t.TempDir()
opts := []Option{
withLookPath(func(name string) (string, error) {
return "", fmt.Errorf("not found")
}),
WithInstallDir(tmpDir),
}
env := GetEnv(opts...)
root := filepath.Join(tmpDir, "tinygo")
foundTinyRoot := false
foundPath := false
for _, e := range env {
key, _, found := strings.Cut(e, "=")
if !found {
continue
}
upperKey := strings.ToUpper(key)
if upperKey == "TINYGOROOT" {
foundTinyRoot = true
if e != "TINYGOROOT="+root {
t.Errorf("expected TINYGOROOT=%s, got %s", root, e)
}
}
if upperKey == "PATH" {
foundPath = true
binDir := filepath.Join(root, "bin")
if !strings.Contains(e, binDir) {
t.Errorf("PATH does not contain bin dir %q: %q", binDir, e)
}
if !strings.HasPrefix(e, key+"="+binDir+string(os.PathListSeparator)) && e != key+"="+binDir {
t.Errorf("PATH should start with %q: %q", binDir, e)
}
}
}
if !foundTinyRoot {
t.Error("TINYGOROOT not found in env")
}
if !foundPath {
t.Error("PATH not found in env")
}
}
func TestGetEnv_OverridesExistingTINYGOROOT(t *testing.T) {
// Set a dummy TINYGOROOT in the current process to ensure it's overridden
os.Setenv("TINYGOROOT", "/stale/path")
defer os.Unsetenv("TINYGOROOT")
tmpDir := t.TempDir()
opts := []Option{
withLookPath(func(name string) (string, error) {
return "", fmt.Errorf("not found")
}),
WithInstallDir(tmpDir),
}
env := GetEnv(opts...)
root := filepath.Join(tmpDir, "tinygo")
foundCount := 0
for _, e := range env {
key, _, found := strings.Cut(e, "=")
if found && strings.ToUpper(key) == "TINYGOROOT" {
foundCount++
if e != key+"="+root {
t.Errorf("expected TINYGOROOT=%s, got %s", root, e)
}
}
}
if foundCount != 1 {
t.Errorf("expected exactly 1 TINYGOROOT entry, got %d", foundCount)
}
}