11package util
22
33import (
4+ "os"
5+ "os/exec"
46 "path/filepath"
57 "testing"
68
9+ "github.com/creativeprojects/resticprofile/platform"
710 "github.com/stretchr/testify/assert"
811 "github.com/stretchr/testify/require"
912)
@@ -15,3 +18,110 @@ func TestExecutableIsAbsolute(t *testing.T) {
1518
1619 assert .True (t , filepath .IsAbs (executable ))
1720}
21+
22+ func TestExecutable (t * testing.T ) {
23+ if platform .IsWindows () {
24+ t .Skip ("Executable test is not applicable on Windows" )
25+ }
26+
27+ tempDir , err := os .MkdirTemp ("" , "resticprofile-executable" )
28+ if err != nil {
29+ t .Fatalf ("failed to create temp dir: %v" , err )
30+ }
31+
32+ t .Cleanup (func () {
33+ if err := os .RemoveAll (tempDir ); err != nil {
34+ t .Errorf ("failed to remove temp dir: %v" , err )
35+ }
36+ })
37+
38+ helperBinary := filepath .Join (tempDir , "executable_test_helper" )
39+ assert .True (t , filepath .IsAbs (helperBinary ), "Helper binary path should be absolute" )
40+
41+ cmd := exec .Command ("go" , "build" , "-buildvcs=false" , "-o" , helperBinary , "./test_executable" )
42+ if err := cmd .Run (); err != nil {
43+ t .Fatalf ("Error building helper binary: %s\n " , err )
44+ }
45+
46+ symlinkBinary := filepath .Join (tempDir , "executable_test_symlink" )
47+ err = os .Symlink (helperBinary , symlinkBinary )
48+ require .NoError (t , err , "Failed to create symlink for helper binary" )
49+
50+ t .Run ("absolute" , func (t * testing.T ) {
51+ cmd = exec .Command (helperBinary )
52+ output , err := cmd .Output ()
53+ if err != nil {
54+ t .Fatalf ("Error executing helper binary: %s\n " , err )
55+ }
56+ t .Log (string (output ))
57+ assert .Equal (t , string (output ), "\" " + helperBinary + "\" \n " , "Output should match the helper binary path" )
58+ })
59+
60+ t .Run ("absolute symlink" , func (t * testing.T ) {
61+ cmd = exec .Command (symlinkBinary )
62+ output , err := cmd .Output ()
63+ if err != nil {
64+ t .Fatalf ("Error executing helper binary: %s\n " , err )
65+ }
66+ t .Log (string (output ))
67+ assert .Equal (t , string (output ), "\" " + symlinkBinary + "\" \n " , "Output should match the helper binary path" )
68+ })
69+
70+ t .Run ("relative" , func (t * testing.T ) {
71+ cmd = exec .Command ("./" + filepath .Base (helperBinary ))
72+ cmd .Dir = tempDir // Set the working directory to the temp directory
73+ output , err := cmd .Output ()
74+ if err != nil {
75+ t .Fatalf ("Error executing helper binary: %s\n " , err )
76+ }
77+ t .Log (string (output ))
78+ assert .Equal (t , string (output ), "\" " + helperBinary + "\" \n " , "Output should match the helper binary path" )
79+ })
80+
81+ t .Run ("relative symlink" , func (t * testing.T ) {
82+ cmd = exec .Command ("./" + filepath .Base (symlinkBinary ))
83+ cmd .Dir = tempDir // Set the working directory to the temp directory
84+ output , err := cmd .Output ()
85+ if err != nil {
86+ t .Fatalf ("Error executing helper binary: %s\n " , err )
87+ }
88+ t .Log (string (output ))
89+ assert .Equal (t , string (output ), "\" " + symlinkBinary + "\" \n " , "Output should match the helper binary path" )
90+ })
91+
92+ t .Run ("from PATH" , func (t * testing.T ) {
93+ path := os .Getenv ("PATH" )
94+ t .Cleanup (func () {
95+ os .Setenv ("PATH" , path ) // Restore original PATH after test
96+ })
97+ os .Setenv ("PATH" , tempDir + string (os .PathListSeparator )+ path ) // Add tempDir to PATH for this test
98+ t .Logf ("Using PATH: %s" , os .Getenv ("PATH" ))
99+
100+ cmd = exec .Command (filepath .Base (helperBinary ))
101+ cmd .Dir = tempDir // Set the working directory to the temp directory
102+ output , err := cmd .Output ()
103+ if err != nil {
104+ t .Fatalf ("Error executing helper binary: %s\n " , err )
105+ }
106+ t .Log (string (output ))
107+ assert .Equal (t , string (output ), "\" " + helperBinary + "\" \n " , "Output should match the helper binary path" )
108+ })
109+
110+ t .Run ("symlink from PATH" , func (t * testing.T ) {
111+ path := os .Getenv ("PATH" )
112+ t .Cleanup (func () {
113+ os .Setenv ("PATH" , path ) // Restore original PATH after test
114+ })
115+ os .Setenv ("PATH" , tempDir + string (os .PathListSeparator )+ path ) // Add tempDir to PATH for this test
116+ t .Logf ("Using PATH: %s" , os .Getenv ("PATH" ))
117+
118+ cmd = exec .Command (filepath .Base (symlinkBinary ))
119+ cmd .Dir = tempDir // Set the working directory to the temp directory
120+ output , err := cmd .Output ()
121+ if err != nil {
122+ t .Fatalf ("Error executing helper binary: %s\n " , err )
123+ }
124+ t .Log (string (output ))
125+ assert .Equal (t , string (output ), "\" " + symlinkBinary + "\" \n " , "Output should match the helper binary path" )
126+ })
127+ }
0 commit comments