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

Side by Side Diff: test/filterStorage_readwrite.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Patch Set: Removed unused imports Created March 15, 2017, 3:11 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/filterStorage.js ('k') | test/matcher.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 let {createSandbox, unexpectedError} = require("./_common"); 20 const {createSandbox, unexpectedError} = require("./_common");
21 21
22 let Filter = null; 22 let Filter = null;
23 let FilterNotifier = null; 23 let FilterNotifier = null;
24 let FilterStorage = null; 24 let FilterStorage = null;
25 let IO = null; 25 let IO = null;
26 let Prefs = null; 26 let Prefs = null;
27 let Subscription = null; 27 let Subscription = null;
28 let ExternalSubscription = null; 28 let ExternalSubscription = null;
29 29
30 exports.setUp = function(callback) 30 exports.setUp = function(callback)
31 { 31 {
32 let sandboxedRequire = createSandbox(); 32 let sandboxedRequire = createSandbox();
33 ( 33 (
34 {Filter} = sandboxedRequire("../lib/filterClasses"), 34 {Filter} = sandboxedRequire("../lib/filterClasses"),
35 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"), 35 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"),
36 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), 36 {FilterStorage} = sandboxedRequire("../lib/filterStorage"),
37 {IO} = sandboxedRequire("./stub-modules/io"), 37 {IO} = sandboxedRequire("./stub-modules/io"),
38 {Prefs} = sandboxedRequire("./stub-modules/prefs"), 38 {Prefs} = sandboxedRequire("./stub-modules/prefs"),
39 {Subscription, ExternalSubscription} = sandboxedRequire("../lib/subscription Classes") 39 {Subscription, ExternalSubscription} = sandboxedRequire("../lib/subscription Classes")
40 ); 40 );
41 41
42 FilterStorage.addSubscription(Subscription.fromURL("~fl~")); 42 FilterStorage.addSubscription(Subscription.fromURL("~fl~"));
43 callback(); 43 callback();
44 } 44 };
45 45
46 let testData = new Promise((resolve, reject) => 46 let testData = new Promise((resolve, reject) =>
47 { 47 {
48 let fs = require("fs"); 48 const fs = require("fs");
49 let path = require("path"); 49 const path = require("path");
50 let datapath = path.resolve(__dirname, "data", "patterns.ini"); 50 let datapath = path.resolve(__dirname, "data", "patterns.ini");
51 51
52 fs.readFile(datapath, "utf-8", (error, data) => 52 fs.readFile(datapath, "utf-8", (error, data) =>
53 { 53 {
54 if (error) 54 if (error)
55 reject(error); 55 reject(error);
56 else 56 else
57 resolve(data); 57 resolve(data);
58 }); 58 });
59 }); 59 });
(...skipping 29 matching lines...) Expand all
89 curSection = {header: line, data: []}; 89 curSection = {header: line, data: []};
90 } 90 }
91 else if (curSection && /\S/.test(line)) 91 else if (curSection && /\S/.test(line))
92 curSection.data.push(line); 92 curSection.data.push(line);
93 } 93 }
94 for (let section of sections) 94 for (let section of sections)
95 { 95 {
96 section.key = section.header + " " + section.data[0]; 96 section.key = section.header + " " + section.data[0];
97 section.data.sort(); 97 section.data.sort();
98 } 98 }
99 sections.sort(function(a, b) 99 sections.sort((a, b) =>
100 { 100 {
101 if (a.key < b.key) 101 if (a.key < b.key)
102 return -1; 102 return -1;
103 else if (a.key > b.key) 103 else if (a.key > b.key)
104 return 1; 104 return 1;
105 else 105 return 0;
106 return 0;
107 }); 106 });
108 return sections.map(function(section) { 107 return sections.map(
109 return [section.header].concat(section.data).join("\n"); 108 section => [section.header].concat(section.data).join("\n")
110 }).join("\n"); 109 ).join("\n");
111 } 110 }
112 111
113 return testData.then(data => 112 return testData.then(data =>
114 { 113 {
115 tempFile.contents = data; 114 tempFile.contents = data;
116 return loadFilters(tempFile); 115 return loadFilters(tempFile);
117 }).then(() => 116 }).then(() =>
118 { 117 {
119 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion , "File format version"); 118 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion , "File format version");
120 119
121 if (withExternal) 120 if (withExternal)
122 { 121 {
123 let subscription = new ExternalSubscription("~external~external subscripti on ID", "External subscription"); 122 {
124 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; 123 let subscription = new ExternalSubscription("~external~external subscrip tion ID", "External subscription");
125 FilterStorage.addSubscription(subscription); 124 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")];
125 FilterStorage.addSubscription(subscription);
126 }
126 127
127 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio n => subscription instanceof ExternalSubscription); 128 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio n => subscription instanceof ExternalSubscription);
128 test.equal(externalSubscriptions.length, 1, "Number of external subscripti ons after updateExternalSubscription"); 129 test.equal(externalSubscriptions.length, 1, "Number of external subscripti ons after updateExternalSubscription");
129 130
130 test.equal(externalSubscriptions[0].url, "~external~external subscription ID", "ID of external subscription"); 131 test.equal(externalSubscriptions[0].url, "~external~external subscription ID", "ID of external subscription");
131 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters in external subscription"); 132 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters in external subscription");
132 } 133 }
133 134
134 return saveFilters(tempFile2); 135 return saveFilters(tempFile2);
135 }).then(() => testData).then(expected => 136 }).then(() => testData).then(expected =>
(...skipping 15 matching lines...) Expand all
151 exports.testLegacyGroups = {}; 152 exports.testLegacyGroups = {};
152 153
153 for (let url of ["~wl~", "~fl~", "~eh~"]) 154 for (let url of ["~wl~", "~fl~", "~eh~"])
154 { 155 {
155 exports.testLegacyGroups["read empty " + url] = function(test) 156 exports.testLegacyGroups["read empty " + url] = function(test)
156 { 157 {
157 let data = "[Subscription]\nurl=" + url; 158 let data = "[Subscription]\nurl=" + url;
158 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); 159 let tempFile = IO.resolveFilePath("temp_patterns1.ini");
159 tempFile.contents = data; 160 tempFile.contents = data;
160 161
161 loadFilters(tempFile, function() 162 loadFilters(tempFile, () =>
162 { 163 {
163 test.equal(FilterStorage.subscriptions.length, 0, "Number of filter subscr iptions"); 164 test.equal(FilterStorage.subscriptions.length, 0, "Number of filter subscr iptions");
164 }).catch(unexpectedError.bind(test)).then(() => test.done()); 165 }).catch(unexpectedError.bind(test)).then(() => test.done());
165 }; 166 };
166 167
167 exports.testLegacyGroups["read non-empty " + url] = function(test) 168 exports.testLegacyGroups["read non-empty " + url] = function(test)
168 { 169 {
169 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo"; 170 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo";
170 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); 171 let tempFile = IO.resolveFilePath("temp_patterns1.ini");
171 tempFile.contents = data; 172 tempFile.contents = data;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 { 257 {
257 test.ok(backupFile.exists(), "First backup created"); 258 test.ok(backupFile.exists(), "First backup created");
258 259
259 backupFile.lastModifiedTime -= 10000; 260 backupFile.lastModifiedTime -= 10000;
260 oldModifiedTime = backupFile.lastModifiedTime; 261 oldModifiedTime = backupFile.lastModifiedTime;
261 return saveFilters(null); 262 return saveFilters(null);
262 }).then(() => 263 }).then(() =>
263 { 264 {
264 test.equal(backupFile.lastModifiedTime, oldModifiedTime, "Backup not overwri tten if it is only 10 seconds old"); 265 test.equal(backupFile.lastModifiedTime, oldModifiedTime, "Backup not overwri tten if it is only 10 seconds old");
265 266
266 backupFile.lastModifiedTime -= 40*60*60*1000; 267 backupFile.lastModifiedTime -= 40 * 60 * 60 * 1000;
267 oldModifiedTime = backupFile.lastModifiedTime; 268 oldModifiedTime = backupFile.lastModifiedTime;
268 return saveFilters(null); 269 return saveFilters(null);
269 }).then(() => 270 }).then(() =>
270 { 271 {
271 test.notEqual(backupFile.lastModifiedTime, oldModifiedTime, "Backup overwrit ten if it is 40 hours old"); 272 test.notEqual(backupFile.lastModifiedTime, oldModifiedTime, "Backup overwrit ten if it is 40 hours old");
272 273
273 test.ok(backupFile2.exists(), "Second backup created when first backup is ov erwritten"); 274 test.ok(backupFile2.exists(), "Second backup created when first backup is ov erwritten");
274 275
275 backupFile.lastModifiedTime -= 20000; 276 backupFile.lastModifiedTime -= 20000;
276 oldModifiedTime = backupFile2.lastModifiedTime; 277 oldModifiedTime = backupFile2.lastModifiedTime;
277 return saveFilters(null); 278 return saveFilters(null);
278 }).then(() => 279 }).then(() =>
279 { 280 {
280 test.equal(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup not overwritten if first one is only 20 seconds old"); 281 test.equal(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup not overwritten if first one is only 20 seconds old");
281 282
282 backupFile.lastModifiedTime -= 25*60*60*1000; 283 backupFile.lastModifiedTime -= 25 * 60 * 60 * 1000;
283 oldModifiedTime = backupFile2.lastModifiedTime; 284 oldModifiedTime = backupFile2.lastModifiedTime;
284 return saveFilters(null); 285 return saveFilters(null);
285 }).then(() => 286 }).then(() =>
286 { 287 {
287 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup overwritten if first one is 25 hours old"); 288 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup overwritten if first one is 25 hours old");
288 289
289 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup s = 2"); 290 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup s = 2");
290 }).catch(unexpectedError.bind(test)).then(() => test.done()); 291 }).catch(unexpectedError.bind(test)).then(() => test.done());
291 }; 292 };
OLDNEW
« no previous file with comments | « test/filterStorage.js ('k') | test/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld