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

Delta Between Two Patch Sets: test/filterStorage_readwrite.js

Issue 29356024: Issue 4223 - Adapt filter storage read/write tests to work in adblockpluscore repository (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Created Oct. 5, 2016, 8:02 p.m.
Right Patch Set: Simplified loadFilters/saveFilters helpers a bit further Created Oct. 6, 2016, 7:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « README.md ('k') | test/stub-modules/io.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 29 matching lines...) Expand all
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 let fs = require("fs");
49 let path = require("path"); 49 let path = require("path");
50 let datapath = path.resolve(__dirname, "data", "patterns.ini"); 50 let datapath = path.resolve(__dirname, "data", "patterns.ini");
kzar 2016/10/06 05:13:38 Did you forget to add the data/patterns.ini file?
Wladimir Palant 2016/10/06 07:13:59 No, I copied the files in a separate commit - what
kzar 2016/10/06 08:06:47 Acknowledged.
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 });
60 60
61 function loadFilters(file) 61 function loadFilters(file)
62 { 62 {
63 return new Promise((resolve, reject) => 63 FilterStorage.loadFromDisk(file);
64 { 64 return FilterNotifier.once("load");
65 let listener = function(action) 65 }
kzar 2016/10/06 05:13:38 Seems like this logic is nearly identical in saveF
Wladimir Palant 2016/10/06 07:13:59 Reusing isn't quite trivial but with the new Filte
66 { 66
67 if (action == "load") 67 function saveFilters(file)
68 { 68 {
69 FilterNotifier.removeListener(listener); 69 FilterStorage.saveToDisk(file);
70 resolve(); 70 return FilterNotifier.once("save");
71 }
72 };
73 FilterNotifier.addListener(listener);
74
75 FilterStorage.loadFromDisk(file);
76 });
77 }
78
79 function saveFilters(file, callback)
80 {
81 return new Promise((resolve, reject) =>
82 {
83 let listener = function(action)
84 {
85 if (action == "save")
86 {
87 FilterNotifier.removeListener(listener);
88 resolve();
89 }
90 };
91 FilterNotifier.addListener(listener);
92
93 FilterStorage.saveToDisk(file);
94 });
95 } 71 }
96 72
97 function testReadWrite(test, withExternal) 73 function testReadWrite(test, withExternal)
98 { 74 {
99 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); 75 let tempFile = IO.resolveFilePath("temp_patterns1.ini");
100 let tempFile2 = IO.resolveFilePath("temp_patterns2.ini"); 76 let tempFile2 = IO.resolveFilePath("temp_patterns2.ini");
101 77
102 function canonize(data) 78 function canonize(data)
103 { 79 {
104 let curSection = null; 80 let curSection = null;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 tempFile.contents = data; 115 tempFile.contents = data;
140 return loadFilters(tempFile); 116 return loadFilters(tempFile);
141 }).then(() => 117 }).then(() =>
142 { 118 {
143 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion , "File format version"); 119 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion , "File format version");
144 120
145 if (withExternal) 121 if (withExternal)
146 { 122 {
147 let subscription = new ExternalSubscription("~external~external subscripti on ID", "External subscription"); 123 let subscription = new ExternalSubscription("~external~external subscripti on ID", "External subscription");
148 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; 124 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")];
149 FilterStorage.addSubscription(subscription); 125 FilterStorage.addSubscription(subscription);
Wladimir Palant 2016/10/05 20:10:22 This was calling our public API in the original te
kzar 2016/10/06 05:13:38 Acknowledged.
150 126
151 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio n => subscription instanceof ExternalSubscription); 127 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio n => subscription instanceof ExternalSubscription);
152 test.equal(externalSubscriptions.length, 1, "Number of external subscripti ons after updateExternalSubscription"); 128 test.equal(externalSubscriptions.length, 1, "Number of external subscripti ons after updateExternalSubscription");
153 129
154 test.equal(externalSubscriptions[0].url, "~external~external subscription ID", "ID of external subscription"); 130 test.equal(externalSubscriptions[0].url, "~external~external subscription ID", "ID of external subscription");
155 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters in external subscription"); 131 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters in external subscription");
156 } 132 }
157 133
158 return saveFilters(tempFile2); 134 return saveFilters(tempFile2);
159 }).then(() => testData).then(expected => 135 }).then(() => testData).then(expected =>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo"; 169 let data = "[Subscription]\nurl=" + url + "\n[Subscription filters]\nfoo";
194 let tempFile = IO.resolveFilePath("temp_patterns1.ini"); 170 let tempFile = IO.resolveFilePath("temp_patterns1.ini");
195 tempFile.contents = data; 171 tempFile.contents = data;
196 172
197 loadFilters(tempFile).then(() => 173 loadFilters(tempFile).then(() =>
198 { 174 {
199 test.equal(FilterStorage.subscriptions.length, 1, "Number of filter subscr iptions"); 175 test.equal(FilterStorage.subscriptions.length, 1, "Number of filter subscr iptions");
200 if (FilterStorage.subscriptions.length == 1) 176 if (FilterStorage.subscriptions.length == 1)
201 { 177 {
202 let subscription = FilterStorage.subscriptions[0]; 178 let subscription = FilterStorage.subscriptions[0];
203 test.equal(subscription.url, url, "Subscription ID"); 179 test.equal(subscription.url, url, "Subscription ID");
kzar 2016/10/06 05:13:37 Won't `url` always be "~eh~" by the time the test
Wladimir Palant 2016/10/06 07:13:59 Good question. I checked and this isn't the case -
kzar 2016/10/06 08:06:47 Ah of course.
204 test.equal(subscription.title, null, "Subscription title"); 180 test.equal(subscription.title, null, "Subscription title");
205 test.deepEqual(subscription.defaults, null, "Default types"); 181 test.deepEqual(subscription.defaults, null, "Default types");
Wladimir Palant 2016/10/05 20:10:22 I had to modify this, the fallback for legacy grou
kzar 2016/10/06 05:13:37 Acknowledged.
206 test.equal(subscription.filters.length, 1, "Number of subscription filte rs"); 182 test.equal(subscription.filters.length, 1, "Number of subscription filte rs");
207 if (subscription.filters.length == 1) 183 if (subscription.filters.length == 1)
208 test.equal(subscription.filters[0].text, "foo", "First filter"); 184 test.equal(subscription.filters[0].text, "foo", "First filter");
209 } 185 }
210 }).catch(unexpectedError.bind(test)).then(() => test.done()); 186 }).catch(unexpectedError.bind(test)).then(() => test.done());
211 }; 187 };
212 } 188 }
213 189
214 exports.testReadLegacyFilters = function(test) 190 exports.testReadLegacyFilters = function(test)
215 { 191 {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 backupFile.lastModifiedTime -= 25*60*60*1000; 282 backupFile.lastModifiedTime -= 25*60*60*1000;
307 oldModifiedTime = backupFile2.lastModifiedTime; 283 oldModifiedTime = backupFile2.lastModifiedTime;
308 return saveFilters(null); 284 return saveFilters(null);
309 }).then(() => 285 }).then(() =>
310 { 286 {
311 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup overwritten if first one is 25 hours old"); 287 test.notEqual(backupFile2.lastModifiedTime, oldModifiedTime, "Second backup overwritten if first one is 25 hours old");
312 288
313 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup s = 2"); 289 test.ok(!backupFile3.exists(), "Third backup not created with patternsbackup s = 2");
314 }).catch(unexpectedError.bind(test)).then(() => test.done()); 290 }).catch(unexpectedError.bind(test)).then(() => test.done());
315 }; 291 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld