github.com/gogf/gf/v2@v2.7.4/os/gproc/gproc_z_unit_process_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  	"path/filepath"
    15  	"strings"
    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_ProcessRun(t *testing.T) {
    25  	gtest.C(t, func(t *gtest.T) {
    26  		binary := gproc.SearchBinary("go")
    27  		t.AssertNE(binary, "")
    28  		var command = gproc.NewProcess(binary, nil)
    29  
    30  		testPath := gtest.DataPath("gobuild")
    31  		filename := filepath.Join(testPath, "main.go")
    32  		output := filepath.Join(testPath, "main.exe")
    33  
    34  		command.Args = append(command.Args, "build")
    35  		command.Args = append(command.Args, `-ldflags="-X 'main.TestString=\"test string\"'"`)
    36  		command.Args = append(command.Args, "-o", output)
    37  		command.Args = append(command.Args, filename)
    38  
    39  		err := command.Run(gctx.GetInitCtx())
    40  		t.AssertNil(err)
    41  
    42  		exists := gfile.Exists(output)
    43  		t.Assert(exists, true)
    44  		defer gfile.Remove(output)
    45  
    46  		runCmd := gproc.NewProcess(output, nil)
    47  		var buf strings.Builder
    48  		runCmd.Stdout = &buf
    49  		runCmd.Stderr = &buf
    50  		err = runCmd.Run(gctx.GetInitCtx())
    51  		t.Assert(err, nil)
    52  		t.Assert(buf.String(), `"test string"`)
    53  	})
    54  
    55  	gtest.C(t, func(t *gtest.T) {
    56  		binary := gproc.SearchBinary("go")
    57  		t.AssertNE(binary, "")
    58  		// NewProcess(path,args) path: It's best not to have spaces
    59  		var command = gproc.NewProcess(binary, nil)
    60  
    61  		testPath := gtest.DataPath("gobuild")
    62  		filename := filepath.Join(testPath, "main.go")
    63  		output := filepath.Join(testPath, "main.exe")
    64  
    65  		command.Args = append(command.Args, "build")
    66  		command.Args = append(command.Args, `-ldflags="-s -w"`)
    67  		command.Args = append(command.Args, "-o", output)
    68  		command.Args = append(command.Args, filename)
    69  
    70  		err := command.Run(gctx.GetInitCtx())
    71  		t.AssertNil(err)
    72  
    73  		exists := gfile.Exists(output)
    74  		t.Assert(exists, true)
    75  
    76  		defer gfile.Remove(output)
    77  	})
    78  }