-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathexecutable_test.go
More file actions
127 lines (109 loc) · 4.03 KB
/
executable_test.go
File metadata and controls
127 lines (109 loc) · 4.03 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
package util
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/creativeprojects/resticprofile/platform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExecutableIsAbsolute(t *testing.T) {
executable, err := Executable()
require.NoError(t, err)
assert.NotEmpty(t, executable)
assert.True(t, filepath.IsAbs(executable))
}
func TestExecutable(t *testing.T) {
if platform.IsWindows() {
t.Skip("Executable test is not applicable on Windows")
}
tempDir, err := os.MkdirTemp("", "resticprofile-executable")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
t.Cleanup(func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Errorf("failed to remove temp dir: %v", err)
}
})
helperBinary := filepath.Join(tempDir, "executable_test_helper")
assert.True(t, filepath.IsAbs(helperBinary), "Helper binary path should be absolute")
cmd := exec.Command("go", "build", "-buildvcs=false", "-o", helperBinary, "./test_executable")
if err := cmd.Run(); err != nil {
t.Fatalf("Error building helper binary: %s\n", err)
}
symlinkBinary := filepath.Join(tempDir, "executable_test_symlink")
err = os.Symlink(helperBinary, symlinkBinary)
require.NoError(t, err, "Failed to create symlink for helper binary")
t.Run("absolute", func(t *testing.T) {
cmd = exec.Command(helperBinary)
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+helperBinary+"\"\n", "Output should match the helper binary path")
})
t.Run("absolute symlink", func(t *testing.T) {
cmd = exec.Command(symlinkBinary)
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+symlinkBinary+"\"\n", "Output should match the helper binary path")
})
t.Run("relative", func(t *testing.T) {
cmd = exec.Command("./" + filepath.Base(helperBinary))
cmd.Dir = tempDir // Set the working directory to the temp directory
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+helperBinary+"\"\n", "Output should match the helper binary path")
})
t.Run("relative symlink", func(t *testing.T) {
cmd = exec.Command("./" + filepath.Base(symlinkBinary))
cmd.Dir = tempDir // Set the working directory to the temp directory
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+symlinkBinary+"\"\n", "Output should match the helper binary path")
})
t.Run("from PATH", func(t *testing.T) {
path := os.Getenv("PATH")
t.Cleanup(func() {
os.Setenv("PATH", path) // Restore original PATH after test
})
os.Setenv("PATH", tempDir+string(os.PathListSeparator)+path) // Add tempDir to PATH for this test
t.Logf("Using PATH: %s", os.Getenv("PATH"))
cmd = exec.Command(filepath.Base(helperBinary))
cmd.Dir = tempDir // Set the working directory to the temp directory
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+helperBinary+"\"\n", "Output should match the helper binary path")
})
t.Run("symlink from PATH", func(t *testing.T) {
path := os.Getenv("PATH")
t.Cleanup(func() {
os.Setenv("PATH", path) // Restore original PATH after test
})
os.Setenv("PATH", tempDir+string(os.PathListSeparator)+path) // Add tempDir to PATH for this test
t.Logf("Using PATH: %s", os.Getenv("PATH"))
cmd = exec.Command(filepath.Base(symlinkBinary))
cmd.Dir = tempDir // Set the working directory to the temp directory
output, err := cmd.Output()
if err != nil {
t.Fatalf("Error executing helper binary: %s\n", err)
}
t.Log(string(output))
assert.Equal(t, string(output), "\""+symlinkBinary+"\"\n", "Output should match the helper binary path")
})
}