Index: go/src/infra/tools/cipd/local/testing.go |
diff --git a/go/src/infra/tools/cipd/local/testing.go b/go/src/infra/tools/cipd/local/testing.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..58a53f86a0904c07c298bd0d85299e97006bbf93 |
--- /dev/null |
+++ b/go/src/infra/tools/cipd/local/testing.go |
@@ -0,0 +1,58 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package local |
+ |
+import ( |
+ "bytes" |
+ "fmt" |
+ "io" |
+ "io/ioutil" |
+) |
+ |
+// NewTestFile returns File implementation (Symlink == false) backed by a fake |
+// in-memory data. It is useful in unit tests. |
+func NewTestFile(name string, data string, executable bool) File { |
+ return &testFile{ |
+ name: name, |
+ data: data, |
+ executable: executable, |
+ } |
+} |
+ |
+// NewTestSymlink returns File implementation (Symlink == true) backed by a fake |
+// in-memory data. It is useful in unit tests. |
+func NewTestSymlink(name string, target string) File { |
+ return &testFile{ |
+ name: name, |
+ symlinkTarget: target, |
+ } |
+} |
+ |
+type testFile struct { |
+ name string |
+ data string |
+ executable bool |
+ symlinkTarget string |
+} |
+ |
+func (f *testFile) Name() string { return f.name } |
+func (f *testFile) Size() uint64 { return uint64(len(f.data)) } |
+func (f *testFile) Executable() bool { return f.executable } |
+func (f *testFile) Symlink() bool { return f.symlinkTarget != "" } |
+ |
+func (f *testFile) SymlinkTarget() (string, error) { |
+ if f.symlinkTarget == "" { |
+ return "", fmt.Errorf("Not a symlink: %s", f.Name()) |
+ } |
+ return f.symlinkTarget, nil |
+} |
+ |
+func (f *testFile) Open() (io.ReadCloser, error) { |
+ if f.Symlink() { |
+ return nil, fmt.Errorf("Can't open symlink: %s", f.Name()) |
+ } |
+ r := bytes.NewReader([]byte(f.data)) |
+ return ioutil.NopCloser(r), nil |
+} |