Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/stub-modules/io.js

Issue 29408742: Issue 5059 - Simplify I/O API and FilterStorage implementation (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Created April 10, 2017, 2:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 "use strict"; 1 "use strict";
2 2
3 let data = {}; 3 let data = new Map();
4
5 //
6 // Fake nsIFile implementation for our I/O
7 //
8 function FakeFile(path)
9 {
10 this.path = path;
11 }
12 FakeFile.prototype =
13 {
14 get leafName()
15 {
16 return this.path;
17 },
18 set leafName(value)
19 {
20 this.path = value;
21 },
22 append(path)
23 {
24 this.path += path;
25 },
26 exists()
27 {
28 return this.path in data;
29 },
30 get contents()
31 {
32 return (data[this.path] || {}).contents;
33 },
34 set contents(value)
35 {
36 data[this.path] = {lastModified: Date.now()};
37 return data[this.path].contents = value;
38 },
39 get lastModifiedTime()
40 {
41 return (data[this.path] || {}).lastModified;
42 },
43 set lastModifiedTime(value)
44 {
45 return (data[this.path] || {}).lastModified = value;
46 },
47 clone()
48 {
49 return new FakeFile(this.path);
50 },
51 get parent()
52 {
53 return {create() {}};
54 },
55 normalize() {}
56 };
57 4
58 exports.IO = { 5 exports.IO = {
59 lineBreak: "\n", 6 // Non-public API, for tests only
60 resolveFilePath(path) 7 _getFileContents(fileName)
61 { 8 {
62 return new FakeFile(path); 9 if (data.has(fileName))
10 return data.get(fileName).contents;
11 return null;
63 }, 12 },
64 writeToFile(file, generator, callback) 13 _setFileContents(fileName, contents)
65 { 14 {
66 Promise.resolve().then(() => 15 data.set(fileName, {
16 lastModified: Date.now(),
17 contents
18 });
19 },
20 _getModifiedTime(fileName)
21 {
22 if (data.has(fileName))
23 return data.get(fileName).lastModified;
24 return 0;
25 },
26 _setModifiedTime(fileName, lastModified)
27 {
28 if (data.has(fileName))
29 data.get(fileName).lastModified = lastModified;
30 },
31
32 // Public API
33 writeToFile(fileName, generator)
34 {
35 return Promise.resolve().then(() =>
67 { 36 {
68 let lines = []; 37 data.set(fileName, {
69 for (let line of generator) 38 lastModified: Date.now(),
70 lines.push(line); 39 contents: Array.from(generator)
71 file.contents = lines.join("\n") + "\n"; 40 });
72 }).then(() => callback(null)).catch(e => callback(e)); 41 });
73 }, 42 },
74 readFromFile(file, listener, callback) 43 readFromFile(fileName, listener)
75 { 44 {
76 Promise.resolve().then(() => 45 return Promise.resolve().then(() =>
77 { 46 {
78 if (!data.hasOwnProperty(file.path)) 47 if (!data.has(fileName))
79 throw new Error("File doesn't exist"); 48 throw new Error("File doesn't exist");
80 49
81 let lines = file.contents.split("\n"); 50 let lines = data.get(fileName).contents;
82 if (lines.length && lines[lines.length - 1] == "")
83 lines.pop();
84 for (let line of lines) 51 for (let line of lines)
85 listener.process(line); 52 listener(line);
86 listener.process(null); 53 });
87 }).then(() => callback(null)).catch(e => callback(e));
88 }, 54 },
89 copyFile(from, to, callback) 55 copyFile(fromName, toName)
90 { 56 {
91 Promise.resolve().then(() => 57 return Promise.resolve().then(() =>
92 { 58 {
93 if (!data.hasOwnProperty(from.path)) 59 if (!data.has(fromName))
94 throw new Error("File doesn't exist"); 60 throw new Error("File doesn't exist");
95 if (from.path == to.path) 61 if (fromName == toName)
96 throw new Error("Cannot copy file to itself"); 62 throw new Error("Cannot copy file to itself");
97 63
98 to.contents = from.contents; 64 data.set(toName, data.get(fromName));
99 }).then(() => callback(null)).catch(e => callback(e)); 65 });
100 }, 66 },
101 renameFile(from, newName, callback) 67 renameFile(fromName, toName)
102 { 68 {
103 Promise.resolve().then(() => 69 return this.copyFile(fromName, toName).then(() => this.removeFile(fromName)) ;
70 },
71 removeFile(fileName)
72 {
73 return Promise.resolve().then(() =>
104 { 74 {
105 if (!data.hasOwnProperty(from.path)) 75 if (!data.has(fileName))
106 throw new Error("File doesn't exist");
107 if (from.path == newName)
108 throw new Error("Cannot move file to itself");
109
110 data[newName] = data[from.path];
111 delete data[from.path];
112 }).then(() => callback(null)).catch(e => callback(e));
113 },
114 removeFile(file, callback)
115 {
116 Promise.resolve().then(() =>
117 {
118 if (!data.hasOwnProperty(file.path))
119 throw new Error("File doesn't exist"); 76 throw new Error("File doesn't exist");
120 77
121 delete data[file.path]; 78 data.delete(fileName);
122 }).then(() => callback(null)).catch(e => callback(e)); 79 });
123 }, 80 },
124 statFile(file, callback) 81 statFile(fileName)
125 { 82 {
126 Promise.resolve().then(() => 83 return Promise.resolve().then(() =>
127 { 84 {
128 if (file.exists()) 85 if (data.has(fileName))
129 { 86 {
130 return { 87 return {
131 exists: true, 88 exists: true,
132 isDirectory: false, 89 lastModified: data.get(fileName).lastModified
133 isFile: true,
134 lastModified: file.lastModifiedTime
135 }; 90 };
136 } 91 }
137 return { 92 return {
138 exists: false, 93 exists: false,
139 isDirectory: false,
140 isFile: false,
141 lastModified: 0 94 lastModified: 0
142 }; 95 };
143 }).then(result => callback(null, result)).catch(e => callback(e)); 96 });
144 } 97 }
145 }; 98 };
OLDNEW
« test/filterStorage_readwrite.js ('K') | « test/filterStorage_readwrite.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld