Skip to content

Commit

Permalink
consolidate NewDir and NewDirPath
Browse files Browse the repository at this point in the history
  • Loading branch information
vito committed Apr 17, 2023
1 parent 1289144 commit 8e011c3
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 99 deletions.
12 changes: 6 additions & 6 deletions pkg/bass/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ func TestBinding(t *testing.T) {
},
{
Name: "dir match",
Bindable: bass.NewDir("foo"),
Value: bass.NewDir("foo"),
Bindable: bass.NewDirPath("foo"),
Value: bass.NewDirPath("foo"),
Bindings: bass.Bindings{},
},
{
Name: "dir mismatch",
Bindable: bass.NewDir("foo"),
Value: bass.NewDir("bar"),
Bindable: bass.NewDirPath("foo"),
Value: bass.NewDirPath("bar"),
Err: bass.BindMismatchError{
Need: bass.NewDir("foo"),
Have: bass.NewDir("bar"),
Need: bass.NewDirPath("foo"),
Have: bass.NewDirPath("bar"),
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/bass/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var encodable = []bass.Value{
bass.String("hello"),
),
}.Scope(),
bass.NewDir("directory-path"),
bass.NewDirPath("directory-path"),
bass.GlobDir(
"directory-path",
[]string{"*.bash"},
Expand Down
4 changes: 2 additions & 2 deletions pkg/bass/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestEnums(t *testing.T) {
Enum: &bass.FileOrDirPath{},
Valid: []bass.Value{
bass.FilePath{"file"},
bass.NewDir("dir"),
bass.NewDirPath("dir"),
},
Invalid: []bass.Value{
bass.CommandPath{"cmd"},
Expand All @@ -39,7 +39,7 @@ func TestEnums(t *testing.T) {
{
Enum: &bass.ThunkDir{},
Valid: []bass.Value{
bass.NewDir("dir"),
bass.NewDirPath("dir"),
bass.ThunkPath{
Thunk: bass.Thunk{
Args: []bass.Value{
Expand Down
18 changes: 0 additions & 18 deletions pkg/bass/filesystem_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bass

import (
"fmt"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -52,23 +51,6 @@ func ParseFileOrDirPath(arg string) FileOrDirPath {
return fod
}

func NewFilePath(p string) FilePath {
if p == "" {
panic("empty file path")
}

return FilePath{
Path: path.Clean(p),
}
}

func NewDirPath(p string) DirPath {
return DirPath{
// trim suffix left behind from Clean returning "/"
Path: strings.TrimSuffix(path.Clean(p), "/"),
}
}

func IsPathLike(arg string) bool {
return strings.HasPrefix(arg, "./") ||
strings.HasPrefix(arg, "/") ||
Expand Down
14 changes: 7 additions & 7 deletions pkg/bass/filesystem_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func TestParseFilesystemPath(t *testing.T) {
for _, test := range []test{
{
Arg: ".",
Path: bass.NewDir("."),
Path: bass.DirPath{Path: "."},
},
{
Arg: "/",
Path: bass.NewDir(""),
Path: bass.DirPath{Path: ""},
},
{
Arg: "./",
Path: bass.NewDir("."),
Path: bass.DirPath{Path: "."},
},
{
Arg: "./foo",
Expand All @@ -36,15 +36,15 @@ func TestParseFilesystemPath(t *testing.T) {
},
{
Arg: "./foo/",
Path: bass.NewDir("foo"),
Path: bass.DirPath{Path: "foo"},
},
{
Arg: "foo/",
Path: bass.NewDir("foo"),
Path: bass.DirPath{Path: "foo"},
},
{
Arg: "./foo/bar/",
Path: bass.NewDir("foo/bar"),
Path: bass.DirPath{Path: "foo/bar"},
},
{
Arg: "foo/bar",
Expand All @@ -64,7 +64,7 @@ func TestFileOrDirPathFilesystemPath(t *testing.T) {
is := is.New(t)

is.Equal(
bass.NewDir("dir"),
bass.NewDirPath("dir"),
bass.FileOrDirPath{
Dir: &bass.DirPath{Path: "dir"},
}.FilesystemPath(),
Expand Down
12 changes: 6 additions & 6 deletions pkg/bass/ground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestGroundPrimitivePredicates(t *testing.T) {
bass.Symbol("sup"),
bass.CommandPath{"foo"},
bass.FilePath{"foo"},
bass.NewDir("foo"),
bass.NewDirPath("foo"),
},
Falses: []bass.Value{
bass.Keyword("sup"),
Expand All @@ -372,7 +372,7 @@ func TestGroundPrimitivePredicates(t *testing.T) {
bass.Symbol("sup"),
bass.CommandPath{"foo"},
bass.FilePath{"foo"},
bass.NewDir("foo"),
bass.NewDirPath("foo"),
},
Falses: []bass.Value{
bass.Keyword("sup"),
Expand All @@ -391,13 +391,13 @@ func TestGroundPrimitivePredicates(t *testing.T) {
bass.Symbol("sup"),
bass.CommandPath{"foo"},
bass.FilePath{"foo"},
bass.NewDir("foo"),
bass.NewDirPath("foo"),
},
},
{
Name: "path?",
Trues: []bass.Value{
bass.NewDir("foo"),
bass.NewDirPath("foo"),
bass.FilePath{"foo"},
bass.CommandPath{"foo"},
},
Expand Down Expand Up @@ -1675,7 +1675,7 @@ func TestGroundConversions(t *testing.T) {
{
Name: "string->fs-path",
Bass: `(string->fs-path "./dir/")`,
Result: bass.NewDir("dir"),
Result: bass.NewDirPath("dir"),
},
{
Name: "string->fs-path",
Expand Down Expand Up @@ -1789,7 +1789,7 @@ func TestGroundPaths(t *testing.T) {
{
Name: "subpath dir dir",
Bass: `(subpath ./dir/ ./sub/)`,
Result: bass.NewDir("dir/sub"),
Result: bass.NewDirPath("dir/sub"),
},
{
Name: "subpath thunk file",
Expand Down
21 changes: 17 additions & 4 deletions pkg/bass/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ type DirPath struct {
Exclude []string `json:"exclude,omitempty"`
}

func NewDir(path string) DirPath {
return DirPath{Path: path}
func NewDirPath(p string) DirPath {
return DirPath{
// trim suffix left behind from Clean returning "/"
Path: strings.TrimSuffix(path.Clean(p), "/"),
}
}

func GlobDir(path string, include, exclude []string) DirPath {
Expand Down Expand Up @@ -189,7 +192,7 @@ func (value DirPath) FromSlash() string {
}

func (value DirPath) Dir() DirPath {
return NewDir(path.Dir(value.Path))
return NewDirPath(path.Dir(value.Path))
}

func (value DirPath) IsDir() bool {
Expand All @@ -214,6 +217,16 @@ type FilePath struct {
Path string `json:"file"`
}

func NewFilePath(p string) FilePath {
if p == "" {
panic("empty file path")
}

return FilePath{
Path: path.Clean(p),
}
}

var _ Value = FilePath{}

func (value FilePath) String() string {
Expand Down Expand Up @@ -316,7 +329,7 @@ func (value FilePath) FromSlash() string {
}

func (value FilePath) Dir() DirPath {
return NewDir(path.Dir(value.Path))
return NewDirPath(path.Dir(value.Path))
}

func (value FilePath) IsDir() bool {
Expand Down
62 changes: 31 additions & 31 deletions pkg/bass/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,75 @@ func TestDirPathDecode(t *testing.T) {
is := is.New(t)

var foo bass.DirPath
err := bass.NewDir("foo").Decode(&foo)
err := bass.NewDirPath("foo").Decode(&foo)
is.NoErr(err)
is.Equal(foo, bass.NewDir("foo"))
is.Equal(foo, bass.NewDirPath("foo"))

err = bass.NewDir("bar").Decode(&foo)
err = bass.NewDirPath("bar").Decode(&foo)
is.NoErr(err)
is.Equal(foo, bass.NewDir("bar"))
is.Equal(foo, bass.NewDirPath("bar"))

var path_ bass.Path
err = bass.NewDir("bar").Decode(&path_)
err = bass.NewDirPath("bar").Decode(&path_)
is.NoErr(err)
is.Equal(path_, bass.NewDir("bar"))
is.Equal(path_, bass.NewDirPath("bar"))

var comb bass.Combiner
err = bass.NewDir("foo").Decode(&comb)
err = bass.NewDirPath("foo").Decode(&comb)
is.NoErr(err)
is.Equal(comb, bass.NewDir("foo"))
is.Equal(comb, bass.NewDirPath("foo"))

var app bass.Applicative
err = bass.NewDir("bar").Decode(&app)
err = bass.NewDirPath("bar").Decode(&app)
is.NoErr(err)
is.Equal(app, bass.NewDir("bar"))
is.Equal(app, bass.NewDirPath("bar"))
}

func TestDirPathEqual(t *testing.T) {
is := is.New(t)

Equal(t, bass.NewDir("hello"), bass.NewDir("hello"))
Equal(t, bass.NewDir(""), bass.NewDir(""))
is.True(!bass.NewDir("hello").Equal(bass.NewDir("")))
is.True(!bass.NewDir("hello").Equal(bass.FilePath{"hello"}))
is.True(!bass.NewDir("hello").Equal(bass.CommandPath{"hello"}))
Equal(t, bass.NewDir("hello"), wrappedValue{bass.NewDir("hello")})
is.True(!bass.NewDir("hello").Equal(wrappedValue{bass.NewDir("")}))
Equal(t, bass.NewDirPath("hello"), bass.NewDirPath("hello"))
Equal(t, bass.NewDirPath(""), bass.NewDirPath(""))
is.True(!bass.NewDirPath("hello").Equal(bass.NewDirPath("")))
is.True(!bass.NewDirPath("hello").Equal(bass.FilePath{"hello"}))
is.True(!bass.NewDirPath("hello").Equal(bass.CommandPath{"hello"}))
Equal(t, bass.NewDirPath("hello"), wrappedValue{bass.NewDirPath("hello")})
is.True(!bass.NewDirPath("hello").Equal(wrappedValue{bass.NewDirPath("")}))
}

func TestDirPathIsDir(t *testing.T) {
is := is.New(t)

is.True(bass.NewDir("hello").IsDir())
is.True(bass.NewDirPath("hello").IsDir())
}

func TestDirPathFromSlash(t *testing.T) {
is := is.New(t)

is.Equal(filepath.FromSlash("./hello/foo/bar/"), bass.NewDir("hello/foo/bar").FromSlash())
is.Equal(filepath.FromSlash("/hello/foo/bar/"), bass.NewDir("/hello/foo/bar").FromSlash())
is.Equal(filepath.FromSlash("./"), bass.NewDir(".").FromSlash())
is.Equal(filepath.FromSlash("./hello/foo/bar/"), bass.NewDir("./hello/foo/bar").FromSlash())
is.Equal(filepath.FromSlash("./hello/foo/bar/"), bass.NewDirPath("hello/foo/bar").FromSlash())
is.Equal(filepath.FromSlash("/hello/foo/bar/"), bass.NewDirPath("/hello/foo/bar").FromSlash())
is.Equal(filepath.FromSlash("./"), bass.NewDirPath(".").FromSlash())
is.Equal(filepath.FromSlash("./hello/foo/bar/"), bass.NewDirPath("./hello/foo/bar").FromSlash())
}

func TestDirPathName(t *testing.T) {
is := is.New(t)

is.Equal("hello", bass.NewDir("foo/hello").Name())
is.Equal("baz.buzz", bass.NewDir("foo/bar/baz.buzz").Name())
is.Equal("hello", bass.NewDirPath("foo/hello").Name())
is.Equal("baz.buzz", bass.NewDirPath("foo/bar/baz.buzz").Name())
}

func TestDirPathExtend(t *testing.T) {
is := is.New(t)

var parent, child bass.Path

parent = bass.NewDir("foo")
parent = bass.NewDirPath("foo")

child = bass.NewDir("bar")
child = bass.NewDirPath("bar")
sub, err := parent.Extend(child)
is.NoErr(err)
is.Equal(sub, bass.NewDir("foo/bar"))
is.Equal(sub, bass.NewDirPath("foo/bar"))

child = bass.FilePath{"bar"}
sub, err = parent.Extend(child)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestFilePathEqual(t *testing.T) {
Equal(t, bass.FilePath{"hello"}, bass.FilePath{"hello"})
Equal(t, bass.FilePath{""}, bass.FilePath{""})
is.True(!bass.FilePath{"hello"}.Equal(bass.FilePath{""}))
is.True(!bass.FilePath{"hello"}.Equal(bass.NewDir("hello")))
is.True(!bass.FilePath{"hello"}.Equal(bass.NewDirPath("hello")))
is.True(!bass.FilePath{"hello"}.Equal(bass.CommandPath{"hello"}))
Equal(t, bass.FilePath{"hello"}, wrappedValue{bass.FilePath{"hello"}})
is.True(!bass.FilePath{"hello"}.Equal(wrappedValue{bass.FilePath{""}}))
Expand All @@ -162,7 +162,7 @@ func TestFilePathExtend(t *testing.T) {

parent = bass.FilePath{"foo"}

child = bass.NewDir("bar")
child = bass.NewDirPath("bar")
_, err := parent.Extend(child)
is.True(err != nil)

Expand Down Expand Up @@ -209,7 +209,7 @@ func TestCommandPathEqual(t *testing.T) {
Equal(t, bass.CommandPath{"hello"}, bass.CommandPath{"hello"})
Equal(t, bass.CommandPath{""}, bass.CommandPath{""})
is.True(!bass.CommandPath{"hello"}.Equal(bass.CommandPath{""}))
is.True(!bass.CommandPath{"hello"}.Equal(bass.NewDir("hello")))
is.True(!bass.CommandPath{"hello"}.Equal(bass.NewDirPath("hello")))
is.True(!bass.CommandPath{"hello"}.Equal(bass.FilePath{"hello"}))
Equal(t, bass.CommandPath{"hello"}, wrappedValue{bass.CommandPath{"hello"}})
is.True(!bass.CommandPath{"hello"}.Equal(wrappedValue{bass.CommandPath{""}}))
Expand All @@ -228,7 +228,7 @@ func TestCommandPathExtend(t *testing.T) {

parent = bass.CommandPath{"foo"}

child = bass.NewDir("bar")
child = bass.NewDirPath("bar")
_, err := parent.Extend(child)
is.True(err != nil)

Expand Down
Loading

0 comments on commit 8e011c3

Please sign in to comment.