github.com/gogf/gf/v2@v2.7.4/os/gproc/gproc_z_unit_shell_windows_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 //go:build windows 10 11 package gproc_test 12 13 import ( 14 "fmt" 15 "path/filepath" 16 "testing" 17 18 "github.com/gogf/gf/v2/os/gctx" 19 "github.com/gogf/gf/v2/os/gfile" 20 "github.com/gogf/gf/v2/os/gproc" 21 "github.com/gogf/gf/v2/test/gtest" 22 ) 23 24 func Test_ShellExec_GoBuild_Windows(t *testing.T) { 25 gtest.C(t, func(t *gtest.T) { 26 testPath := gtest.DataPath("gobuild") 27 filename := filepath.Join(testPath, "main.go") 28 output := filepath.Join(testPath, "main.exe") 29 cmd := fmt.Sprintf(`go build -ldflags="-s -w" -o %s %s`, output, filename) 30 31 err := gproc.ShellRun(gctx.New(), cmd) 32 t.Assert(err, nil) 33 34 exists := gfile.Exists(output) 35 t.Assert(exists, true) 36 37 defer gfile.Remove(output) 38 }) 39 40 gtest.C(t, func(t *gtest.T) { 41 testPath := gtest.DataPath("gobuild") 42 filename := filepath.Join(testPath, "main.go") 43 output := filepath.Join(testPath, "main.exe") 44 cmd := fmt.Sprintf(`go build -ldflags="-X 'main.TestString=\"test string\"'" -o %s %s`, output, filename) 45 46 err := gproc.ShellRun(gctx.New(), cmd) 47 t.Assert(err, nil) 48 49 exists := gfile.Exists(output) 50 t.Assert(exists, true) 51 defer gfile.Remove(output) 52 53 result, err := gproc.ShellExec(gctx.New(), output) 54 t.Assert(err, nil) 55 t.Assert(result, `"test string"`) 56 }) 57 58 } 59 60 func Test_ShellExec_SpaceDir_Windows(t *testing.T) { 61 gtest.C(t, func(t *gtest.T) { 62 testPath := gtest.DataPath("shellexec") 63 filename := filepath.Join(testPath, "main.go") 64 // go build -o test.exe main.go 65 cmd := fmt.Sprintf(`go build -o test.exe %s`, filename) 66 r, err := gproc.ShellExec(gctx.New(), cmd) 67 t.AssertNil(err) 68 t.Assert(r, "") 69 70 exists := gfile.Exists(filename) 71 t.Assert(exists, true) 72 73 outputDir := filepath.Join(testPath, "testdir") 74 output := filepath.Join(outputDir, "test.exe") 75 err = gfile.Move("test.exe", output) 76 t.AssertNil(err) 77 defer gfile.Remove(output) 78 79 expectContent := "123" 80 testOutput := filepath.Join(testPath, "space dir", "test.txt") 81 cmd = fmt.Sprintf(`%s -c %s -o "%s"`, output, expectContent, testOutput) 82 r, err = gproc.ShellExec(gctx.New(), cmd) 83 t.AssertNil(err) 84 85 exists = gfile.Exists(testOutput) 86 t.Assert(exists, true) 87 defer gfile.Remove(testOutput) 88 89 contents := gfile.GetContents(testOutput) 90 t.Assert(contents, expectContent) 91 }) 92 gtest.C(t, func(t *gtest.T) { 93 testPath := gtest.DataPath("shellexec") 94 filename := filepath.Join(testPath, "main.go") 95 // go build -o test.exe main.go 96 cmd := fmt.Sprintf(`go build -o test.exe %s`, filename) 97 r, err := gproc.ShellExec(gctx.New(), cmd) 98 t.AssertNil(err) 99 t.Assert(r, "") 100 101 exists := gfile.Exists(filename) 102 t.Assert(exists, true) 103 104 outputDir := filepath.Join(testPath, "space dir") 105 output := filepath.Join(outputDir, "test.exe") 106 err = gfile.Move("test.exe", output) 107 t.AssertNil(err) 108 defer gfile.Remove(output) 109 110 expectContent := "123" 111 testOutput := filepath.Join(testPath, "testdir", "test.txt") 112 cmd = fmt.Sprintf(`"%s" -c %s -o %s`, output, expectContent, testOutput) 113 r, err = gproc.ShellExec(gctx.New(), cmd) 114 t.AssertNil(err) 115 116 exists = gfile.Exists(testOutput) 117 t.Assert(exists, true) 118 defer gfile.Remove(testOutput) 119 120 contents := gfile.GetContents(testOutput) 121 t.Assert(contents, expectContent) 122 123 }) 124 }