@@ -15,10 +15,11 @@ import (
1515const llcppgGoVersion = "1.20.14"
1616
1717type testCase struct {
18- modpath string
19- dir string
20- pkg upstream.Package
21- config map [string ]string // conan options
18+ modpath string
19+ dir string
20+ pkg upstream.Package
21+ config map [string ]string // conan options
22+ demosDir string
2223}
2324
2425var testCases = []testCase {
@@ -29,21 +30,31 @@ var testCases = []testCase{
2930 config : map [string ]string {
3031 "options" : "utils=True" ,
3132 },
33+ demosDir : "./testdata/cjson/demo" ,
34+ },
35+ {
36+ modpath : "github.com/goplus/llcppg/_cmptest/testdata/cjson/1.7.17/cjson" ,
37+ dir : "./testdata/cjson/1.7.17" ,
38+ pkg : upstream.Package {Name : "cjson" , Version : "1.7.17" },
39+ config : map [string ]string {
40+ "options" : "utils=True" ,
41+ },
42+ demosDir : "./testdata/cjson/demo" ,
3243 },
3344}
3445
3546func TestEnd2End (t * testing.T ) {
3647 for _ , tc := range testCases {
3748 tc := tc
38- t .Run (tc .pkg .Name , func (t * testing.T ) {
39- t .Parallel ()
49+ t .Run (fmt .Sprintf ("%s/%s" , tc .pkg .Name , tc .pkg .Version ), func (t * testing.T ) {
4050 testFrom (t , tc , false )
4151 })
4252 }
4353}
4454
4555func testFrom (t * testing.T , tc testCase , gen bool ) {
4656 wd , _ := os .Getwd ()
57+ dir := filepath .Join (wd , tc .dir )
4758 conanDir , err := os .MkdirTemp ("" , "llcppg_end2end_test_conan_dir_*" )
4859 if err != nil {
4960 t .Fatal (err )
@@ -68,33 +79,82 @@ func testFrom(t *testing.T, tc testCase, gen bool) {
6879 t .Fatal (err )
6980 }
7081
71- cmd := exec .Command ("llcppg" , "-v" , "-mod=" + tc .modpath )
72- cmd .Dir = resultDir
73- cmd .Stdout = os .Stdout
74- cmd .Stderr = os .Stderr
75- lockGoVersion (cmd , conanDir )
82+ cmd := command (resultDir , "llcppg" , "-v" , "-mod=" + tc .modpath )
83+ cmd .Env = append (cmd .Env , goVerEnv ())
84+ cmd .Env = append (cmd .Env , pcPathEnv (conanDir )... )
7685
7786 err = cmd .Run ()
7887 if err != nil {
7988 t .Fatal (err )
8089 }
90+
8191 // llcppg.symb.json is a middle file
8292 os .Remove (filepath .Join (resultDir , config .LLCPPG_SYMB ))
8393
8494 if gen {
85- os .RemoveAll (filepath .Join (wd , tc .dir ))
86- os .Rename (resultDir , filepath .Join (wd , tc .dir ))
87- return
95+ os .RemoveAll (dir )
96+ os .Rename (resultDir , dir )
97+ } else {
98+ // check the result is the same as the expected result
99+ // when have diff,will got exit code 1
100+ diffCmd := command (wd , "git" , "diff" , "--no-index" , dir , resultDir )
101+ err = diffCmd .Run ()
102+ if err != nil {
103+ t .Fatal (err )
104+ }
105+ }
106+ runDemos (t , filepath .Join (wd , tc .demosDir ), tc .pkg .Name , filepath .Join (dir , tc .pkg .Name ), conanDir )
107+ }
108+
109+ // pkgpath is the filepath use to replace the import path in demo's go.mod
110+ func runDemos (t * testing.T , demosPath string , pkgname , pkgpath , pcPath string ) {
111+ tempDemosPath , err := os .MkdirTemp ("" , "llcppg_end2end_test_demos_*" )
112+ if err != nil {
113+ t .Fatal (err )
114+ }
115+ defer os .RemoveAll (tempDemosPath )
116+ err = os .CopyFS (tempDemosPath , os .DirFS (demosPath ))
117+ if err != nil {
118+ t .Fatal (err )
119+ }
120+
121+ goMod := command (tempDemosPath , "go" , "mod" , "init" , "test" )
122+ err = goMod .Run ()
123+ if err != nil {
124+ t .Fatal (err )
125+ }
126+
127+ replace := command (tempDemosPath , "go" , "mod" , "edit" , "-replace" , pkgname + "=" + pkgpath )
128+ err = replace .Run ()
129+ if err != nil {
130+ t .Fatal (err )
88131 }
89132
90- diffCmd := exec .Command ("git" , "diff" , "--no-index" , tc .dir , resultDir )
91- diffCmd .Dir = wd
92- diffCmd .Stdout = os .Stdout
93- diffCmd .Stderr = os .Stderr
94- err = diffCmd .Run ()
133+ tidy := command (tempDemosPath , "go" , "mod" , "tidy" )
134+ err = tidy .Run ()
95135 if err != nil {
96136 t .Fatal (err )
97137 }
138+
139+ demos , err := os .ReadDir (tempDemosPath )
140+ if err != nil {
141+ t .Fatal (err )
142+ }
143+
144+ for _ , demo := range demos {
145+ if ! demo .IsDir () {
146+ continue
147+ }
148+ demoPath := filepath .Join (tempDemosPath , demo .Name ())
149+ demoCmd := command (demoPath , "llgo" , "run" , "." )
150+ demoCmd .Env = append (demoCmd .Env , llgoEnv ()... )
151+ demoCmd .Env = append (demoCmd .Env , pcPathEnv (pcPath )... )
152+ err = demoCmd .Run ()
153+ if err != nil {
154+ t .Fatal (err )
155+ }
156+ }
157+
98158}
99159
100160func appendPCPath (path string ) string {
@@ -104,15 +164,29 @@ func appendPCPath(path string) string {
104164 return path
105165}
106166
107- // lockGoVersion locks current Go version to `llcppgGoVersion` via GOTOOLCHAIN
108- func lockGoVersion ( cmd * exec. Cmd , pcPath string ) {
109- // don't change global settings, use temporary environment.
110- // see issue: https://github.com/goplus/llpkgstore /issues/18
111- setPath ( cmd , pcPath )
112- cmd . Env = append ( cmd . Env , fmt . Sprintf ( "GOTOOLCHAIN=go%s" , llcppgGoVersion ))
167+ // llgo env
168+ func llgoEnv () [] string {
169+ return [] string {
170+ // for https://github.com/goplus/llgo /issues/1135
171+ "LLGO_RPATH_CHANGE=on" ,
172+ }
113173}
114174
115- func setPath (cmd * exec.Cmd , path string ) {
175+ // env for pkg-config
176+ func pcPathEnv (path string ) []string {
116177 pcPath := fmt .Sprintf ("PKG_CONFIG_PATH=%s" , appendPCPath (path ))
117- cmd .Env = append (os .Environ (), pcPath )
178+ return append (os .Environ (), pcPath )
179+ }
180+
181+ // control the go version in output version
182+ func goVerEnv () string {
183+ return fmt .Sprintf ("GOTOOLCHAIN=go%s" , llcppgGoVersion )
184+ }
185+
186+ func command (dir string , app string , args ... string ) * exec.Cmd {
187+ cmd := exec .Command (app , args ... )
188+ cmd .Dir = dir
189+ cmd .Stdout = os .Stdout
190+ cmd .Stderr = os .Stderr
191+ return cmd
118192}
0 commit comments