-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathenv_test.go
More file actions
145 lines (123 loc) · 3.95 KB
/
env_test.go
File metadata and controls
145 lines (123 loc) · 3.95 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmd
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"text/tabwriter"
)
func TestEnvCmd_ContainsExpectedFields(t *testing.T) {
var buf bytes.Buffer
writer = tabwriter.NewWriter(&buf, 1, 1, 1, ' ', 0)
buildVersion = "1.2.3"
buildGitCommitHash = "a1b2c3d"
buildTime = "2026-01-01T00:00:00Z"
envCmd.Run(envCmd, []string{})
output := buf.String()
expectedFields := []string{
"Binary:",
"Config:",
"Plugins Dir:",
"Plugins:",
"Version:",
"Commit:",
"Build Time:",
"Go Version:",
"OS/Arch:",
}
for _, field := range expectedFields {
if !strings.Contains(output, field) {
t.Errorf("expected output to contain %q, got:\n%s", field, output)
}
}
}
func TestEnvCmd_DisplaysBuildInfo(t *testing.T) {
var buf bytes.Buffer
writer = tabwriter.NewWriter(&buf, 1, 1, 1, ' ', 0)
buildVersion = "test-version"
buildGitCommitHash = "e4f5a6b"
buildTime = "2026-02-15T00:00:00Z"
envCmd.Run(envCmd, []string{})
output := buf.String()
if !strings.Contains(output, "test-version") {
t.Errorf("expected output to contain version 'test-version', got:\n%s", output)
}
if !strings.Contains(output, "e4f5a6b") {
t.Errorf("expected output to contain commit 'e4f5a6b', got:\n%s", output)
}
if !strings.Contains(output, "2026-02-15T00:00:00Z") {
t.Errorf("expected output to contain build time, got:\n%s", output)
}
if !strings.Contains(output, runtime.Version()) {
t.Errorf("expected output to contain Go version %q, got:\n%s", runtime.Version(), output)
}
expectedArch := runtime.GOOS + "/" + runtime.GOARCH
if !strings.Contains(output, expectedArch) {
t.Errorf("expected output to contain OS/Arch %q, got:\n%s", expectedArch, output)
}
}
func TestEnvCmd_ShowsBinaryPath(t *testing.T) {
var buf bytes.Buffer
writer = tabwriter.NewWriter(&buf, 1, 1, 1, ' ', 0)
envCmd.Run(envCmd, []string{})
output := buf.String()
execPath, err := os.Executable()
if err == nil && !strings.Contains(output, execPath) {
t.Errorf("expected output to contain executable path %q, got:\n%s", execPath, output)
}
}
func TestDiscoverPluginNames_EmptyDir(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "pvtr-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
result := discoverPluginNames(tmpDir)
if result != "none" {
t.Errorf("expected 'none' for empty dir, got: %s", result)
}
}
func TestDiscoverPluginNames_NonexistentDir(t *testing.T) {
result := discoverPluginNames("/nonexistent/path")
if result != "none" {
t.Errorf("expected 'none' for nonexistent dir, got: %s", result)
}
}
func TestDiscoverPluginNames_FiltersPvtrAndPrivateer(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "pvtr-test-*")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer func() { _ = os.RemoveAll(tmpDir) }()
for _, name := range []string{"pvtr", "pvtr-foo", "privateer", "privateer-foo", "my-plugin", "other-tool"} {
path := filepath.Join(tmpDir, name)
if err := os.WriteFile(path, []byte("#!/bin/sh\n"), 0755); err != nil {
t.Fatalf("failed to create file %s: %v", name, err)
}
}
result := discoverPluginNames(tmpDir)
// Exact-name binaries should be filtered out
for _, filtered := range []string{"pvtr", "privateer"} {
// Check the result doesn't contain the exact name as a standalone entry
for _, entry := range strings.Split(result, ", ") {
if entry == filtered {
t.Errorf("expected %q to be filtered out, got: %s", filtered, result)
}
}
}
// Prefixed plugin names should still be discoverable
if !strings.Contains(result, "pvtr-foo") {
t.Errorf("expected 'pvtr-foo' in result, got: %s", result)
}
if !strings.Contains(result, "privateer-foo") {
t.Errorf("expected 'privateer-foo' in result, got: %s", result)
}
if !strings.Contains(result, "my-plugin") {
t.Errorf("expected 'my-plugin' in result, got: %s", result)
}
if !strings.Contains(result, "other-tool") {
t.Errorf("expected 'other-tool' in result, got: %s", result)
}
}