github.com/bir3/gocompiler@v0.9.2202/gocompiler_test.go (about)

     1  package gocompiler_test
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/bir3/gocompiler"
    11  	"github.com/bir3/gocompiler/extra"
    12  )
    13  
    14  func TestMain(m *testing.M) {
    15  
    16  	// the go toolchain is built into the executable and must be given a chance to run
    17  	// => avoid side effects in init() as they will occur multiple times during compilation
    18  	if gocompiler.IsRunToolchainRequest() {
    19  		gocompiler.RunToolchain()
    20  		return
    21  	}
    22  
    23  	os.Exit(m.Run())
    24  }
    25  
    26  func compileAndRunString(t *testing.T, code string) string {
    27  	dir := t.TempDir()
    28  
    29  	err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(code), 0666)
    30  	if err != nil {
    31  		t.Fatalf("%s", err)
    32  	}
    33  
    34  	run := func(args ...string) string {
    35  		cmd, err := gocompiler.Command(os.Environ(), args...)
    36  		if err != nil {
    37  			t.Fatalf("%s", err)
    38  		}
    39  		cmd.Dir = dir
    40  		buf, err := cmd.CombinedOutput()
    41  		s := string(buf)
    42  		if err != nil {
    43  			cmd := strings.Join(args, " ")
    44  			t.Fatalf("cmd %s failed with %s - %s", cmd, err, s)
    45  		}
    46  		return s
    47  	}
    48  	run("go", "mod", "init", "abc")
    49  	run("go", "build")
    50  	cmd := exec.Command(filepath.Join(dir, "abc"))
    51  	buf, err := cmd.CombinedOutput()
    52  	s := string(buf)
    53  	if err != nil {
    54  		t.Fatalf("failed to run - %s - %s", s, err)
    55  	}
    56  
    57  	return s
    58  }
    59  
    60  func TestCompile(t *testing.T) {
    61  	t.Parallel()
    62  	goCode := `
    63  	package main
    64  	
    65  	import "fmt"
    66  	
    67  	func main() {
    68  			fmt.Printf("magic")
    69  	}`
    70  	output := compileAndRunString(t, goCode)
    71  	exp := "magic"
    72  	if output != exp {
    73  		t.Fatalf("expected string %q but got %q", exp, output)
    74  	}
    75  }
    76  
    77  func TestCompileWithC(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	goCode := `
    81  	package main
    82  	
    83  	// typedef int (*intFunc) ();
    84  	//
    85  	// int
    86  	// bridge_int_func(intFunc f)
    87  	// {
    88  	//		return f();
    89  	// }
    90  	//
    91  	// int fortytwo()
    92  	// {
    93  	//	    return 42;
    94  	// }
    95  	import "C"
    96  	import "fmt"
    97  	
    98  	func main() {
    99  		f := C.intFunc(C.fortytwo)
   100  		fmt.Println(int(C.bridge_int_func(f)))
   101  		// Output: 42
   102  	}
   103  	
   104  	`
   105  	output := compileAndRunString(t, goCode)
   106  	exp := "42\n"
   107  	if output != exp {
   108  		t.Fatalf("expected string %q but got %q", exp, output)
   109  	}
   110  }
   111  
   112  func TestMkdirAllRace(t *testing.T) {
   113  	t.Parallel()
   114  	dir := t.TempDir()
   115  	dir = filepath.Join(dir, "a", "b", "c")
   116  	err := extra.MkdirAllRace(dir, 0777)
   117  	if err != nil {
   118  		t.Fatalf("%s", err)
   119  	}
   120  	info, err := os.Stat(dir)
   121  	if err != nil {
   122  		t.Fatalf("%s", err)
   123  	}
   124  	if !info.IsDir() {
   125  		t.Fatal("failed")
   126  	}
   127  }