Browse Source

fixed API making it reasonable :D

tags/v1.0.0
ojizero 11 months ago
parent
commit
28b18e388a
2 changed files with 41 additions and 33 deletions
  1. 9
    9
      findup.go
  2. 32
    24
      findup_test.go

+ 9
- 9
findup.go View File

@@ -8,7 +8,7 @@ import (

var fs = afero.NewOsFs()

func findIn(name, dir string, fs afero.Fs) (bool, error) {
func hasFile(name, dir string, fs afero.Fs) (bool, error) {
files, err := afero.ReadDir(fs, dir)

if err != nil {
@@ -24,40 +24,40 @@ func findIn(name, dir string, fs afero.Fs) (bool, error) {
return false, nil
}

func findupFrom(name, dir string, fs afero.Fs) (bool, error) {
func findupFrom(name, dir string, fs afero.Fs) (string, error) {
for {
found, err := findIn(name, dir, fs)
found, err := hasFile(name, dir, fs)

if err != nil {
return false, err
return "", err
}

if found {
return true, nil
return filepath.Join(dir, name), nil
}

parent := filepath.Dir(dir)

if parent == dir {
return false, nil
return "", nil
}

dir = parent
}
}

func findup(name string, fs afero.Fs) (bool, error) {
func findup(name string, fs afero.Fs) (string, error) {
return findupFrom(name, ".", fs)
}

// Recursively find a file by walking up parents in the file tree
// starting from a specific directory.
func FindupFrom(name, dir string) (bool, error) {
func FindupFrom(name, dir string) (string, error) {
return findupFrom(name, dir, fs)
}

// Recursively find a file by walking up parents in the file tree
// starting from the current working directory.
func Findup(name string) (bool, error) {
func Findup(name string) (string, error) {
return findup(name, fs)
}

+ 32
- 24
findup_test.go View File

@@ -15,17 +15,19 @@ type given struct {

type expect struct {
found bool
path string
err string
}

type assertion struct {
given given
expect expect
type finderAssertion struct {
given
expect
}

type assertions []assertion
type finderAssertions []finderAssertion

type finderFunc func(string, string, afero.Fs) (bool, error)
type findupFunc func(string, string, afero.Fs) (string, error)

var fakefs = afero.NewMemMapFs()

@@ -53,9 +55,16 @@ func init() {
afero.WriteFile(fakefs, "r.txt", []byte("some mock file"), 0644)
}

func testAssertions(t *testing.T, as assertions, fn finderFunc) {
for _, a := range as {
found, err := fn(a.given.file, a.given.base, fakefs)
func TestFindIn(t *testing.T) {
assertions := finderAssertions{
{given{"ab", "/test/a"}, expect{found: true, err: ""}},
{given{"ac", "/test/a"}, expect{found: true, err: ""}},
{given{"ae", "/test/a"}, expect{found: false, err: ""}},
{given{"de", "/test/d"}, expect{found: false, err: "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
}

for _, a := range assertions {
found, err := hasFile(a.given.file, a.given.base, fakefs)

if a.expect.found {
assert.True(t, found)
@@ -71,25 +80,24 @@ func testAssertions(t *testing.T, as assertions, fn finderFunc) {
}
}

func TestFindIn(t *testing.T) {
assertions := assertions{
{given{"ab", "/test/a"}, expect{true, ""}},
{given{"ac", "/test/a"}, expect{true, ""}},
{given{"ae", "/test/a"}, expect{false, ""}},
{given{"de", "/test/d"}, expect{false, "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
func TestFindUpFrom(t *testing.T) {
assertions := finderAssertions{
{given{"ab", "/test/a"}, expect{path: filepath.Join("/", "test", "a", "ab"), err: ""}},
{given{"ab", "/test/a/ac"}, expect{path: filepath.Join("/", "test", "a", "ab"), err: ""}},
{given{"t.txt", "/test/a/ac"}, expect{path: filepath.Join("/", "test", "t.txt"), err: ""}},
{given{"r.txt", "/test/a/ac"}, expect{path: filepath.Join("/", "r.txt"), err: ""}},
{given{"r.txt", "/test/d"}, expect{path: "", err: "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
}

testAssertions(t, assertions, findIn)
}
for _, a := range assertions {
found, err := findupFrom(a.given.file, a.given.base, fakefs)

func TestFindUpFrom(t *testing.T) {
assertions := assertions{
{given{"ab", "/test/a"}, expect{true, ""}},
{given{"ab", "/test/a/ac"}, expect{true, ""}},
{given{"t.txt", "/test/a/ac"}, expect{true, ""}},
{given{"r.txt", "/test/a/ac"}, expect{true, ""}},
{given{"r.txt", "/test/d"}, expect{false, "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
}
assert.Equal(t, found, a.expect.path)

testAssertions(t, assertions, findupFrom)
if a.expect.err == "" {
assert.Nil(t, err)
} else {
assert.EqualError(t, err, a.expect.err)
}
}
}

Loading…
Cancel
Save