Skip to content
Permalink
Browse files

Remove dep on afero

  • Loading branch information...
frioux committed Apr 6, 2019
1 parent 33fa3e0 commit 625ce00ff267cf0e6cbf5bb370a4a610f4fe5d3c
Showing with 20 additions and 17 deletions.
  1. +0 −1 go.mod
  2. +0 −2 go.sum
  3. +9 −8 internal/tool/undefer/undefer.go
  4. +11 −6 internal/tool/undefer/undefer_test.go
1 go.mod
@@ -17,7 +17,6 @@ require (
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/pierrec/lz4 v2.0.5+incompatible
github.com/pkg/errors v0.8.1
github.com/spf13/afero v1.2.0
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b
golang.org/x/net v0.0.0-20190125002852-4b62a64f59f7
2 go.sum
@@ -40,8 +40,6 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.2.0 h1:O9FblXGxoTc51M+cqr74Bm2Tmt4PvkA5iu/j8HrkNuY=
github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -11,13 +11,9 @@ import (
"time"

"github.com/pkg/errors"
"github.com/spf13/afero"
)

var (
fs = afero.NewOsFs()
dateRE = regexp.MustCompile(`^(\d{4})-(\d\d)-(\d\d)`)
)
var dateRE = regexp.MustCompile(`^(\d{4})-(\d\d)-(\d\d)`)

func newFiles(dir string, files []os.FileInfo, now time.Time) ([]string, error) {
ret := make([]string, 0, len(files))
@@ -52,7 +48,7 @@ func newFiles(dir string, files []os.FileInfo, now time.Time) ([]string, error)

func content(paths []string, stdout io.Writer) error {
for _, path := range paths {
file, err := fs.Open(path)
file, err := os.Open(path)
if err != nil {
return errors.Wrap(err, "couldn't Open")
}
@@ -64,7 +60,7 @@ func content(paths []string, stdout io.Writer) error {
if err != nil {
return errors.Wrap(err, "couldn't Close")
}
err = fs.Remove(path)
err = os.Remove(path)
if err != nil {
return errors.Wrap(err, "couldn't Remove")
}
@@ -81,7 +77,12 @@ func Run(args []string, _ io.Reader) error {
os.Exit(1)
}

files, err := afero.Afero{Fs: fs}.ReadDir(args[1])
dir, err := os.Open(args[1])
if err != nil {
return errors.Wrap(err, "Couldn't Open")
}

files, err := dir.Readdir(-1)
if err != nil {
return errors.Wrap(err, "Couldn't ReadDir")
}
@@ -2,11 +2,12 @@ package undefer

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
)

@@ -41,28 +42,32 @@ func TestNewFiles(t *testing.T) {
}

func TestContent(t *testing.T) {
fs = afero.NewMemMapFs()
d, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("Couldn't set up TempDir: %s", err)
}
defer os.RemoveAll(d)

f, err := fs.Create("x/y")
f, err := os.Create(filepath.Join(d, "y"))
if assert.NoError(t, err) {
_, err := f.WriteString("in output1\n")
assert.NoError(t, err)
}

f, err = fs.Create("x/z")
f, err = os.Create(filepath.Join(d, "z"))
if assert.NoError(t, err) {
_, err := f.WriteString("in output2\n")
assert.NoError(t, err)
}

f, err = fs.Create("x/t")
f, err = os.Create(filepath.Join(d, "t"))
if assert.NoError(t, err) {
_, err := f.WriteString("XXX\n")
assert.NoError(t, err)
}

w := &bytes.Buffer{}
err = content([]string{"x/y", "x/z"}, w)
err = content([]string{filepath.Join(d, "y"), filepath.Join(d, "z")}, w)
if assert.NoError(t, err) {
assert.Equal(t, "in output1\nin output2\n", string(w.Bytes()))
}

0 comments on commit 625ce00

Please sign in to comment.
You can’t perform that action at this time.