OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package local |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "fmt" |
| 10 "io" |
| 11 "io/ioutil" |
| 12 ) |
| 13 |
| 14 // NewTestFile returns File implementation (Symlink == false) backed by a fake |
| 15 // in-memory data. It is useful in unit tests. |
| 16 func NewTestFile(name string, data string, executable bool) File { |
| 17 return &testFile{ |
| 18 name: name, |
| 19 data: data, |
| 20 executable: executable, |
| 21 } |
| 22 } |
| 23 |
| 24 // NewTestSymlink returns File implementation (Symlink == true) backed by a fake |
| 25 // in-memory data. It is useful in unit tests. |
| 26 func NewTestSymlink(name string, target string) File { |
| 27 return &testFile{ |
| 28 name: name, |
| 29 symlinkTarget: target, |
| 30 } |
| 31 } |
| 32 |
| 33 type testFile struct { |
| 34 name string |
| 35 data string |
| 36 executable bool |
| 37 symlinkTarget string |
| 38 } |
| 39 |
| 40 func (f *testFile) Name() string { return f.name } |
| 41 func (f *testFile) Size() uint64 { return uint64(len(f.data)) } |
| 42 func (f *testFile) Executable() bool { return f.executable } |
| 43 func (f *testFile) Symlink() bool { return f.symlinkTarget != "" } |
| 44 |
| 45 func (f *testFile) SymlinkTarget() (string, error) { |
| 46 if f.symlinkTarget == "" { |
| 47 return "", fmt.Errorf("Not a symlink: %s", f.Name()) |
| 48 } |
| 49 return f.symlinkTarget, nil |
| 50 } |
| 51 |
| 52 func (f *testFile) Open() (io.ReadCloser, error) { |
| 53 if f.Symlink() { |
| 54 return nil, fmt.Errorf("Can't open symlink: %s", f.Name()) |
| 55 } |
| 56 r := bytes.NewReader([]byte(f.data)) |
| 57 return ioutil.NopCloser(r), nil |
| 58 } |
OLD | NEW |