forked from gogf/gf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgproc_z_unit_shell_windows_test.go
124 lines (98 loc) · 3.4 KB
/
gproc_z_unit_shell_windows_test.go
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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
//go:build windows
package gproc_test
import (
"fmt"
"path/filepath"
"testing"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_ShellExec_GoBuild_Windows(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
testPath := gtest.DataPath("gobuild")
filename := filepath.Join(testPath, "main.go")
output := filepath.Join(testPath, "main.exe")
cmd := fmt.Sprintf(`go build -ldflags="-s -w" -o %s %s`, output, filename)
err := gproc.ShellRun(gctx.New(), cmd)
t.Assert(err, nil)
exists := gfile.Exists(output)
t.Assert(exists, true)
defer gfile.Remove(output)
})
gtest.C(t, func(t *gtest.T) {
testPath := gtest.DataPath("gobuild")
filename := filepath.Join(testPath, "main.go")
output := filepath.Join(testPath, "main.exe")
cmd := fmt.Sprintf(`go build -ldflags="-X 'main.TestString=\"test string\"'" -o %s %s`, output, filename)
err := gproc.ShellRun(gctx.New(), cmd)
t.Assert(err, nil)
exists := gfile.Exists(output)
t.Assert(exists, true)
defer gfile.Remove(output)
result, err := gproc.ShellExec(gctx.New(), output)
t.Assert(err, nil)
t.Assert(result, `"test string"`)
})
}
func Test_ShellExec_SpaceDir_Windows(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
testPath := gtest.DataPath("shellexec")
filename := filepath.Join(testPath, "main.go")
// go build -o test.exe main.go
cmd := fmt.Sprintf(`go build -o test.exe %s`, filename)
r, err := gproc.ShellExec(gctx.New(), cmd)
t.AssertNil(err)
t.Assert(r, "")
exists := gfile.Exists(filename)
t.Assert(exists, true)
outputDir := filepath.Join(testPath, "testdir")
output := filepath.Join(outputDir, "test.exe")
err = gfile.Move("test.exe", output)
t.AssertNil(err)
defer gfile.Remove(output)
expectContent := "123"
testOutput := filepath.Join(testPath, "space dir", "test.txt")
cmd = fmt.Sprintf(`%s -c %s -o "%s"`, output, expectContent, testOutput)
r, err = gproc.ShellExec(gctx.New(), cmd)
t.AssertNil(err)
exists = gfile.Exists(testOutput)
t.Assert(exists, true)
defer gfile.Remove(testOutput)
contents := gfile.GetContents(testOutput)
t.Assert(contents, expectContent)
})
gtest.C(t, func(t *gtest.T) {
testPath := gtest.DataPath("shellexec")
filename := filepath.Join(testPath, "main.go")
// go build -o test.exe main.go
cmd := fmt.Sprintf(`go build -o test.exe %s`, filename)
r, err := gproc.ShellExec(gctx.New(), cmd)
t.AssertNil(err)
t.Assert(r, "")
exists := gfile.Exists(filename)
t.Assert(exists, true)
outputDir := filepath.Join(testPath, "space dir")
output := filepath.Join(outputDir, "test.exe")
err = gfile.Move("test.exe", output)
t.AssertNil(err)
defer gfile.Remove(output)
expectContent := "123"
testOutput := filepath.Join(testPath, "testdir", "test.txt")
cmd = fmt.Sprintf(`"%s" -c %s -o %s`, output, expectContent, testOutput)
r, err = gproc.ShellExec(gctx.New(), cmd)
t.AssertNil(err)
exists = gfile.Exists(testOutput)
t.Assert(exists, true)
defer gfile.Remove(testOutput)
contents := gfile.GetContents(testOutput)
t.Assert(contents, expectContent)
})
}