@@ -2,27 +2,33 @@ package main
2
2
3
3
import (
4
4
"bytes"
5
+ "flag"
5
6
"os"
6
7
"os/exec"
8
+ "path/filepath"
7
9
"testing"
8
10
)
9
11
10
- const embeddingTestOutput = "testdata/embedding_golden.txt"
12
+ const embeddingTestOutput = "testdata/embedding_out_golden.txt"
13
+
11
14
var regen = flag .Bool ("regen" , false , "regenerate golden files" )
12
15
13
16
func TestEmbeddedExample (t * testing.T ) {
14
17
15
18
tmp , err := os .MkdirTemp ("" , "go-python-embedding-" )
16
- if err != nil { t .Fatal (err ) }
19
+ if err != nil {
20
+ t .Fatal (err )
21
+ }
17
22
defer os .RemoveAll (tmp )
18
- cmd := exec .Command ("go" , "build" , "-o" , filepath .Join (tmp ,"exe" ), "." )
19
- err := cmd .Run ()
23
+
24
+ cmd := exec .Command ("go" , "build" , "-o" , filepath .Join (tmp , "exe" ), "." )
25
+ err = cmd .Run ()
20
26
if err != nil {
21
27
t .Fatalf ("failed to compile embedding example: %v" , err )
22
28
}
23
29
24
30
out := new (bytes.Buffer )
25
- cmd = exec .Command (filepath .Join (tmp ,"exe" ), "mylib-demo.py" )
31
+ cmd = exec .Command (filepath .Join (tmp , "exe" ), "mylib-demo.py" )
26
32
cmd .Stdout = out
27
33
28
34
err = cmd .Run ()
@@ -31,17 +37,20 @@ func TestEmbeddedExample(t *testing.T) {
31
37
}
32
38
33
39
testOutput := out .Bytes ()
40
+
41
+ flag .Parse ()
34
42
if * regen {
35
43
err = os .WriteFile (embeddingTestOutput , testOutput , 0644 )
36
44
if err != nil {
37
45
t .Fatalf ("failed to write test output: %v" , err )
38
46
}
39
47
}
40
- mustMatch , err := os .ReadFile (embeddingTestOutput )
41
- if err != nil {
42
- t .Fatalf ("failed read %q" , embeddingTestOutput )
43
- }
44
- if ! bytes .Equal (testOutput , mustMatch ) {
45
- t .Fatalf ("embedded test output did not match accepted output from %q" , embeddingTestOutput )
46
- }
48
+
49
+ mustMatch , err := os .ReadFile (embeddingTestOutput )
50
+ if err != nil {
51
+ t .Fatalf ("failed read %q" , embeddingTestOutput )
52
+ }
53
+ if ! bytes .Equal (testOutput , mustMatch ) {
54
+ t .Fatalf ("embedded test output did not match accepted output from %q" , embeddingTestOutput )
55
+ }
47
56
}
0 commit comments