github.com/mlmmr/revel-cmd@v0.21.2-0.20191112133115-68d8795776dd/revel/command_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"github.com/mlmmr/revel-cmd/logger"
     5  	"github.com/mlmmr/revel-cmd/model"
     6  	"github.com/mlmmr/revel-cmd/utils"
     7  	"github.com/stretchr/testify/assert"
     8  	"go/build"
     9  	"os"
    10  	"path/filepath"
    11  )
    12  
    13  // Test that the event handler can be attached and it dispatches the event received
    14  func setup(suffix string, a *assert.Assertions) (string) {
    15  	temp := os.TempDir()
    16  	wd, _ := os.Getwd()
    17  	utils.InitLogger(wd, logger.LvlInfo)
    18  	gopath := filepath.Join(temp, "revel-test",suffix)
    19  	if utils.Exists(gopath) {
    20  		utils.Logger.Info("Removing test path", "path", gopath)
    21  		if err := os.RemoveAll(gopath); err != nil {
    22  			a.Fail("Failed to remove test path")
    23  		}
    24  	}
    25  	err := os.MkdirAll(gopath, os.ModePerm)
    26  	a.Nil(err, "Failed to create gopath "+gopath)
    27  
    28  	// So this is the issue, on the mac when folders are created in a temp folder they are returned like
    29  	// /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
    30  	// But if you change into that directory and read the current folder it is
    31  	// /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
    32  	// So to make this work on darwin this code was added
    33  	os.Chdir(gopath)
    34  	newwd, _ := os.Getwd()
    35  	gopath = newwd
    36  	defaultBuild := build.Default
    37  	defaultBuild.GOPATH = gopath
    38  	build.Default = defaultBuild
    39  	utils.Logger.Info("Setup stats", "original wd", wd, "new wd", newwd, "gopath",gopath, "gopath exists", utils.DirExists(gopath), "wd exists", utils.DirExists(newwd))
    40  
    41  	return gopath
    42  }
    43  
    44  // Create a new app for the name
    45  func newApp(name string, command model.COMMAND, precall func(c *model.CommandConfig), a *assert.Assertions) *model.CommandConfig {
    46  	c := &model.CommandConfig{}
    47  	switch command {
    48  	case model.NEW:
    49  		c.New.ImportPath = name
    50  	case model.BUILD:
    51  		c.Build.ImportPath = name
    52  	case model.TEST:
    53  		c.Test.ImportPath = name
    54  	case model.PACKAGE:
    55  		c.Package.ImportPath = name
    56  	case model.VERSION:
    57  		c.Version.ImportPath = name
    58  	case model.CLEAN:
    59  		c.Clean.ImportPath = name
    60  	default:
    61  		a.Fail("Unknown command ", command)
    62  	}
    63  
    64  	c.Index = command
    65  	if precall != nil {
    66  		precall(c)
    67  	}
    68  	if c.UpdateImportPath()!=nil {
    69  		a.Fail("Unable to update import path")
    70  	}
    71  	c.InitGoPaths()
    72  	c.InitPackageResolver()
    73  	return c
    74  }