Skip to content

Commit

Permalink
up: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 committed Apr 13, 2024
1 parent 18b6b82 commit 20f8705
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 65 deletions.
67 changes: 17 additions & 50 deletions os/gproc/gproc_z_unit_process_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,80 +19,48 @@ import (
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)

func Test_ProcessRun(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gf := gproc.SearchBinary("gf")
if gf == "" {
return
}
var command = gproc.NewProcess(gf, nil)
command.Args = append(command.Args, "version")
var buf strings.Builder
command.Stdout = &buf
command.Stderr = &buf
err := command.Run(gctx.GetInitCtx())
t.AssertNil(err)

errOutput := `up upgrade GoFrame version/tool to latest one in current project`
t.Assert(gstr.Contains(buf.String(), errOutput), false)
})

gtest.C(t, func(t *gtest.T) {
binary := gproc.SearchBinary("go")
t.AssertNE(binary, "")
var command = gproc.NewProcess(binary, nil)
command.Args = append(command.Args, "version")
var buf strings.Builder
command.Stdout = &buf
command.Stderr = &buf
err := command.Run(gctx.GetInitCtx())
t.AssertNil(err)

errOutput := `bug start a bug report`
t.Assert(gstr.Contains(buf.String(), errOutput), false)
})

gtest.C(t, func(t *gtest.T) {
binary := gproc.SearchBinary("go")
t.AssertNE(binary, "")
var command = gproc.NewProcess(binary, nil)

testpath := gtest.DataPath("gobuild")
filename := filepath.Join(testpath, "main.go")
output := filepath.Join(testpath, "main.exe")
testPath := gtest.DataPath("gobuild")
filename := filepath.Join(testPath, "main.go")
output := filepath.Join(testPath, "main.exe")

command.Args = append(command.Args, "build")
command.Args = append(command.Args, `-ldflags="-X 'main.TestString=\"test string\"'"`)
command.Args = append(command.Args, "-o", output)
command.Args = append(command.Args, filename)

var buf strings.Builder
command.Stdout = &buf
command.Stderr = &buf
err := command.Run(gctx.GetInitCtx())
t.AssertNil(err)

realPath, err := gfile.Search(output)
t.AssertNE(realPath, "")
t.Assert(err, nil)
exists := gfile.Exists(output)
t.Assert(exists, true)
defer gfile.Remove(output)

result, err := gproc.ShellExec(gctx.New(), output)
runCmd := gproc.NewProcess(output, nil)
var buf strings.Builder
runCmd.Stdout = &buf
runCmd.Stderr = &buf
err = runCmd.Run(gctx.GetInitCtx())
t.Assert(err, nil)
t.Assert(gstr.Contains(result, "test string"), true)
t.Assert(buf.String(), `"test string"`)
})

gtest.C(t, func(t *gtest.T) {
binary := gproc.SearchBinary("go")
t.AssertNE(binary, "")
// NewProcess(path,args) path: It's best not to have spaces
var command = gproc.NewProcess(binary, nil)

testpath := gtest.DataPath("gobuild")
filename := filepath.Join(testpath, "main.go")
output := filepath.Join(testpath, "main.exe")
testPath := gtest.DataPath("gobuild")
filename := filepath.Join(testPath, "main.go")
output := filepath.Join(testPath, "main.exe")

command.Args = append(command.Args, "build")
command.Args = append(command.Args, `-ldflags="-s -w"`)
Expand All @@ -102,9 +70,8 @@ func Test_ProcessRun(t *testing.T) {
err := command.Run(gctx.GetInitCtx())
t.AssertNil(err)

realPath, err := gfile.Search(output)
t.Assert(err, nil)
t.AssertNE(realPath, "")
exists := gfile.Exists(output)
t.Assert(exists, true)

defer gfile.Remove(output)
})
Expand Down
92 changes: 78 additions & 14 deletions os/gproc/gproc_z_unit_shell_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,106 @@ import (
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)

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")
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)

realPath, err := gfile.Search(output)
t.AssertNE(realPath, "")
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")
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)

realPath, err := gfile.Search(output)
t.AssertNE(realPath, "")
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(gstr.Contains(result, "test string"), true)
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)

})
}
8 changes: 7 additions & 1 deletion os/gproc/testdata/gobuild/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// 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.

package main

var (
TestString string
)

func main() {
println(TestString)
print(TestString)
}
32 changes: 32 additions & 0 deletions os/gproc/testdata/shellexec/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

package main

import (
"flag"
"fmt"
"os"
)

func main() {
var content string
var output string
flag.StringVar(&content, "c", "", "写入内容")
flag.StringVar(&output, "o", "", "写入路径")
flag.Parse()
fmt.Println(os.Args)
fmt.Println(content)
fmt.Println(output)
if output != "" {
file, err := os.Create(output)
if err != nil {
panic("create file fail: " + err.Error())
}
defer file.Close()
file.WriteString(content)
}
}

0 comments on commit 20f8705

Please sign in to comment.