github.com/woremacx/kocha@v0.7.1-0.20150731103243-a5889322afc9/cmd/kocha-build/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"reflect"
    11  	"runtime"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func Test_buildCommand_Name(t *testing.T) {
    17  	c := &buildCommand{}
    18  	var actual interface{} = c.Name()
    19  	var expect interface{} = "kocha build"
    20  	if !reflect.DeepEqual(actual, expect) {
    21  		t.Errorf(`%T.Name() => %#v; want %#v`, c, actual, expect)
    22  	}
    23  }
    24  
    25  func Test_buildCommand_Run_withNoENVGiven(t *testing.T) {
    26  	c := &buildCommand{}
    27  	args := []string{}
    28  	err := c.Run(args)
    29  	actual := err.Error()
    30  	expect := "cannot import "
    31  	if !strings.HasPrefix(actual, expect) {
    32  		t.Errorf(`%T.Run(%#v) => %#v; want %#v`, c, args, actual, expect)
    33  	}
    34  }
    35  
    36  func Test_buildCommand_Run(t *testing.T) {
    37  	tempDir, err := ioutil.TempDir("", "Test_buildCommandRun")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer os.RemoveAll(tempDir)
    42  	appName := "testappname"
    43  	dstPath := filepath.Join(tempDir, "src", appName)
    44  	_, filename, _, _ := runtime.Caller(0)
    45  	baseDir := filepath.Dir(filename)
    46  	testdataDir := filepath.Join(baseDir, "testdata")
    47  	if err := copyAll(testdataDir, dstPath); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if err := os.Chdir(dstPath); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	origGOPATH := build.Default.GOPATH
    54  	defer func() {
    55  		build.Default.GOPATH = origGOPATH
    56  		os.Setenv("GOPATH", origGOPATH)
    57  	}()
    58  	build.Default.GOPATH = tempDir + string(filepath.ListSeparator) + build.Default.GOPATH
    59  	os.Setenv("GOPATH", build.Default.GOPATH)
    60  	f, err := os.OpenFile(os.DevNull, os.O_WRONLY, os.ModePerm)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	defer f.Close()
    65  	oldStdout, oldStderr := os.Stdout, os.Stderr
    66  	os.Stdout, os.Stderr = f, f
    67  	defer func() {
    68  		os.Stdout, os.Stderr = oldStdout, oldStderr
    69  	}()
    70  	c := &buildCommand{}
    71  	args := []string{}
    72  	err = c.Run(args)
    73  	var actual interface{} = err
    74  	var expect interface{} = nil
    75  	if !reflect.DeepEqual(actual, expect) {
    76  		t.Errorf(`%T.Run(%#v) => %#v; want %#v`, c, args, actual, expect)
    77  	}
    78  
    79  	tmpDir := filepath.Join(dstPath, "tmp")
    80  	if _, err := os.Stat(tmpDir); err == nil {
    81  		t.Errorf("Expect %v was removed, but exists", tmpDir)
    82  	}
    83  
    84  	execName := appName
    85  	if runtime.GOOS == "windows" {
    86  		execName += ".exe"
    87  	}
    88  	execPath := filepath.Join(dstPath, execName)
    89  	if _, err := os.Stat(execPath); err != nil {
    90  		t.Fatalf("Expect %v is exists, but not exists", execName)
    91  	}
    92  
    93  	output, err := exec.Command(execPath, "-v").CombinedOutput()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	actual = string(output)
    98  	expect = fmt.Sprintf("%s version ", execName)
    99  	if !strings.HasPrefix(actual.(string), expect.(string)) {
   100  		t.Errorf("Expect starts with %v, but %v", expect, actual)
   101  	}
   102  }