Skip to content

Commit

Permalink
Moved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Gajewski committed Nov 12, 2016
1 parent 2c61402 commit 32a89ae
Show file tree
Hide file tree
Showing 18 changed files with 739 additions and 134 deletions.
20 changes: 12 additions & 8 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ func (c *Cli) GenerateFile() error {

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

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

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

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

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

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

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

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

return exec.Command("go", "build", "-x", "-v", "-buildmode=plugin", "-o", pluginPath, pluginPackage).Run()
command := []string{"build", "-x", "-v", "-buildmode=plugin", "-o", pluginPath, pluginPackage}

c.logger.Printf("Running: go %s", strings.Join(command, " "))

return exec.Command("go", command...).Run()
}

func (c *Cli) buildCommandArgs() string {
Expand Down Expand Up @@ -250,7 +254,7 @@ func (c *Cli) buildCommandArgs() string {

func validateConfig(config *Config) error {
if config.PluginPath == "" && config.PluginPackage == "" {
return fmt.Errorf("Either PluginPath or PluginPackage must be provided")
return fmt.Errorf("either PluginPath or PluginPackage must be provided")
}

if config.ForcePluginRebuild && config.PluginPackage == "" {
Expand Down
13 changes: 7 additions & 6 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -46,9 +45,9 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
t.Logf("[Test %d] Generating %s plugin...", i, testCase.Plugin)

config := Config{
PluginPackage: fmt.Sprintf("../internal/test_fixtures/%s", testCase.Plugin),
OutputPath: fmt.Sprintf("../internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin),
PluginPath: fmt.Sprintf("../internal/test_fixtures/generated/%s/plugin.so", testCase.Plugin),
PluginPackage: fmt.Sprintf("./internal/test_fixtures/%s", testCase.Plugin),
OutputPath: fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.go", testCase.Plugin),
PluginPath: fmt.Sprintf("./internal/test_fixtures/generated/%s/plugin.so", testCase.Plugin),
FormatCode: true,
CheckSha256: true,
ForcePluginRebuild: true,
Expand All @@ -57,7 +56,9 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
AsInterface: testCase.AsInterface,
}

client, err := New(config, log.New(ioutil.Discard, "", 0))
t.Logf("[Test %d] Generator config: %+v", i, config)

client, err := New(config, log.New(os.Stdout, "", 0))
if err != nil {
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, err)
}
Expand All @@ -66,7 +67,7 @@ func TestWillGenerateComplexPluginWithoutErrors(t *testing.T) {
t.Fatalf("[Test %d] Expected err to be nil, actual: %s", i, generateErr)
}

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

t.Logf("[Test %d] Running plugin via %s", i, runFile)
output, err := runPlugin(testCase.ExecutedCode, runFile, config)
Expand Down
Binary file added cli/internal/test_fixtures/basic_plugin/plugin.so
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package main

import "C"

import (
"fmt"
"net/http"

http2 "github.com/wendigo/go-bind-plugin/internal/test_fixtures/complex_plugin/http"
http2 "github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/complex_plugin/http"
)

// DoWork is only exported for testing purposes
func DoWork(h *http.Header) *http.Header {
return h
}

// PrintHello is only exported for testing purposes
func PrintHello() string {
return "Hello world!"
Expand All @@ -22,16 +18,6 @@ func PrintHello2(in int) string {
return fmt.Sprintf("Hello %d", in)
}

// DoWork3 is only exported for testing purposes
func DoWork3() *http.Header {
return &http.Header{}
}

// DoWork4 is only exported for testing purposes
func DoWork4() http.Header {
return http.Header{}
}

// DoWorkInt is only exported for testing purposes
func DoWorkInt(x map[string]int) []int32 {
return []int32{}
Expand All @@ -43,8 +29,8 @@ func DoWorkOnChan(x <-chan int) chan<- int32 {
}

// DoWorkOnChan2 is only exported for testing purposes
func DoWorkOnChan2(x <-chan http2.Work) chan<- http.Header {
return make(chan<- http.Header)
func DoWorkOnChan2(x <-chan http2.Work) chan<- http2.Work {
return make(chan<- http2.Work)
}

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

// DoWork2 is only exported for testing purposes
func DoWork2(work http.Header, work2 http.Header) string {
return "Hello"
}

// X is only exported for testing purposes
var X = http.Header{"Name": []string{"Value"}}
var X = http2.Work{Work: "Hello world!"}

// Y is only exported for testing purposes
var Y = &http.Header{"Name": []string{"Value2"}}

// Z is only exported for testing purposes
var Z = DoWork4

// V is only exported for testing purposes
var V = DoWork2
var Y = &http2.Work{Work: "Hello"}
178 changes: 178 additions & 0 deletions cli/internal/test_fixtures/generated/basic_plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package main

// Autogenerated by github.com/wendigo/go-bind-plugin on 2016-11-12 19:45:36.989271091 +0100 CET, do not edit!
// 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
//
// Plugin ./internal/test_fixtures/generated/basic_plugin/plugin.so info:
// - package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
// - size: 2447824 bytes
// - sha256: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"plugin"
"reflect"
"strings"
)

// TestWrapper wraps symbols (functions and variables) exported by plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin
type TestWrapper struct {
// Exported functions
_NonReturningFunction func()
_ReturningInt32 func() int32
_ReturningIntArray func() [3]int32
_ReturningStringSlice func() []string

// Exported variables (public references)

}

// NonReturningFunction function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'NonReturningFunction'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#NonReturningFunction
func (p *TestWrapper) NonReturningFunction() {
p._NonReturningFunction()
}

// ReturningInt32 function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningInt32'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningInt32
func (p *TestWrapper) ReturningInt32() int32 {
return p._ReturningInt32()
}

// ReturningIntArray function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningIntArray'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningIntArray
func (p *TestWrapper) ReturningIntArray() [3]int32 {
return p._ReturningIntArray()
}

// ReturningStringSlice function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningStringSlice'
//
// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningStringSlice
func (p *TestWrapper) ReturningStringSlice() []string {
return p._ReturningStringSlice()
}

// String returnes textual representation of the wrapper. It provides info on exported symbols and variables.
func (p *TestWrapper) String() string {
var lines []string
lines = append(lines, "Struct TestWrapper:")
lines = append(lines, "\t- Generated on: 2016-11-12 19:45:36.989271091 +0100 CET")
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")
lines = append(lines, "\nPlugin info:")
lines = append(lines, "\t- package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin")
lines = append(lines, "\t- sha256 sum: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483")
lines = append(lines, "\t- size: 2447824 bytes")
lines = append(lines, "\nExported functions (4):")
lines = append(lines, "\t- NonReturningFunction func()")
lines = append(lines, "\t- ReturningInt32 func() (int32)")
lines = append(lines, "\t- ReturningIntArray func() ([3]int32)")
lines = append(lines, "\t- ReturningStringSlice func() ([]string)")

return strings.Join(lines, "\n")
}

// BindTestWrapper loads plugin from the given path and binds symbols (variables and functions)
// to the TestWrapper struct.
// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned.
func BindTestWrapper(path string) (*TestWrapper, error) {
p, err := plugin.Open(path)

if err != nil {
return nil, fmt.Errorf("could not open plugin: %s", err)
}

fileChecksum := func(path string) (string, error) {
hasher := sha256.New()

file, err := os.Open(path)

if err != nil {
return "", err
}
defer file.Close()

if _, err := io.Copy(hasher, file); err != nil {
return "", err
}

return hex.EncodeToString(hasher.Sum(nil)), nil
}

checksum, err := fileChecksum(path)
if err != nil {
return nil, fmt.Errorf("could not calculate file %s checksum", path)
}

if checksum != "e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483" {
return nil, fmt.Errorf("SHA256 checksum mismatch (expected: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483, actual: %s)", checksum)
}

ret := new(TestWrapper)

funcNonReturningFunction, err := p.Lookup("NonReturningFunction")
if err != nil {
return nil, fmt.Errorf("could not import function 'NonReturningFunction', symbol not found: %s", err)
}

if typed, ok := funcNonReturningFunction.(func()); ok {
ret._NonReturningFunction = typed
} else {
return nil, fmt.Errorf("could not import function 'NonReturningFunction', incompatible types 'func()' and '%s'", reflect.TypeOf(funcNonReturningFunction))
}

funcReturningInt32, err := p.Lookup("ReturningInt32")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningInt32', symbol not found: %s", err)
}

if typed, ok := funcReturningInt32.(func() int32); ok {
ret._ReturningInt32 = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningInt32', incompatible types 'func() (int32)' and '%s'", reflect.TypeOf(funcReturningInt32))
}

funcReturningIntArray, err := p.Lookup("ReturningIntArray")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningIntArray', symbol not found: %s", err)
}

if typed, ok := funcReturningIntArray.(func() [3]int32); ok {
ret._ReturningIntArray = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningIntArray', incompatible types 'func() ([3]int32)' and '%s'", reflect.TypeOf(funcReturningIntArray))
}

funcReturningStringSlice, err := p.Lookup("ReturningStringSlice")
if err != nil {
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', symbol not found: %s", err)
}

if typed, ok := funcReturningStringSlice.(func() []string); ok {
ret._ReturningStringSlice = typed
} else {
return nil, fmt.Errorf("could not import function 'ReturningStringSlice', incompatible types 'func() ([]string)' and '%s'", reflect.TypeOf(funcReturningStringSlice))
}

return ret, nil
}


func main() {
pl, err := BindTestWrapper("./internal/test_fixtures/generated/basic_plugin/plugin.so")

if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(pl.ReturningIntArray())
}
Binary file not shown.
Loading

0 comments on commit 32a89ae

Please sign in to comment.