Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
improve error testing for non go directories.
Browse files Browse the repository at this point in the history
also cleans up there here.Info api
  • Loading branch information
markbates committed Nov 10, 2019
1 parent 2a9f964 commit 812232a
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 95 deletions.
3 changes: 2 additions & 1 deletion cmd/pkger/cmds/list.go
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/markbates/pkger"
"github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa"
Expand Down Expand Up @@ -39,7 +40,7 @@ func (e *listCmd) Exec(args []string) error {
return err
}

fp := info.FilePath(outName)
fp := filepath.Join(info.Dir, outName)
os.RemoveAll(fp)

decls, err := parser.Parse(info)
Expand Down
3 changes: 2 additions & 1 deletion cmd/pkger/cmds/pack.go
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

Expand All @@ -30,7 +31,7 @@ func (e *packCmd) Exec(args []string) error {
return err
}

fp := info.FilePath(outName)
fp := filepath.Join(info.Dir, outName)
os.RemoveAll(fp)

decls, err := parser.Parse(info)
Expand Down
94 changes: 55 additions & 39 deletions here/dir.go
Expand Up @@ -4,14 +4,13 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)

// Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) {
i, err := Cache(p, func(p string) (Info, error) {

var i Info

fi, err := os.Stat(p)
Expand All @@ -33,36 +32,11 @@ func Dir(p string) (Info, error) {
os.Chdir(p)

b, err := run("go", "list", "-json")
// go: cannot find main module; see 'go help modules'
// build .: cannot find module for path .
// no Go files in
if err != nil {

es := err.Error()
if strings.Contains(es, "cannot find module for path .") || strings.Contains(es, "no Go files") || strings.Contains(es, "can't load package") {
if _, err := os.Stat(fmt.Sprintf("%s/go.mod", p)); err == nil {
var mod Module
bm, err := run("go", "list", "-m", "-json")
if err != nil {
return i, err
}

if err := json.Unmarshal(bm, &mod); err != nil {
return i, err
}
info := NewInfoFromPath(p, mod)
prepareInfo(p, info, &i)

return i, err
}
} else {
return i, err
}

info, err := Dir(filepath.Dir(p))
if err != nil {
return info, err
}

prepareInfo(p, info, &i)
return i, err
return fromNonGoDir(p)
}

if err := json.Unmarshal(b, &i); err != nil {
Expand All @@ -76,20 +50,62 @@ func Dir(p string) (Info, error) {
return i, err
}

Cache(i.ImportPath, func(p string) (Info, error) {
return Cache(i.ImportPath, func(p string) (Info, error) {
return i, nil
})

}

func dir(p string) (Info, error) {
var i Info

fi, err := os.Stat(p)
if err != nil {
return i, err
}

if !fi.IsDir() {
p = filepath.Dir(p)
}

pwd, err := os.Getwd()
if err != nil {
return i, err
}

defer os.Chdir(pwd)

os.Chdir(p)

b, err := run("go", "list", "-json")
// go: cannot find main module; see 'go help modules'
// build .: cannot find module for path .
// no Go files in
if err != nil {
return fromNonGoDir(p)
}

if err := json.Unmarshal(b, &i); err != nil {
return i, err
}

return i, nil
}

func prepareInfo(p string, info Info, target *Info) {
target.Module = info.Module
ph := strings.TrimPrefix(p, target.Module.Dir)
func fromNonGoDir(dir string) (Info, error) {
fmt.Println(">>>TODO here/dir.go:59: dir ", dir)
i := Info{
Dir: dir,
}

target.ImportPath = path.Join(info.Module.Path, ph)
target.Name = path.Base(target.ImportPath)
b, err := run("go", "list", "-json", "-m")
if err != nil {
return i, err
}

if err := json.Unmarshal(b, &i.Module); err != nil {
return i, err
}

ph = filepath.Join(info.Module.Dir, ph)
target.Dir = ph
return i, err
}
30 changes: 29 additions & 1 deletion here/dir_test.go
@@ -1,8 +1,11 @@
package here_test

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

"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
Expand All @@ -13,18 +16,43 @@ func Test_Dir(t *testing.T) {
ref, err := pkgtest.NewRef()
r.NoError(err)

root := ref.Dir

r.NoError(err)
defer os.RemoveAll(root)

public := filepath.Join(root, "public")
r.NoError(os.MkdirAll(public, 0755))

gf := filepath.Join(root, "cmd", "main.go")
r.NoError(os.MkdirAll(filepath.Dir(gf), 0755))

f, err := os.Create(gf)
r.NoError(err)

_, err = f.Write([]byte("package main"))
r.NoError(err)

r.NoError(f.Close())

table := []struct {
in string
err bool
}{
{in: ref.Dir, err: false},
{in: root, err: false},
{in: public, err: false},
{in: gf, err: false},
{in: filepath.Join(root, "."), err: false},
{in: "/unknown", err: true},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)

_, err = here.Dir(tt.in)
if tt.err {
r.Error(err)
return
}
r.NoError(err)

Expand Down
9 changes: 9 additions & 0 deletions here/here.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os/exec"
"regexp"
"sync"
)

Expand Down Expand Up @@ -37,3 +38,11 @@ func Cache(p string, fn func(string) (Info, error)) (Info, error) {
cache.Store(p, i)
return i, nil
}

func ClearCache() {
cache = &infoMap{
data: &sync.Map{},
}
}

var nonGoDirRx = regexp.MustCompile(`cannot find main|go help modules|go: |build .:|no Go files`)
32 changes: 32 additions & 0 deletions here/here_test.go
@@ -0,0 +1,32 @@
package here

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_nonGoDirRx(t *testing.T) {
r := require.New(t)
r.False(nonGoDirRx.MatchString(""))
r.False(nonGoDirRx.MatchString("hello"))

table := []string{
"go: cannot find main module; see 'go help modules'",
"go help modules",
"go: ",
"build .:",
"no Go files",
}

for _, tt := range table {
t.Run(tt, func(st *testing.T) {
r := require.New(st)

b := nonGoDirRx.MatchString(tt)
r.True(b)

})
}

}
46 changes: 0 additions & 46 deletions here/info.go
Expand Up @@ -2,10 +2,6 @@ package here

import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
)

// Info represents details about the directory/package
Expand All @@ -16,15 +12,6 @@ type Info struct {
Module Module
}

// NewInfoFromPath initialize a Info with a basic information and his module
// this method could be used when the Unmarshal information is not possible
func NewInfoFromPath(path string, m Module) Info {
return Info{
Dir: path,
Module: m,
}
}

func (i Info) MarshalJSON() ([]byte, error) {
mm := map[string]interface{}{
"ImportPath": i.ImportPath,
Expand All @@ -36,45 +23,12 @@ func (i Info) MarshalJSON() ([]byte, error) {
return json.Marshal(mm)
}

func (i Info) FilePath(paths ...string) string {
res := []string{i.Dir}
for _, p := range paths {
p = strings.TrimPrefix(p, i.Dir)
p = strings.TrimPrefix(p, "/")
if runtime.GOOS == "windows" {
p = strings.Replace(p, "/", "\\", -1)
}
res = append(res, p)
}
return filepath.Join(res...)
}

func (i Info) Open(p string) (*os.File, error) {
return os.Open(i.FilePath(p))
}

// ModuleName returns the name of the current
// module, or if not using modules, the current
// package. These *might* not match.
func (i Info) ModuleName() string {
if i.Mods() {
return i.Module.Path
}
return i.ImportPath
}

// IsZero checks if the type has been filled
// with rich chocolately data goodness
func (i Info) IsZero() bool {
return i.String() == Info{}.String()
}

// Mods returns whether Go modules are used
// in this directory/package.
func (i Info) Mods() bool {
return !i.Module.IsZero()
}

func (i Info) String() string {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
Expand Down
11 changes: 4 additions & 7 deletions parser/parser_test.go
Expand Up @@ -50,21 +50,18 @@ func Test_Parser_Ref(t *testing.T) {
func Test_Parser_Example_HTTP(t *testing.T) {
r := require.New(t)

cur, err := here.Current()
here.ClearCache()
cur, err := here.Package("github.com/markbates/pkger")
r.NoError(err)

pwd, err := os.Getwd()
r.NoError(err)
defer os.Chdir(pwd)

root := filepath.Join(cur.Dir, "examples", "http", "pkger")
r.NoError(os.Chdir(root))

defer func() {
c := exec.Command("go", "mod", "tidy", "-v")
c.Run()
}()

her, err := here.Dir(".")
her, err := here.Dir(root)
r.NoError(err)

res, err := Parse(her)
Expand Down
1 change: 1 addition & 0 deletions pkging/pkgtest/ref.go
Expand Up @@ -17,6 +17,7 @@ type Ref struct {
}

func NewRef() (*Ref, error) {
here.ClearCache()
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
Expand Down

0 comments on commit 812232a

Please sign in to comment.