github.com/gotranspile/cxgo@v0.3.7/gcc_test.go (about) 1 package cxgo 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "runtime/debug" 9 "strings" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 14 "github.com/gotranspile/cxgo/internal/git" 15 "github.com/gotranspile/cxgo/libs" 16 "github.com/gotranspile/cxgo/types" 17 ) 18 19 func downloadGCC(t testing.TB, dst string) { 20 err := os.MkdirAll(dst, 0755) 21 require.NoError(t, err) 22 23 const ( 24 repo = "https://github.com/gcc-mirror/gcc.git" 25 sub = "gcc/testsuite/gcc.c-torture/compile" 26 branch = "releases/gcc-10.2.0" 27 ) 28 29 dir := filepath.Join(os.TempDir(), "cxgo_gcc_git") 30 _ = os.RemoveAll(dir) 31 32 t.Log("cloning", repo, "to", dir) 33 err = git.Clone(repo, branch, dir) 34 if err != nil { 35 os.RemoveAll(dst) 36 } 37 require.NoError(t, err) 38 defer os.RemoveAll(dir) 39 40 files, err := filepath.Glob(filepath.Join(dir, sub, "*.c")) 41 require.NoError(t, err) 42 require.NotEmpty(t, files) 43 44 for _, path := range files { 45 base := filepath.Base(path) 46 err = copyFile(path, filepath.Join(dst, base)) 47 require.NoError(t, err) 48 } 49 } 50 51 func TestGCCExecute(t *testing.T) { 52 if testing.Short() || os.Getenv("CXGO_RUN_TESTS_GCC") != "true" { 53 t.SkipNow() 54 } 55 dir := filepath.Join(testDataDir, "gcc") 56 57 ignoreTests := map[string]string{ 58 "limits-caselabels": "OOM", 59 } 60 61 blacklist := map[string]struct{}{} 62 63 isLib := map[string]struct{}{} 64 65 if _, err := os.Stat(dir); os.IsNotExist(err) { 66 downloadGCC(t, dir) 67 } 68 69 files, err := filepath.Glob(filepath.Join(dir, "*.c")) 70 require.NoError(t, err) 71 72 wd, err := os.Getwd() 73 require.NoError(t, err) 74 75 out := filepath.Join(os.TempDir(), "cxgo_test_gcc") 76 err = os.MkdirAll(out, 0755) 77 require.NoError(t, err) 78 79 goProject(t, out, wd) 80 goProjectMod(t, out) 81 82 for _, path := range files { 83 path := path 84 tname := strings.TrimSuffix(filepath.Base(path), ".c") 85 _, skip := blacklist[tname] 86 t.Run(tname, func(t *testing.T) { 87 if reason, ignore := ignoreTests[tname]; ignore { 88 t.Skip(reason) 89 } 90 //t.Parallel() 91 defer func() { 92 if r := recover(); r != nil { 93 if skip { 94 defer debug.PrintStack() 95 t.Skipf("panic: %v", r) 96 } else { 97 require.Nil(t, r) 98 } 99 } 100 if !t.Failed() && !t.Skipped() && skip { 101 t.Error("blacklisted test pass") 102 } 103 }() 104 oname := filepath.Base(path) + ".go" 105 env := libs.NewEnv(types.Config32()) 106 _, lib := isLib[tname] 107 if data, err := ioutil.ReadFile(path); err == nil && !bytes.Contains(data, []byte("main")) { 108 t.Log("testing as a library (no main found)") 109 lib = true 110 } 111 pkg := "main" 112 if lib { 113 pkg = "lib" 114 } 115 err = Translate(filepath.Dir(path), path, out, env, Config{ 116 Predef: ` 117 #include <stdlib.h> 118 #include <string.h> 119 `, 120 Package: pkg, 121 GoFile: oname, 122 MaxDecls: -1, 123 FixImplicitReturns: true, 124 }) 125 failOrSkip(t, err, skip) 126 127 t.Log(path) 128 t.Log(filepath.Join(out, oname)) 129 goRun(t, out, []string{oname}, runConfig{Arch32: false, Skip: skip, BuildOnly: lib}) 130 }) 131 } 132 }