github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/utils/test_files_compiler.go (about)

     1  // Copyright (c) 2015-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func CompileGo(t *testing.T, sourceCode, outputPath string) {
    17  	dir, err := ioutil.TempDir(".", "")
    18  	require.NoError(t, err)
    19  	defer os.RemoveAll(dir)
    20  	require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "main.go"), []byte(sourceCode), 0600))
    21  	cmd := exec.Command("go", "build", "-o", outputPath, "main.go")
    22  	cmd.Dir = dir
    23  	cmd.Stdout = os.Stdout
    24  	cmd.Stderr = os.Stderr
    25  	require.NoError(t, cmd.Run(), "failed to compile go")
    26  }