Skip to content

Commit

Permalink
Merge pull request #177 from icholy/master
Browse files Browse the repository at this point in the history
Add WithReaderContent PathOp
  • Loading branch information
dnephin authored Dec 21, 2019
2 parents 3dcccba + 36a3c7b commit 68a1b2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions fs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ func WithBytes(raw []byte) PathOp {
}
}

// WithReaderContent copies the reader contents to the file at Path
func WithReaderContent(r io.Reader) PathOp {
return func(path Path) error {
if m, ok := path.(manifestFile); ok {
m.SetContent(ioutil.NopCloser(r))
return nil
}
f, err := os.OpenFile(path.Path(), os.O_WRONLY, defaultFileMode)
if err != nil {
return err
}
_, err = io.Copy(f, r)
f.Close()
return err
}
}

// AsUser changes ownership of the file system object at Path
func AsUser(uid, gid int) PathOp {
return func(path Path) error {
Expand Down
12 changes: 12 additions & 0 deletions fs/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -86,3 +87,14 @@ func TestApply(t *testing.T) {
assert.Assert(t, fs.Equal(tmpDir.Path(), expected))
})
}

func TestWithReaderContent(t *testing.T) {
content := "this is a test"
dir := fs.NewDir(t, t.Name(),
fs.WithFile("1", "",
fs.WithReaderContent(strings.NewReader(content))),
)
defer dir.Remove()
expected := fs.Expected(t, fs.WithFile("1", content))
assert.Assert(t, fs.Equal(dir.Path(), expected))
}

0 comments on commit 68a1b2a

Please sign in to comment.