2018-06-22 07:01:36 -05:00
|
|
|
package gaper
|
2018-06-20 09:54:10 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-06-20 21:15:58 -05:00
|
|
|
"runtime"
|
2018-06-20 09:54:10 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuilderSuccessBuild(t *testing.T) {
|
|
|
|
bArgs := []string{}
|
2018-06-20 21:15:58 -05:00
|
|
|
bin := resolveBinNameByOS("srv")
|
2018-06-20 09:54:10 -05:00
|
|
|
dir := filepath.Join("testdata", "server")
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't get current working directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBuilder(dir, bin, wd, bArgs)
|
|
|
|
err = b.Build()
|
|
|
|
assert.Nil(t, err, "build error")
|
|
|
|
|
|
|
|
file, err := os.Open(filepath.Join(wd, bin))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't open open built binary: %v", err)
|
|
|
|
}
|
|
|
|
assert.NotNil(t, file, "binary not written properly")
|
|
|
|
}
|
2018-06-20 20:40:09 -05:00
|
|
|
|
|
|
|
func TestBuilderFailureBuild(t *testing.T) {
|
|
|
|
bArgs := []string{}
|
|
|
|
bin := "srv"
|
|
|
|
dir := filepath.Join("testdata", "build-failure")
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't get current working directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBuilder(dir, bin, wd, bArgs)
|
|
|
|
err = b.Build()
|
|
|
|
assert.NotNil(t, err, "build error")
|
2018-09-08 08:35:29 -05:00
|
|
|
assert.Equal(t, err.Error(), "build failed with exit status 2\n"+
|
2019-11-12 15:37:44 -06:00
|
|
|
"# github.com/maxcnunes/gaper/testdata/build-failure\n"+
|
2018-09-08 08:35:29 -05:00
|
|
|
"./main.go:4:6: func main must have no arguments and no return values\n"+
|
2022-08-07 08:10:24 -05:00
|
|
|
"./main.go:5:1: missing return\n")
|
2018-06-20 20:40:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderDefaultBinName(t *testing.T) {
|
|
|
|
bin := ""
|
|
|
|
dir := filepath.Join("testdata", "server")
|
|
|
|
wd := "/src/projects/project-name"
|
|
|
|
b := NewBuilder(dir, bin, wd, nil)
|
2018-06-20 21:15:58 -05:00
|
|
|
assert.Equal(t, b.Binary(), resolveBinNameByOS("project-name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveBinNameByOS(name string) string {
|
|
|
|
if runtime.GOOS == OSWindows {
|
|
|
|
name += ".exe"
|
|
|
|
}
|
|
|
|
return name
|
2018-06-20 20:40:09 -05:00
|
|
|
}
|