Debian Codegraph @

closure-library/closure/goog/testing/fs/directoryentry_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.fs.DirectoryEntryTest');
16 goog.setTestOnly('goog.testing.fs.DirectoryEntryTest');
17
18 goog.require('goog.array');
19 goog.require('goog.fs.DirectoryEntry');
20 goog.require('goog.fs.Error');
21 goog.require('goog.testing.MockClock');
22 goog.require('goog.testing.TestCase');
23 goog.require('goog.testing.fs.FileSystem');
24 goog.require('goog.testing.jsunit');
25
26 var Behavior = goog.fs.DirectoryEntry.Behavior;
27 var fs, dir, mockClock;
28
29 function setUpPage() {
30 // This test has a tendency to timeout on external Travis testing
31 // infrastructure. Up to 5s from 1s.
32 goog.testing.TestCase.getActiveTestCase().promiseTimeout = 5000;
33 }
34
35 function setUp() {
36 // Install the MockClock to create predictable timestamps for new files.
37 mockClock = new goog.testing.MockClock(true);
38 fs = new goog.testing.fs.FileSystem();
39 dir = fs.getRoot().createDirectorySync('foo');
40 dir.createDirectorySync('subdir').createFileSync('subfile');
41 dir.createFileSync('file');
42 mockClock.uninstall();
43 }
44
45 function testIsFile() {
46 assertFalse(dir.isFile());
47 }
48
49 function testIsDirectory() {
50 assertTrue(dir.isDirectory());
51 }
52
53 function testRemoveWithChildren() {
54 dir.getFileSync('bar', Behavior.CREATE);
55 return dir.remove().then(fail, function(e) {
56 assertEquals(goog.fs.Error.ErrorCode.INVALID_MODIFICATION, e.code);
57 });
58 }
59
60 function testRemoveWithoutChildren() {
61 var emptyDir = dir.getDirectorySync('empty', Behavior.CREATE);
62 return emptyDir.remove().then(function() {
63 assertTrue(emptyDir.deleted);
64 assertFalse(fs.getRoot().hasChild('empty'));
65 });
66 }
67
68 function testRemoveRootRecursively() {
69 var root = fs.getRoot();
70 return root.removeRecursively().then(function() {
71 assertTrue(dir.deleted);
72 assertFalse(fs.getRoot().deleted);
73 });
74 }
75
76 function testGetFile() {
77 return dir.getFile('file')
78 .then(function(file) {
79 assertEquals(dir.getFileSync('file'), file);
80 assertEquals('file', file.getName());
81 assertEquals('/foo/file', file.getFullPath());
82 assertTrue(file.isFile());
83
84 return dir.getLastModified();
85 })
86 .then(function(date) {
87 assertEquals(
88 'Reading a file should not update the modification date.', 0,
89 date.getTime());
90 return dir.getMetadata();
91 })
92 .then(function(metadata) {
93 assertEquals(
94 'Reading a file should not update the metadata.', 0,
95 metadata.modificationTime.getTime());
96 });
97 }
98
99 function testGetFileFromSubdir() {
100 return dir.getFile('subdir/subfile').then(function(file) {
101 assertEquals(dir.getDirectorySync('subdir').getFileSync('subfile'), file);
102 assertEquals('subfile', file.getName());
103 assertEquals('/foo/subdir/subfile', file.getFullPath());
104 assertTrue(file.isFile());
105 });
106 }
107
108 function testGetAbsolutePaths() {
109 return fs.getRoot()
110 .getFile('/foo/subdir/subfile')
111 .then(function(subfile) {
112 assertEquals('/foo/subdir/subfile', subfile.getFullPath());
113 return fs.getRoot().getDirectory('//foo////');
114 })
115 .then(function(foo) {
116 assertEquals('/foo', foo.getFullPath());
117 return foo.getDirectory('/');
118 })
119 .then(function(root) {
120 assertEquals('/', root.getFullPath());
121 return root.getDirectory('/////');
122 })
123 .then(function(root) { assertEquals('/', root.getFullPath()); });
124 }
125
126 function testCreateFile() {
127 // Advance the clock to an arbitrary, known time.
128 mockClock.install();
129 mockClock.tick(43);
130 var promise =
131 dir.getLastModified()
132 .then(function(date) { assertEquals(0, date.getTime()); })
133 .then(function() { return dir.getFile('bar', Behavior.CREATE); })
134 .then(function(file) {
135 mockClock.tick();
136 assertEquals('bar', file.getName());
137 assertEquals('/foo/bar', file.getFullPath());
138 assertEquals(dir, file.parent);
139 assertTrue(file.isFile());
140
141 return dir.getLastModified();
142 })
143 .then(function(date) {
144 assertEquals(43, date.getTime());
145 return dir.getMetadata();
146 })
147 .then(function(metadata) {
148 assertEquals(43, metadata.modificationTime.getTime());
149 })
150 .thenAlways(function() { mockClock.uninstall(); });
151 mockClock.tick();
152 return promise;
153 }
154
155 function testCreateFileThatAlreadyExists() {
156 mockClock.install();
157 mockClock.tick(47);
158 var existingFile = dir.getFileSync('file');
159 var promise = dir.getFile('file', Behavior.CREATE)
160 .then(function(file) {
161 assertEquals('file', file.getName());
162 assertEquals('/foo/file', file.getFullPath());
163 assertEquals(dir, file.parent);
164 assertEquals(existingFile, file);
165 assertTrue(file.isFile());
166
167 return dir.getLastModified();
168 })
169 .then(function(date) {
170 assertEquals(47, date.getTime());
171 return dir.getMetadata();
172 })
173 .then(function(metadata) {
174 assertEquals(47, metadata.modificationTime.getTime());
175 })
176 .thenAlways(function() { mockClock.uninstall(); });
177 mockClock.tick();
178 return promise;
179 }
180
181 function testCreateFileInSubdir() {
182 return dir.getFile('subdir/bar', Behavior.CREATE).then(function(file) {
183 assertEquals('bar', file.getName());
184 assertEquals('/foo/subdir/bar', file.getFullPath());
185 assertEquals(dir.getDirectorySync('subdir'), file.parent);
186 assertTrue(file.isFile());
187 });
188 }
189
190 function testCreateFileExclusive() {
191 return dir.getFile('bar', Behavior.CREATE_EXCLUSIVE).then(function(file) {
192 assertEquals('bar', file.getName());
193 assertEquals('/foo/bar', file.getFullPath());
194 assertEquals(dir, file.parent);
195 assertTrue(file.isFile());
196 });
197 }
198
199 function testGetNonExistentFile() {
200 return dir.getFile('bar').then(fail, function(e) {
201 assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
202 });
203 }
204
205 function testGetNonExistentFileInSubdir() {
206 return dir.getFile('subdir/bar').then(fail, function(e) {
207 assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
208 });
209 }
210
211 function testGetFileInNonExistentSubdir() {
212 return dir.getFile('bar/subfile').then(fail, function(e) {
213 assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
214 });
215 }
216
217 function testGetFileThatsActuallyADirectory() {
218 return dir.getFile('subdir').then(fail, function(e) {
219 assertEquals(goog.fs.Error.ErrorCode.TYPE_MISMATCH, e.code);
220 });
221 }
222
223 function testCreateFileInNonExistentSubdir() {
224 return dir.getFile('bar/newfile', Behavior.CREATE).then(fail, function(e) {
225 assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, e.code);
226 });
227 }
228
229 function testCreateFileThatsActuallyADirectory() {
230 return dir.getFile('subdir', Behavior.CREATE).then(fail, function(e) {
231 assertEquals(goog.fs.Error.ErrorCode.TYPE_MISMATCH, e.code);
232 });
233 }
234
235 function testCreateExclusiveExistingFile() {
236 return dir.getFile('file', Behavior.CREATE_EXCLUSIVE).then(fail, function(e) {
237 assertEquals(goog.fs.Error.ErrorCode.INVALID_MODIFICATION, e.code);
238 });
239 }
240
241 function testListEmptyDirectory() {
242 var emptyDir = fs.getRoot().getDirectorySync('empty', Behavior.CREATE);
243
244 return emptyDir.listDirectory().then(function(entryList) {
245 assertSameElements([], entryList);
246 });
247 }
248
249 function testListDirectory() {
250 var root = fs.getRoot();
251 root.getDirectorySync('dir1', Behavior.CREATE);
252 root.getDirectorySync('dir2', Behavior.CREATE);
253 root.getFileSync('file1', Behavior.CREATE);
254 root.getFileSync('file2', Behavior.CREATE);
255
256 return fs.getRoot().listDirectory().then(function(entryList) {
257 assertSameElements(
258 ['dir1', 'dir2', 'file1', 'file2', 'foo'],
259 goog.array.map(entryList, function(entry) { return entry.getName(); }));
260 });
261 }
262
263 function testCreatePath() {
264 return dir.createPath('baz/bat')
265 .then(function(batDir) {
266 assertEquals('/foo/baz/bat', batDir.getFullPath());
267 return batDir.createPath('../zazzle');
268 })
269 .then(function(zazzleDir) {
270 assertEquals('/foo/baz/zazzle', zazzleDir.getFullPath());
271 return zazzleDir.createPath('/elements/actinides/neptunium/');
272 })
273 .then(function(elDir) {
274 assertEquals('/elements/actinides/neptunium', elDir.getFullPath());
275 });
276 }