File: factory_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (123 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download | duplicates (2)
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
package cmdutil

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func Test_executable(t *testing.T) {
	testExe, err := os.Executable()
	if err != nil {
		t.Fatal(err)
	}

	testExeName := filepath.Base(testExe)

	// Create 3 extra PATH entries that each contain an executable with the same name as the running test
	// process. The first is a symlink, but to an unrelated executable, the second is a symlink to our test
	// process and thus represents the result we want, and the third one is an unrelated executable.
	dir := t.TempDir()
	bin1 := filepath.Join(dir, "bin1")
	bin1Exe := filepath.Join(bin1, testExeName)
	bin2 := filepath.Join(dir, "bin2")
	bin2Exe := filepath.Join(bin2, testExeName)
	bin3 := filepath.Join(dir, "bin3")
	bin3Exe := filepath.Join(bin3, testExeName)

	if err := os.MkdirAll(bin1, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(bin2, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(bin3, 0755); err != nil {
		t.Fatal(err)
	}
	if f, err := os.OpenFile(bin3Exe, os.O_CREATE, 0755); err == nil {
		f.Close()
	} else {
		t.Fatal(err)
	}
	if err := os.Symlink(testExe, bin2Exe); err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(bin3Exe, bin1Exe); err != nil {
		t.Fatal(err)
	}

	oldPath := os.Getenv("PATH")
	t.Setenv("PATH", strings.Join([]string{bin1, bin2, bin3, oldPath}, string(os.PathListSeparator)))

	if got := executable(""); got != bin2Exe {
		t.Errorf("executable() = %q, want %q", got, bin2Exe)
	}
}

func Test_executable_relative(t *testing.T) {
	testExe, err := os.Executable()
	if err != nil {
		t.Fatal(err)
	}

	testExeName := filepath.Base(testExe)

	// Create 3 extra PATH entries that each contain an executable with the same name as the running test
	// process. The first is a relative symlink, but to an unrelated executable, the second is a relative
	// symlink to our test process and thus represents the result we want, and the third one is an unrelated
	// executable.
	dir := t.TempDir()
	bin1 := filepath.Join(dir, "bin1")
	bin1Exe := filepath.Join(bin1, testExeName)
	bin2 := filepath.Join(dir, "bin2")
	bin2Exe := filepath.Join(bin2, testExeName)
	bin3 := filepath.Join(dir, "bin3")
	bin3Exe := filepath.Join(bin3, testExeName)

	if err := os.MkdirAll(bin1, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(bin2, 0755); err != nil {
		t.Fatal(err)
	}
	if err := os.MkdirAll(bin3, 0755); err != nil {
		t.Fatal(err)
	}

	if f, err := os.OpenFile(bin3Exe, os.O_CREATE, 0755); err == nil {
		f.Close()
	} else {
		t.Fatal(err)
	}
	bin2Rel, err := filepath.Rel(bin2, testExe)
	if err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(bin2Rel, bin2Exe); err != nil {
		t.Fatal(err)
	}
	bin1Rel, err := filepath.Rel(bin1, bin3Exe)
	if err != nil {
		t.Fatal(err)
	}
	if err := os.Symlink(bin1Rel, bin1Exe); err != nil {
		t.Fatal(err)
	}

	oldPath := os.Getenv("PATH")
	t.Setenv("PATH", strings.Join([]string{bin1, bin2, bin3, oldPath}, string(os.PathListSeparator)))

	if got := executable(""); got != bin2Exe {
		t.Errorf("executable() = %q, want %q", got, bin2Exe)
	}
}

func Test_Executable_override(t *testing.T) {
	override := strings.Join([]string{"C:", "cygwin64", "home", "gh.exe"}, string(os.PathSeparator))
	t.Setenv("GH_PATH", override)
	f := Factory{}
	if got := f.Executable(); got != override {
		t.Errorf("executable() = %q, want %q", got, override)
	}
}