github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmg/SubCommand/goCmd/GoRun_test.go (about)

     1  package goCmd
     2  
     3  import (
     4  	"github.com/bronze1man/kmg/kmgCmd"
     5  	"github.com/bronze1man/kmg/kmgConfig"
     6  	"github.com/bronze1man/kmg/kmgFile"
     7  	"github.com/bronze1man/kmg/kmgTest"
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    13  func TestGoRunFile(ot *testing.T) {
    14  	wd, err := os.Getwd()
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	defer os.Chdir(wd)
    19  	root := kmgConfig.DefaultEnv().ProjectPath
    20  	os.Chdir(root)
    21  	kmgPath := filepath.Join(root, "bin/kmg")
    22  	kmgFile.MustDelete(kmgPath)
    23  	//kmgCmd.MustRun("kmg go install github.com/bronze1man/kmg/kmg")
    24  	gopath := filepath.Join(wd, "testWorkspace")
    25  	os.Chdir(gopath)
    26  	kmgFile.MustDelete(filepath.Join(gopath, "bin"))
    27  	kmgFile.MustDelete(filepath.Join(gopath, "pkg"))
    28  	kmgFile.MustDelete(filepath.Join(gopath, "tmp"))
    29  	kmgFile.MustDelete(filepath.Join(gopath, "src", "kmgTestMain", "other.go"))
    30  	goRunInstall(gopath, "kmgTestMain")
    31  	output := kmgCmd.MustRunAndReturnOutput(filepath.Join(gopath, "bin", "kmgTestMain"))
    32  	kmgTest.Equal(string(output), "1\n")
    33  	kmgFile.MustWriteFile(filepath.Join(wd, "testWorkspace", "src", "kmgTestMain", "other.go"), []byte(`
    34  package main
    35  
    36  func init(){
    37  	a=2
    38  }
    39  `))
    40  
    41  	goRunInstall(gopath, "kmgTestMain")
    42  	output = kmgCmd.MustRunAndReturnOutput(filepath.Join(gopath, "bin", "kmgTestMain"))
    43  	kmgTest.Equal(string(output), "2\n")
    44  	kmgFile.MustDelete(filepath.Join(wd, "testWorkspace", "src", "kmgTestMain", "other.go"))
    45  
    46  	goRunInstall(gopath, "kmgTestMain")
    47  	output = kmgCmd.MustRunAndReturnOutput(filepath.Join(gopath, "bin", "kmgTestMain"))
    48  	kmgTest.Equal(string(output), "1\n")
    49  
    50  	goRunInstall(gopath, "kmgTestMain/l2Main")
    51  	output = kmgCmd.MustRunAndReturnOutput(filepath.Join(gopath, "bin", "l2Main"))
    52  	kmgTest.Equal(string(output), "l2Main\n")
    53  
    54  	kmgFile.MustWriteFile(filepath.Join(gopath, "src", "replaceBin", "replaceBin.go"), []byte(`
    55  package main
    56  
    57  import "fmt"
    58  
    59  func main(){
    60  	fmt.Println("replaceBin")
    61  }
    62  `))
    63  	goRunInstall(gopath, "replaceBin")
    64  	output = kmgCmd.MustRunAndReturnOutput(filepath.Join(gopath, "bin", "replaceBin"))
    65  	kmgTest.Equal(string(output), "replaceBin\n")
    66  
    67  	kmgFile.MustWriteFile(filepath.Join(gopath, "src", "replaceBin", "replaceBin.go"), []byte(`
    68  package replaceBin
    69  
    70  var A = "1"
    71  `))
    72  	goRunInstall(gopath, "replaceBin")
    73  	// 应该会说这个不是main的package
    74  }
    75  
    76  /*
    77  func TestGoRunPackageName(ot *testing.T){
    78  	wd:=kmgFile.MustGetWd()
    79  	projectPath:=filepath.Join(wd,"testWorkspace")
    80  	kmgFile.MustDelete(filepath.Join(wd,"testWrokspace","pkg"))
    81  	kmgFile.MustDelete(filepath.Join(wd,"testWorkspace","src","kmgTestMain","other.go"))
    82  	goRunPackageName(projectPath,"kmgTestMain")
    83  }
    84  */