More linting

This commit is contained in:
Paul Tötterman
2021-02-10 16:34:20 +02:00
parent b562bd2dc5
commit ddec572d5d
29 changed files with 275 additions and 107 deletions

View File

@@ -19,7 +19,7 @@ func TestBuild(t *testing.T) {
t.Run("Build", func(t *testing.T) {
a := assert.New(t)
c := newApp("build-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c)
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.BUILD
c.Build.TargetPath = filepath.Join(gopath, "build-test", "target")
c.Build.ImportPath = c.ImportPath
@@ -34,7 +34,7 @@ func TestBuild(t *testing.T) {
"build-test-WithFlags/app.AppVersion=SomeValue",
"build-test-WithFlags/app.SomeOtherValue=SomeValue",
}
main.Commands[model.NEW].RunWith(c)
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.BUILD
c.Build.TargetPath = filepath.Join(gopath, "build-test", "target")
c.Build.ImportPath = c.ImportPath

View File

@@ -19,9 +19,12 @@ func TestClean(t *testing.T) {
t.Run("Clean", func(t *testing.T) {
a := assert.New(t)
c := newApp("clean-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c)
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.TEST
main.Commands[model.TEST].RunWith(c)
a.Nil(main.Commands[model.TEST].RunWith(c), "failed to run test")
a.True(utils.Exists(filepath.Join(gopath, "clean-test", "app", "tmp", "main.go")),
"Missing main from path "+filepath.Join(gopath, "clean-test", "app", "tmp", "main.go"))
c.Clean.ImportPath = c.ImportPath

View File

@@ -33,7 +33,10 @@ func setup(suffix string, a *assert.Assertions) string {
// But if you change into that directory and read the current folder it is
// /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/revel-test/revel-test-build
// So to make this work on darwin this code was added
os.Chdir(gopath)
if err := os.Chdir(gopath); err != nil {
panic(err)
}
newwd, _ := os.Getwd()
gopath = newwd
defaultBuild := build.Default

View File

@@ -18,6 +18,8 @@ import (
"github.com/revel/cmd/utils"
)
const ErrNoSkeleton Error = "failed to find skeleton in filepath"
var cmdNew = &Command{
UsageLine: "new -i [path] -s [skeleton]",
Short: "create a skeleton Revel application",
@@ -92,7 +94,9 @@ func newApp(c *model.CommandConfig) (err error) {
// This kicked off the download of the revel app, not needed for vendor
if !c.Vendored {
// At this point the versions can be set
c.SetVersions()
if err = c.SetVersions(); err != nil {
return
}
}
// copy files to new app directory
@@ -114,11 +118,18 @@ func newApp(c *model.CommandConfig) (err error) {
// Need to prep the run command
c.Run.ImportPath = c.ImportPath
updateRunConfig(c, nil)
c.UpdateImportPath()
runApp(c)
if err = c.UpdateImportPath(); err != nil {
return
}
if err = runApp(c); err != nil {
return
}
} else {
fmt.Fprintln(os.Stdout, "\nYou can run it with:\n revel run -a ", c.ImportPath)
}
return
}
@@ -129,13 +140,16 @@ func createModVendor(c *model.CommandConfig) (err error) {
utils.CmdInit(goModCmd, !c.Vendored, c.AppPath)
utils.Logger.Info("Exec:", "args", goModCmd.Args, "env", goModCmd.Env, "workingdir", goModCmd.Dir)
getOutput, err := goModCmd.CombinedOutput()
if c.New.Callback != nil {
err = c.New.Callback()
}
if err != nil {
return utils.NewBuildIfError(err, string(getOutput))
}
return
}
@@ -206,7 +220,7 @@ func setSkeletonPath(c *model.CommandConfig) (err error) {
c.New.SkeletonPath = fullpath
utils.Logger.Info("Set skeleton path to ", fullpath)
if !utils.DirExists(fullpath) {
return fmt.Errorf("failed to find skeleton in filepath %s %s", fullpath, sp.String())
return fmt.Errorf("%w %s %s", ErrNoSkeleton, fullpath, sp.String())
}
case "git":
fallthrough

View File

@@ -17,7 +17,7 @@ func TestPackage(t *testing.T) {
t.Run("Package", func(t *testing.T) {
a := assert.New(t)
c := newApp("package-test", model.NEW, nil, a)
main.Commands[model.NEW].RunWith(c)
a.Nil(main.Commands[model.NEW].RunWith(c), "failed to run new")
c.Index = model.PACKAGE
c.Package.ImportPath = c.ImportPath
a.Nil(main.Commands[model.PACKAGE].RunWith(c), "Failed to run package-test")

View File

@@ -22,6 +22,16 @@ import (
"github.com/revel/cmd/utils"
)
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const ErrInvalidCommandLine Error = "invalid command line arguments"
const (
// RevelCmdImportPath Revel framework cmd tool import path.
RevelSkeletonsImportPath = "github.com/revel/skeletons"
@@ -138,7 +148,7 @@ func ParseArgs(c *model.CommandConfig, parser *flags.Parser, args []string) (err
if !Commands[c.Index].UpdateConfig(c, extraArgs) {
buffer := &bytes.Buffer{}
parser.WriteHelp(buffer)
err = fmt.Errorf("invalid command line arguments %v\n%s", extraArgs, buffer.String())
err = fmt.Errorf("%w %v\n%s", ErrInvalidCommandLine, extraArgs, buffer.String())
}
return

View File

@@ -82,7 +82,9 @@ func (v *VersionCommand) RunWith(c *model.CommandConfig) (err error) {
if e := cmd.Start(); e != nil {
fmt.Println("Go command error ", e)
} else {
cmd.Wait()
if err = cmd.Wait(); err != nil {
return
}
}
return
@@ -188,7 +190,9 @@ func (v *VersionCommand) versionFromBytes(sourceStream []byte) (version *model.V
r := spec.Values[0].(*ast.BasicLit)
switch spec.Names[0].Name {
case "Version":
version.ParseVersion(strings.ReplaceAll(r.Value, `"`, ""))
if err = version.ParseVersion(strings.ReplaceAll(r.Value, `"`, "")); err != nil {
return
}
case "BuildDate":
version.BuildDate = r.Value
case "MinimumGoVersion":
@@ -202,7 +206,12 @@ func (v *VersionCommand) versionFromBytes(sourceStream []byte) (version *model.V
// Fetch the local version of revel from the file system.
func (v *VersionCommand) updateLocalVersions() {
v.cmdVersion = &model.Version{}
v.cmdVersion.ParseVersion(cmd.Version)
if err := v.cmdVersion.ParseVersion(cmd.Version); err != nil {
utils.Logger.Warn("Error parsing version", "error", err, "version", cmd.Version)
return
}
v.cmdVersion.BuildDate = cmd.BuildDate
v.cmdVersion.MinGoVersion = cmd.MinimumGoVersion

View File

@@ -1,6 +1,7 @@
package main_test
import (
"errors"
"os"
"path/filepath"
"testing"
@@ -34,8 +35,9 @@ func TestVersion(t *testing.T) {
c.Version.ImportPath = c.ImportPath
a.Nil(main.Commands[model.VERSION].RunWith(c), "Failed to run version-test")
})
if !t.Failed() {
if err := os.RemoveAll(gopath); err != nil && err != os.ErrNotExist {
if err := os.RemoveAll(gopath); err != nil && !errors.Is(err, os.ErrNotExist) {
a.Fail("Failed to remove test path", err.Error())
}
}