github.com/yuki2006/cmd@v0.12.1-0.20240406021944-1e791c677ee9/revel/command_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/yuki2006/cmd/logger"
    12  	"github.com/yuki2006/cmd/model"
    13  	"github.com/yuki2006/cmd/utils"
    14  )
    15  
    16  // Test that the event handler can be attached and it dispatches the event received.
    17  func setup(suffix string, a *assert.Assertions) string {
    18  	temp := os.TempDir()
    19  	wd, _ := os.Getwd()
    20  	utils.InitLogger(wd, logger.LvlInfo)
    21  	gopath := filepath.Join(temp, "revel-test", suffix)
    22  	if utils.Exists(gopath) {
    23  		utils.Logger.Info("Removing test path", "path", gopath)
    24  		if err := os.RemoveAll(gopath); err != nil {
    25  			a.Fail("Failed to remove test path")
    26  		}
    27  	}
    28  	err := os.MkdirAll(gopath, os.ModePerm)
    29  	a.Nil(err, "Failed to create gopath "+gopath)
    30  
    31  	// So this is the issue, on the mac when folders are created in a temp folder they are returned like
    32  	// /var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
    33  	// But if you change into that directory and read the current folder it is
    34  	// /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
    35  	// So to make this work on darwin this code was added
    36  	if err := os.Chdir(gopath); err != nil {
    37  		panic(err)
    38  	}
    39  
    40  	newwd, _ := os.Getwd()
    41  	gopath = newwd
    42  	defaultBuild := build.Default
    43  	defaultBuild.GOPATH = gopath
    44  	build.Default = defaultBuild
    45  	utils.Logger.Info("Setup stats", "original wd", wd, "new wd", newwd, "gopath", gopath, "gopath exists", utils.DirExists(gopath), "wd exists", utils.DirExists(newwd))
    46  
    47  	return gopath
    48  }
    49  
    50  // Create a new app for the name.
    51  func newApp(name string, command model.COMMAND, precall func(c *model.CommandConfig), a *assert.Assertions) *model.CommandConfig {
    52  	c := &model.CommandConfig{Vendored: true}
    53  	switch command {
    54  	case model.NEW:
    55  		c.New.ImportPath = name
    56  		c.New.Callback = func() error {
    57  			// On callback we will invoke a specific branch of revel so that it works
    58  
    59  			goModCmd := exec.Command("go", "mod", "tidy")
    60  			utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
    61  			getOutput, _ := goModCmd.CombinedOutput()
    62  			fmt.Printf("Calling go mod tidy %s", string(getOutput))
    63  
    64  			goModCmd = exec.Command("go", "mod", "edit", "-replace=github.com/revel/revel=github.com/revel/revel@develop")
    65  			utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
    66  			getOutput, _ = goModCmd.CombinedOutput()
    67  			fmt.Printf("Calling go mod edit %v", string(getOutput))
    68  
    69  			return nil
    70  		}
    71  	case model.BUILD:
    72  		c.Build.ImportPath = name
    73  	case model.TEST:
    74  		c.Test.ImportPath = name
    75  	case model.PACKAGE:
    76  		c.Package.ImportPath = name
    77  	case model.VERSION:
    78  		c.Version.ImportPath = name
    79  	case model.CLEAN:
    80  		c.Clean.ImportPath = name
    81  	default:
    82  		a.Fail("Unknown command ", command)
    83  	}
    84  
    85  	c.Index = command
    86  	if precall != nil {
    87  		precall(c)
    88  	}
    89  	if c.UpdateImportPath() != nil {
    90  		a.Fail("Unable to update import path")
    91  	}
    92  
    93  	c.InitPackageResolver()
    94  	return c
    95  }