Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto infer module when bud create outside $GOPATH #242

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions internal/cli/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/livebud/bud/internal/bail"
"github.com/livebud/bud/internal/cli/bud"
"github.com/livebud/bud/internal/format"
"github.com/livebud/bud/internal/scaffold"
"github.com/livebud/bud/internal/versions"
mod "github.com/livebud/bud/package/gomod"
Expand Down Expand Up @@ -111,14 +110,9 @@ func (c *Command) loadModule() *Module {
if module.Name == "" {
// Try inferring the module name from the directory
module.Name = mod.Infer(c.absDir)
// if can't infer then default it to `change.me`
if module.Name == "" {
// Fail that you need to pass in a module path
c.bail.Bail(format.Errorf(`
Unable to infer a module name. Try again using the module <path> name.

For example,
bud create --module=github.com/my/app %s
`, c.Dir))
module.Name = "change.me"
}
}
// Autoquote the module name since
Expand Down
21 changes: 18 additions & 3 deletions internal/cli/create/create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package create_test

import (
"bufio"
"context"
"os"
"path/filepath"
"testing"

"github.com/livebud/bud/internal/cli/testcli"
Expand All @@ -10,7 +13,15 @@ import (
"golang.org/x/mod/modfile"
)

func TestCreateOutsideGoPathError(t *testing.T) {
func fileFirstLine(filePath string) string {
file, _ := os.Open(filePath)
defer file.Close()
scanner := bufio.NewReader(file)
line, _ := scanner.ReadString('\n')
return line
}

func TestCreateOutsideGoPath(t *testing.T) {
is := is.New(t)
ctx := context.Background()
dir := t.TempDir()
Expand All @@ -20,10 +31,13 @@ func TestCreateOutsideGoPathError(t *testing.T) {
cli := testcli.New(dir)
is.NoErr(td.NotExists(".gitignore"))
result, err := cli.Run(ctx, "create", dir)
is.In(err.Error(), `Try again using the module <path> name.`)
is.Equal(result.Stdout(), "")
is.Equal(result.Stderr(), "")
is.NoErr(td.NotExists(".gitignore"))
is.Equal(fileFirstLine(filepath.Join(dir, "go.mod")), "module change.me\n")
is.NoErr(td.Exists(".gitignore"))
is.NoErr(td.Exists("go.sum"))
is.NoErr(td.Exists("package.json"))
is.NoErr(td.Exists("package-lock.json"))
}

func TestCreateOutsideGoPathModulePath(t *testing.T) {
Expand All @@ -37,6 +51,7 @@ func TestCreateOutsideGoPathModulePath(t *testing.T) {
is.NoErr(err)
is.Equal(result.Stdout(), "")
is.Equal(result.Stderr(), "")
is.Equal(fileFirstLine(filepath.Join(dir, "go.mod")), "module github.com/my/app\n")
is.NoErr(td.Exists(".gitignore"))
is.NoErr(td.Exists("go.sum"))
is.NoErr(td.Exists("package.json"))
Expand Down