Skip to content

Commit 32a89ae

Browse files
author
Mateusz Gajewski
committed
Moved tests
1 parent 2c61402 commit 32a89ae

File tree

18 files changed

+739
-134
lines changed

18 files changed

+739
-134
lines changed

cli/cli.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ func (c *Cli) GenerateFile() error {
9393

9494
if !c.pluginExists(c.config.PluginPath) || c.config.ForcePluginRebuild {
9595
if err := c.buildPluginFromSources(c.config.PluginPath, c.config.PluginPackage); err != nil {
96-
return fmt.Errorf("Could not build plugin from sources: %s", err)
96+
return fmt.Errorf("could not build plugin from sources: %s", err)
9797
}
9898
}
9999

100100
c.logger.Printf("Loading and analyzing plugin from: %s", c.config.PluginPath)
101101
structure, err := loadPlugin(c.config.PluginPath, imports)
102102
if err != nil {
103-
return fmt.Errorf("Could not load plugin from %s: %s", c.config.PluginPath, err)
103+
return fmt.Errorf("could not load plugin from %s: %s", c.config.PluginPath, err)
104104
}
105105

106106
if structure.SymbolsLen() == 0 {
107-
return fmt.Errorf("Plugin %s does not export any symbols", c.config.PluginPath)
107+
return fmt.Errorf("plugin %s does not export any symbols", c.config.PluginPath)
108108
}
109109

110110
outputPackage := c.config.OutputPackage
@@ -114,7 +114,7 @@ func (c *Cli) GenerateFile() error {
114114

115115
outputFile, err := c.createOutputFile(c.config.OutputPath)
116116
if err != nil {
117-
return fmt.Errorf("Could not create output file: %s", err)
117+
return fmt.Errorf("could not create output file: %s", err)
118118
}
119119

120120
tpl, err := template.New("generate").Parse(generateFileTemplate)
@@ -144,7 +144,7 @@ func (c *Cli) GenerateFile() error {
144144
if c.config.FormatCode {
145145
c.logger.Printf("Formatting generated file with gofmt -s -w %s", c.config.OutputPath)
146146
if err := c.formatOutputCode(c.config.OutputPath); err != nil {
147-
return fmt.Errorf("Could not format output code: %s", err)
147+
return fmt.Errorf("could not format output code: %s", err)
148148
}
149149
}
150150

@@ -172,7 +172,7 @@ func (c *Cli) createOutputDir(path string) error {
172172
if info, err := os.Stat(path); err != nil && !os.IsNotExist(err) {
173173
return err
174174
} else if err == nil && !info.IsDir() {
175-
return fmt.Errorf("Output path %s exists and is not a directory", path)
175+
return fmt.Errorf("output path %s exists and is not a directory", path)
176176
}
177177

178178
if err := os.MkdirAll(path, 0700); err != nil {
@@ -210,7 +210,11 @@ func (c *Cli) buildPluginFromSources(pluginPath string, pluginPackage string) er
210210
return err
211211
}
212212

213-
return exec.Command("go", "build", "-x", "-v", "-buildmode=plugin", "-o", pluginPath, pluginPackage).Run()
213+
command := []string{"build", "-x", "-v", "-buildmode=plugin", "-o", pluginPath, pluginPackage}
214+
215+
c.logger.Printf("Running: go %s", strings.Join(command, " "))
216+
217+
return exec.Command("go", command...).Run()
214218
}
215219

216220
func (c *Cli) buildCommandArgs() string {
@@ -250,7 +254,7 @@ func (c *Cli) buildCommandArgs() string {
250254

251255
func validateConfig(config *Config) error {
252256
if config.PluginPath == "" && config.PluginPackage == "" {
253-
return fmt.Errorf("Either PluginPath or PluginPackage must be provided")
257+
return fmt.Errorf("either PluginPath or PluginPackage must be provided")
254258
}
255259

256260
if config.ForcePluginRebuild && config.PluginPackage == "" {

cli/cli_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"bytes"
66
"fmt"
7-
"io/ioutil"
87
"log"
98
"os"
109
"os/exec"
@@ -46,9 +45,9 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
4645
t.Logf("[Test %d] Generating %s plugin...", i, testCase.Plugin)
4746

4847
config := Config{
49-
PluginPackage: fmt.Sprintf("../internal/test_fixtures/%s", testCase.Plugin),
50-
OutputPath: fmt.Sprintf("../internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin),
51-
PluginPath: fmt.Sprintf("../internal/test_fixtures/generated/%s/plugin.so", testCase.Plugin),
48+
PluginPackage: fmt.Sprintf("./internal/test_fixtures/%s", testCase.Plugin),
49+
OutputPath: fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin),
50+
PluginPath: fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.so", testCase.Plugin),
5251
FormatCode: true,
5352
CheckSha256: true,
5453
ForcePluginRebuild: true,
@@ -57,7 +56,9 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
5756
AsInterface: testCase.AsInterface,
5857
}
5958

60-
client, err := New(config, log.New(ioutil.Discard, "", 0))
59+
t.Logf("[Test %d] Generator config: %+v", i, config)
60+
61+
client, err := New(config, log.New(os.Stdout, "", 0))
6162
if err != nil {
6263
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, err)
6364
}
@@ -66,7 +67,7 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
6667
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, generateErr)
6768
}
6869

69-
runFile := fmt.Sprintf("../internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin)
70+
runFile := fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin)
7071

7172
t.Logf("[Test %d] Running plugin via %s", i, runFile)
7273
output, err := runPlugin(testCase.ExecutedCode, runFile, config)
2.33 MB
Binary file not shown.
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package main
22

3+
import "C"
4+
35
import (
46
"fmt"
5-
"net/http"
67

7-
http2 "github.com/wendigo/go-bind-plugin/internal/test_fixtures/complex_plugin/http"
8+
http2 "github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/complex_plugin/http"
89
)
910

10-
// DoWork is only exported for testing purposes
11-
func DoWork(h *http.Header) *http.Header {
12-
return h
13-
}
14-
1511
// PrintHello is only exported for testing purposes
1612
func PrintHello() string {
1713
return "Hello world!"
@@ -22,16 +18,6 @@ func PrintHello2(in int) string {
2218
return fmt.Sprintf("Hello %d", in)
2319
}
2420

25-
// DoWork3 is only exported for testing purposes
26-
func DoWork3() *http.Header {
27-
return &http.Header{}
28-
}
29-
30-
// DoWork4 is only exported for testing purposes
31-
func DoWork4() http.Header {
32-
return http.Header{}
33-
}
34-
3521
// DoWorkInt is only exported for testing purposes
3622
func DoWorkInt(x map[string]int) []int32 {
3723
return []int32{}
@@ -43,8 +29,8 @@ func DoWorkOnChan(x <-chan int) chan<- int32 {
4329
}
4430

4531
// DoWorkOnChan2 is only exported for testing purposes
46-
func DoWorkOnChan2(x <-chan http2.Work) chan<- http.Header {
47-
return make(chan<- http.Header)
32+
func DoWorkOnChan2(x <-chan http2.Work) chan<- http2.Work {
33+
return make(chan<- http2.Work)
4834
}
4935

5036
// DoWorkIntArray is only exported for testing purposes
@@ -72,19 +58,8 @@ func DoWorkMap(m map[string]*http2.Work) *http2.Work {
7258
return nil
7359
}
7460

75-
// DoWork2 is only exported for testing purposes
76-
func DoWork2(work http.Header, work2 http.Header) string {
77-
return "Hello"
78-
}
79-
8061
// X is only exported for testing purposes
81-
var X = http.Header{"Name": []string{"Value"}}
62+
var X = http2.Work{Work: "Hello world!"}
8263

8364
// Y is only exported for testing purposes
84-
var Y = &http.Header{"Name": []string{"Value2"}}
85-
86-
// Z is only exported for testing purposes
87-
var Z = DoWork4
88-
89-
// V is only exported for testing purposes
90-
var V = DoWork2
65+
var Y = &http2.Work{Work: "Hello"}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package main
2+
3+
// Autogenerated by github.com/wendigo/go-bind-plugin on 2016-11-12 19:45:36.989271091 +0100 CET, do not edit!
4+
// Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild
5+
//
6+
// Plugin ./internal/test_fixtures/generated/basic_plugin/plugin.so info:
7+
// - package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
8+
// - size: 2447824 bytes
9+
// - sha256: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483
10+
11+
import (
12+
"crypto/sha256"
13+
"encoding/hex"
14+
"fmt"
15+
"io"
16+
"os"
17+
"plugin"
18+
"reflect"
19+
"strings"
20+
)
21+
22+
// TestWrapper wraps symbols (functions and variables) exported by plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
23+
//
24+
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
25+
type TestWrapper struct {
26+
// Exported functions
27+
_NonReturningFunction func()
28+
_ReturningInt32 func() int32
29+
_ReturningIntArray func() [3]int32
30+
_ReturningStringSlice func() []string
31+
32+
// Exported variables (public references)
33+
34+
}
35+
36+
// NonReturningFunction function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'NonReturningFunction'
37+
//
38+
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#NonReturningFunction
39+
func (p *TestWrapper) NonReturningFunction() {
40+
p._NonReturningFunction()
41+
}
42+
43+
// ReturningInt32 function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningInt32'
44+
//
45+
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningInt32
46+
func (p *TestWrapper) ReturningInt32() int32 {
47+
return p._ReturningInt32()
48+
}
49+
50+
// ReturningIntArray function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningIntArray'
51+
//
52+
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningIntArray
53+
func (p *TestWrapper) ReturningIntArray() [3]int32 {
54+
return p._ReturningIntArray()
55+
}
56+
57+
// ReturningStringSlice function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningStringSlice'
58+
//
59+
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningStringSlice
60+
func (p *TestWrapper) ReturningStringSlice() []string {
61+
return p._ReturningStringSlice()
62+
}
63+
64+
// String returnes textual representation of the wrapper. It provides info on exported symbols and variables.
65+
func (p *TestWrapper) String() string {
66+
var lines []string
67+
lines = append(lines, "Struct TestWrapper:")
68+
lines = append(lines, "\t- Generated on: 2016-11-12 19:45:36.989271091 +0100 CET")
69+
lines = append(lines, "\t- Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild")
70+
lines = append(lines, "\nPlugin info:")
71+
lines = append(lines, "\t- package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin")
72+
lines = append(lines, "\t- sha256 sum: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483")
73+
lines = append(lines, "\t- size: 2447824 bytes")
74+
lines = append(lines, "\nExported functions (4):")
75+
lines = append(lines, "\t- NonReturningFunction func()")
76+
lines = append(lines, "\t- ReturningInt32 func() (int32)")
77+
lines = append(lines, "\t- ReturningIntArray func() ([3]int32)")
78+
lines = append(lines, "\t- ReturningStringSlice func() ([]string)")
79+
80+
return strings.Join(lines, "\n")
81+
}
82+
83+
// BindTestWrapper loads plugin from the given path and binds symbols (variables and functions)
84+
// to the TestWrapper struct.
85+
// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned.
86+
func BindTestWrapper(path string) (*TestWrapper, error) {
87+
p, err := plugin.Open(path)
88+
89+
if err != nil {
90+
return nil, fmt.Errorf("could not open plugin: %s", err)
91+
}
92+
93+
fileChecksum := func(path string) (string, error) {
94+
hasher := sha256.New()
95+
96+
file, err := os.Open(path)
97+
98+
if err != nil {
99+
return "", err
100+
}
101+
defer file.Close()
102+
103+
if _, err := io.Copy(hasher, file); err != nil {
104+
return "", err
105+
}
106+
107+
return hex.EncodeToString(hasher.Sum(nil)), nil
108+
}
109+
110+
checksum, err := fileChecksum(path)
111+
if err != nil {
112+
return nil, fmt.Errorf("could not calculate file %s checksum", path)
113+
}
114+
115+
if checksum != "e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483" {
116+
return nil, fmt.Errorf("SHA256 checksum mismatch (expected: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483, actual: %s)", checksum)
117+
}
118+
119+
ret := new(TestWrapper)
120+
121+
funcNonReturningFunction, err := p.Lookup("NonReturningFunction")
122+
if err != nil {
123+
return nil, fmt.Errorf("could not import function 'NonReturningFunction', symbol not found: %s", err)
124+
}
125+
126+
if typed, ok := funcNonReturningFunction.(func()); ok {
127+
ret._NonReturningFunction = typed
128+
} else {
129+
return nil, fmt.Errorf("could not import function 'NonReturningFunction', incompatible types 'func()' and '%s'", reflect.TypeOf(funcNonReturningFunction))
130+
}
131+
132+
funcReturningInt32, err := p.Lookup("ReturningInt32")
133+
if err != nil {
134+
return nil, fmt.Errorf("could not import function 'ReturningInt32', symbol not found: %s", err)
135+
}
136+
137+
if typed, ok := funcReturningInt32.(func() int32); ok {
138+
ret._ReturningInt32 = typed
139+
} else {
140+
return nil, fmt.Errorf("could not import function 'ReturningInt32', incompatible types 'func() (int32)' and '%s'", reflect.TypeOf(funcReturningInt32))
141+
}
142+
143+
funcReturningIntArray, err := p.Lookup("ReturningIntArray")
144+
if err != nil {
145+
return nil, fmt.Errorf("could not import function 'ReturningIntArray', symbol not found: %s", err)
146+
}
147+
148+
if typed, ok := funcReturningIntArray.(func() [3]int32); ok {
149+
ret._ReturningIntArray = typed
150+
} else {
151+
return nil, fmt.Errorf("could not import function 'ReturningIntArray', incompatible types 'func() ([3]int32)' and '%s'", reflect.TypeOf(funcReturningIntArray))
152+
}
153+
154+
funcReturningStringSlice, err := p.Lookup("ReturningStringSlice")
155+
if err != nil {
156+
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', symbol not found: %s", err)
157+
}
158+
159+
if typed, ok := funcReturningStringSlice.(func() []string); ok {
160+
ret._ReturningStringSlice = typed
161+
} else {
162+
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', incompatible types 'func() ([]string)' and '%s'", reflect.TypeOf(funcReturningStringSlice))
163+
}
164+
165+
return ret, nil
166+
}
167+
168+
169+
func main() {
170+
pl, err := BindTestWrapper("./internal/test_fixtures/generated/basic_plugin/plugin.so")
171+
172+
if err != nil {
173+
fmt.Println(err)
174+
os.Exit(1)
175+
}
176+
177+
fmt.Println(pl.ReturningIntArray())
178+
}
2.33 MB
Binary file not shown.

0 commit comments

Comments
 (0)