diff --git a/here/info.go b/here/info.go index ab56e4d..37a301b 100644 --- a/here/info.go +++ b/here/info.go @@ -2,10 +2,8 @@ package here import ( "encoding/json" - "fmt" "os" "path/filepath" - "regexp" "runtime" "strings" @@ -96,58 +94,3 @@ func (i Info) String() string { s := string(b) return s } - -func (i Info) Parse(p string) (Path, error) { - p = strings.TrimSpace(p) - p = filepath.Clean(p) - p = strings.TrimPrefix(p, i.Dir) - - p = strings.Replace(p, "\\", "/", -1) - p = strings.TrimSpace(p) - - if len(p) == 0 || p == ":" { - return i.build("", "", "") - } - - res := pathrx.FindAllStringSubmatch(p, -1) - if len(res) == 0 { - return Path{}, fmt.Errorf("could not parse %q", p) - } - - matches := res[0] - - if len(matches) != 4 { - return Path{}, fmt.Errorf("could not parse %q", p) - } - - return i.build(p, matches[1], matches[3]) -} - -func (i Info) build(p, pkg, name string) (Path, error) { - pt := Path{ - Pkg: pkg, - Name: name, - } - - if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 { - pt.Name = pt.Pkg - pt.Pkg = i.ImportPath - } - - if len(pt.Name) == 0 { - pt.Name = "/" - } - - if pt.Pkg == pt.Name { - pt.Pkg = i.ImportPath - pt.Name = "/" - } - - if !strings.HasPrefix(pt.Name, "/") { - pt.Name = "/" + pt.Name - } - pt.Name = strings.TrimPrefix(pt.Name, i.Dir) - return pt, nil -} - -var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") diff --git a/here/parse.go b/here/parse.go new file mode 100644 index 0000000..d9dc305 --- /dev/null +++ b/here/parse.go @@ -0,0 +1,63 @@ +package here + +import ( + "fmt" + "path/filepath" + "regexp" + "strings" +) + +func (i Info) Parse(p string) (Path, error) { + p = strings.TrimSpace(p) + p = filepath.Clean(p) + p = strings.TrimPrefix(p, i.Dir) + + p = strings.Replace(p, "\\", "/", -1) + p = strings.TrimSpace(p) + + if len(p) == 0 || p == ":" || p == "." { + return i.build("", "", "") + } + + res := pathrx.FindAllStringSubmatch(p, -1) + if len(res) == 0 { + return Path{}, fmt.Errorf("could not parse %q", p) + } + + matches := res[0] + + if len(matches) != 4 { + return Path{}, fmt.Errorf("could not parse %q", p) + } + + return i.build(p, matches[1], matches[3]) +} + +func (i Info) build(p, pkg, name string) (Path, error) { + pt := Path{ + Pkg: pkg, + Name: name, + } + + if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 { + pt.Name = pt.Pkg + pt.Pkg = i.ImportPath + } + + if len(pt.Name) == 0 { + pt.Name = "/" + } + + if pt.Pkg == pt.Name { + pt.Pkg = i.ImportPath + pt.Name = "/" + } + + if !strings.HasPrefix(pt.Name, "/") { + pt.Name = "/" + pt.Name + } + pt.Name = strings.TrimPrefix(pt.Name, i.Dir) + return pt, nil +} + +var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") diff --git a/here/parse_test.go b/here/parse_test.go new file mode 100644 index 0000000..78423e4 --- /dev/null +++ b/here/parse_test.go @@ -0,0 +1,60 @@ +package here_test + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/markbates/pkger/here" + "github.com/markbates/pkger/pkging/pkgtest" + "github.com/stretchr/testify/require" +) + +func Test_Info_Parse(t *testing.T) { + const name = "/public/index.html" + + r := require.New(t) + + app, err := pkgtest.App() + r.NoError(err) + + ip := app.Info.ImportPath + ip2 := "another/app" + + table := []struct { + in string + exp here.Path + err bool + }{ + {in: name, exp: here.Path{Pkg: ip, Name: name}}, + {in: "", exp: here.Path{Pkg: ip, Name: "/"}}, + {in: "/", exp: here.Path{Pkg: ip, Name: "/"}}, + {in: filepath.Join(app.Info.Dir, name), exp: here.Path{Pkg: ip, Name: name}}, + {in: ":" + name, exp: here.Path{Pkg: ip, Name: name}}, + {in: ip + ":" + name, exp: here.Path{Pkg: ip, Name: name}}, + {in: ip, exp: here.Path{Pkg: ip, Name: "/"}}, + {in: ":", exp: here.Path{Pkg: ip, Name: "/"}}, + {in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}}, + {in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}}, + {in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}}, + } + + for _, tt := range table { + for _, in := range []string{tt.in, strings.ReplaceAll(tt.in, "/", "\\")} { + t.Run(in, func(st *testing.T) { + r := require.New(st) + + pt, err := app.Info.Parse(in) + + if tt.err { + r.Error(err) + return + } + r.NoError(err) + + r.Equal(tt.exp, pt) + + }) + } + } +} diff --git a/here/path.go b/here/path.go index 2b8b90c..0239f65 100644 --- a/here/path.go +++ b/here/path.go @@ -1,9 +1,7 @@ package here import ( - "encoding/json" "fmt" - "os" ) type Path struct { @@ -15,25 +13,8 @@ func (p Path) String() string { if p.Name == "" { p.Name = "/" } + if p.Pkg == "" { + return p.Name + } return fmt.Sprintf("%s:%s", p.Pkg, p.Name) } - -func (p Path) Format(st fmt.State, verb rune) { - switch verb { - case 'v': - if st.Flag('+') { - b, err := json.MarshalIndent(p, "", " ") - if err != nil { - fmt.Fprint(os.Stderr, err) - return - } - fmt.Fprint(st, string(b)) - return - } - fmt.Fprint(st, p.String()) - case 'q': - fmt.Fprintf(st, "%q", p.String()) - default: - fmt.Fprint(st, p.String()) - } -} diff --git a/pkging/pkgtest/http.go b/pkging/pkgtest/http.go index daadaed..b21e84a 100644 --- a/pkging/pkgtest/http.go +++ b/pkging/pkgtest/http.go @@ -96,7 +96,6 @@ func (s Suite) LoadFolder(pkg pkging.Pkger) error { } return nil }) - return nil } func (s Suite) Test_HTTP(t *testing.T) {