Browse Source

remote usage of afero from main package to reduce binary output size

tags/v1.1.3
ojizero 5 months ago
parent
commit
77b9f33e58
3 changed files with 20 additions and 13 deletions
  1. 12
    11
      findup.go
  2. 7
    2
      findup_test.go
  3. 1
    0
      go.sum

+ 12
- 11
findup.go View File

@@ -1,16 +1,17 @@
package gofindup

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/spf13/afero"
)

var fs = afero.NewOsFs()
type readDir func(string) ([]os.FileInfo, error)

var defaultReadDir readDir = ioutil.ReadDir

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

if err != nil {
return false, err
@@ -25,9 +26,9 @@ func hasFile(name, dir string, fs afero.Fs) (bool, error) {
return false, nil
}

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

if err != nil {
return "", err
@@ -47,24 +48,24 @@ func findupFrom(name, dir string, fs afero.Fs) (string, error) {
}
}

func findup(name string, fs afero.Fs) (string, error) {
func findup(name string, readdir readDir) (string, error) {
cwd, err := os.Getwd()

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

return findupFrom(name, cwd, fs)
return findupFrom(name, cwd, readdir)
}

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

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

+ 7
- 2
findup_test.go View File

@@ -1,6 +1,7 @@
package gofindup

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

@@ -31,6 +32,10 @@ type findupFunc func(string, string, afero.Fs) (string, error)

var fakefs = afero.NewMemMapFs()

func fakeReadDir(name string) ([]os.FileInfo, error) {
return afero.ReadDir(fakefs, name)
}

func init() {
// Build fake file system

@@ -64,7 +69,7 @@ func TestFindIn(t *testing.T) {
}

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

if a.expect.found {
assert.True(t, found)
@@ -90,7 +95,7 @@ func TestFindUpFrom(t *testing.T) {
}

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

assert.Equal(t, found, a.expect.path)


+ 1
- 0
go.sum View File

@@ -11,6 +11,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

Loading…
Cancel
Save