Permalink
Browse files

Allow mode to be specified per-file

  • Loading branch information...
1 parent d038131 commit 3b8c44e3354b2ba87f91cadccdbb1ec0378ae6c5 @paulhammond committed Mar 1, 2014
Showing with 16 additions and 10 deletions.
  1. +5 −2 fakefs.go
  2. +11 −8 fakefs_test.go
View
7 fakefs.go
@@ -49,12 +49,14 @@ func FileSystem(files ...http.File) http.FileSystem {
// a test fake file with string contents.
func File(name, contents string, options ...interface{}) http.File {
b := []byte(contents)
- f := file{name: name, size: int64(len(b)), Reader: bytes.NewReader(b)}
+ f := file{name: name, mode: 0644, size: int64(len(b)), Reader: bytes.NewReader(b)}
for _, o := range options {
switch t := o.(type) {
case time.Time:
f.modTime = t
+ case os.FileMode:
+ f.mode = t
default:
panic(fmt.Sprintf("Unknown option type %T", t))
}
@@ -65,6 +67,7 @@ func File(name, contents string, options ...interface{}) http.File {
type file struct {
name string
size int64
+ mode os.FileMode
modTime time.Time
*bytes.Reader
}
@@ -102,7 +105,7 @@ func (f file) Size() int64 {
}
func (f file) Mode() os.FileMode {
- return 0644
+ return f.mode
}
func (f file) ModTime() time.Time {
View
19 fakefs_test.go
@@ -17,7 +17,7 @@ import (
var now = time.Now()
var testFS = FileSystem(
Dir("foo",
- File("bar", "BAR", now),
+ File("bar", "BAR", now, os.FileMode(0777)),
Dir("baz",
Dir("baz",
Dir("baz",
@@ -35,13 +35,14 @@ func TestFilesystem(t *testing.T) {
path string
contents string
modTime time.Time
+ mode os.FileMode
}{
- {path: "foo/bar", contents: "BAR", modTime: now},
- {path: "foo/baz/baz/baz/baz", contents: "BAZ"},
- {path: "hello", contents: "hello"},
- {path: "/hello", contents: "hello"},
- {path: "./hello", contents: "hello"},
- {path: ".//hello", contents: "hello"},
+ {path: "foo/bar", contents: "BAR", modTime: now, mode: 0777},
+ {path: "foo/baz/baz/baz/baz", contents: "BAZ", mode: 0644},
+ {path: "hello", contents: "hello", mode: 0644},
+ {path: "/hello", contents: "hello", mode: 0644},
+ {path: "./hello", contents: "hello", mode: 0644},
+ {path: ".//hello", contents: "hello", mode: 0644},
}
for _, test := range fileTests {
@@ -64,7 +65,9 @@ func TestFilesystem(t *testing.T) {
}
if mt := stat.ModTime(); mt != test.modTime {
t.Errorf("expected %s modtime to be %v, got %v", test.path, test.modTime, mt)
-
+ }
+ if m := stat.Mode(); m != test.mode {
+ t.Errorf("expected %s modtime to be %v, got %v", test.path, test.mode, m)
}
}
}

0 comments on commit 3b8c44e

Please sign in to comment.