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

fill importpath from mod info #11

Merged
merged 1 commit into from
May 13, 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
4 changes: 4 additions & 0 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,9 @@ func fromNonGoDir(dir string) (Info, error) {
return i, fmt.Errorf("here.nonGoDir: %s: %w", dir, err)
}

if i.ImportPath == "" && i.Module.Path != "command-line-arguments" {
i.ImportPath = i.Module.Path
}

return i, nil
}
65 changes: 55 additions & 10 deletions dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,60 @@ func Test_Dir(t *testing.T) {
root, err := os.Getwd()
r.NoError(err)

h := New()

info, err := h.Dir(root)
info, err := Dir(root)
r.NoError(err)
sanityCheck(t, info)
}

func Test_Dir_NonGoDir(t *testing.T) {
r := require.New(t)

root, err := os.Getwd()
r.NoError(err)

empty := filepath.Join(root, "..", "empty")
os.MkdirAll(empty, 0755)
defer os.RemoveAll(empty)

info, err := Dir(empty)
r.NoError(err)
r.NotZero(info)

r.Equal(empty, info.Dir)
r.Equal("", info.Module.GoMod)
r.Equal("", info.ImportPath)
r.Equal("empty", info.Name)
r.Empty(info.GoFiles)
r.Empty(info.Imports)
}

func Test_Dir_Within(t *testing.T) {
r := require.New(t)

root, err := os.Getwd()
r.NoError(err)

cmd := filepath.Join(root, "cmd")
info, err = h.Dir(cmd)
info, err := Dir(cmd)
r.NoError(err)
r.NotZero(info)

r.Equal(cmd, info.Dir)
r.Equal(filepath.Join(root, "go.mod"), info.Module.GoMod)
r.Equal("", info.ImportPath)
r.Equal("github.com/gobuffalo/here", info.ImportPath)
r.Equal("cmd", info.Name)
r.Empty(info.GoFiles)
r.Empty(info.Imports)
}

func Test_Dir_CmdHere(t *testing.T) {
r := require.New(t)

root, err := os.Getwd()
r.NoError(err)

cmd = filepath.Join(cmd, "here")
info, err = h.Dir(cmd)
cmd := filepath.Join(root, "cmd", "here")
info, err := Dir(cmd)
r.NoError(err)
r.NotZero(info)

Expand All @@ -43,9 +77,16 @@ func Test_Dir(t *testing.T) {
r.Equal("main", info.Name)
r.NotEmpty(info.GoFiles)
r.NotEmpty(info.Imports)
}

func Test_Dir_File(t *testing.T) {
r := require.New(t)

cmd = filepath.Join(cmd, "main.go")
info, err = h.Dir(cmd)
root, err := os.Getwd()
r.NoError(err)

cmd := filepath.Join(root, "cmd", "here", "main.go")
info, err := Dir(cmd)
r.NoError(err)
r.NotZero(info)

Expand All @@ -55,8 +96,12 @@ func Test_Dir(t *testing.T) {
r.Equal("main", info.Name)
r.NotEmpty(info.GoFiles)
r.NotEmpty(info.Imports)
}

func Test_Dir_Unknown(t *testing.T) {
r := require.New(t)

info, err = h.Dir("/unknown")
info, err := Dir("/unknown")
r.Error(err)
r.Zero(info)
}