forked from TrueCloudLab/neoneo-go
Merge pull request #568 from nspcc-dev/feature/compiler_clean_and_tests
compiler: clean and tests
This commit is contained in:
commit
9eb880a095
3 changed files with 58 additions and 34 deletions
|
@ -3,10 +3,7 @@ package compiler
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
|
||||||
"go/build"
|
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/types"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -61,11 +58,6 @@ func Compile(r io.Reader) ([]byte, error) {
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type archive struct {
|
|
||||||
f *ast.File
|
|
||||||
typeInfo *types.Info
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompileAndSave will compile and save the file to disk.
|
// CompileAndSave will compile and save the file to disk.
|
||||||
func CompileAndSave(src string, o *Options) ([]byte, error) {
|
func CompileAndSave(src string, o *Options) ([]byte, error) {
|
||||||
if !strings.HasSuffix(src, ".go") {
|
if !strings.HasSuffix(src, ".go") {
|
||||||
|
@ -90,11 +82,3 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
|
||||||
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
out := fmt.Sprintf("%s.%s", o.Outfile, o.Ext)
|
||||||
return b, ioutil.WriteFile(out, b, os.ModePerm)
|
return b, ioutil.WriteFile(out, b, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func gopath() string {
|
|
||||||
gopath := os.Getenv("GOPATH")
|
|
||||||
if len(gopath) == 0 {
|
|
||||||
gopath = build.Default.GOPATH
|
|
||||||
}
|
|
||||||
return gopath
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,21 +7,28 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/compiler"
|
"github.com/CityOfZion/neo-go/pkg/compiler"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
const examplePath = "../../examples"
|
const examplePath = "../../examples"
|
||||||
|
const exampleCompilePath = "testdata/compile"
|
||||||
|
const exampleSavePath = exampleCompilePath + "/save"
|
||||||
|
|
||||||
func TestExamplesFolder(t *testing.T) {
|
type compilerTestCase struct {
|
||||||
infos, err := ioutil.ReadDir(examplePath)
|
name string
|
||||||
if err != nil {
|
function func()
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompiler(t *testing.T) {
|
||||||
|
testCases := []compilerTestCase{
|
||||||
|
{
|
||||||
|
name: "TestCompile",
|
||||||
|
function: func() {
|
||||||
|
infos, err := ioutil.ReadDir(examplePath)
|
||||||
|
require.NoError(t, err)
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name()))
|
infos, err := ioutil.ReadDir(path.Join(examplePath, info.Name()))
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if len(infos) == 0 {
|
if len(infos) == 0 {
|
||||||
t.Fatal("detected smart contract folder with no contract in it")
|
t.Fatal("detected smart contract folder with no contract in it")
|
||||||
}
|
}
|
||||||
|
@ -32,6 +39,32 @@ func TestExamplesFolder(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "TestCompileAndSave",
|
||||||
|
function: func() {
|
||||||
|
infos, err := ioutil.ReadDir(exampleCompilePath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = os.MkdirAll(exampleSavePath, os.ModePerm)
|
||||||
|
require.NoError(t, err)
|
||||||
|
outfile := exampleSavePath + "/test.avm"
|
||||||
|
if _, err := compiler.CompileAndSave(exampleCompilePath+"/"+infos[0].Name(), &compiler.Options{Outfile: outfile}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err := os.RemoveAll(exampleSavePath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tcase := range testCases {
|
||||||
|
t.Run(tcase.name, func(t *testing.T) {
|
||||||
|
tcase.function()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterFilename(infos []os.FileInfo) string {
|
func filterFilename(infos []os.FileInfo) string {
|
||||||
|
|
7
pkg/compiler/testdata/compile/test.go
vendored
Normal file
7
pkg/compiler/testdata/compile/test.go
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package compile
|
||||||
|
|
||||||
|
import "github.com/CityOfZion/neo-go/pkg/interop/runtime"
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
runtime.Notify("Hello world!")
|
||||||
|
}
|
Loading…
Reference in a new issue