r104878 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104877‎ | r104878 | r104879 >
Date:17:51, 1 December 2011
Author:hashar
Status:deferred
Tags:
Comment:
--cache to save the test cases parsing

This is optional but speed up launchtime when other files are not
modified.
Modified paths:
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -20,6 +20,9 @@
2121 colors = require('colors'),
2222 HTML5 = require('html5').HTML5;
2323
 24+// Name of file used to cache the parser tests cases
 25+var cache_file = "parserTests.cache";
 26+
2427 // XXX: avoid a global here!
2528 global.PEG = require('pegjs');
2629
@@ -42,6 +45,11 @@
4346 boolean: true,
4447 default: true,
4548 },
 49+ 'cache': {
 50+ description: 'Get tests cases from cache file ' + cache_file,
 51+ boolean: true,
 52+ default: false,
 53+ },
4654 'filter': {
4755 description: 'Only run tests whose descriptions which match given regex',
4856 alias: 'regex',
@@ -96,11 +104,16 @@
97105
98106 // @fixme wrap more or this setup in a common module
99107
 108+// track files imported / required
 109+var fileDependencies = [];
 110+
100111 // Fetch up some of our wacky parser bits...
101112
102113 var basePath = path.join(path.dirname(path.dirname(process.cwd())), 'modules');
103114 function _require(filename) {
104 - return require(path.join(basePath, filename));
 115+ var fullpath = path.join( basePath, filename );
 116+ fileDependencies.push( fullpath );
 117+ return require( fullpath );
105118 }
106119
107120 function _import(filename, symbols) {
@@ -110,6 +123,7 @@
111124 });
112125 }
113126
 127+
114128 // needed for html5 parser adapter
115129 //var events = require('events');
116130
@@ -162,25 +176,88 @@
163177 console.log(e);
164178 }
165179
166 -var testFile;
167 -try {
168 - testFile = fs.readFileSync(testFileName, 'utf8');
169 -} catch (e) {
170 - // Try opening fallback file
171 - if( testFileName2 !== '' ) {
172 - try { testFile = fs.readFileSync( testFileName2, 'utf8' ); }
173 - catch(e) { console.log(e); }
 180+
 181+/**
 182+ * Get an object holding our tests cases. Eventually from a cache file
 183+ */
 184+function getTests() {
 185+
 186+ // Startup by loading .txt test file
 187+ var testFile;
 188+ try {
 189+ testFile = fs.readFileSync(testFileName, 'utf8');
 190+ fileDependencies.push( testFileName );
 191+ } catch (e) {
 192+ // Try opening fallback file
 193+ if( testFileName2 !== '' ) {
 194+ try {
 195+ testFile = fs.readFileSync( testFileName2, 'utf8' );
 196+ fileDependencies.push( testFileName2 );
 197+ }
 198+ catch(e) { console.log(e); }
 199+ }
174200 }
 201+ if( !argv.cache ) {
 202+ // Cache not wanted, parse file and return object
 203+ return parseTestCase( testFile );
 204+ }
 205+
 206+ // Find out modification time of all files depencies and then hashes those
 207+ // as a unique value using sha1.
 208+ var mtimes = '';
 209+ fileDependencies.sort().forEach( function (file) {
 210+ mtimes += fs.statSync( file )['mtime'];
 211+ });
 212+ var sha1 = require('crypto').createHash('sha1')
 213+ .update( mtimes ).digest( 'hex' );
 214+
 215+ // Look for a cache_file
 216+ var cache_content;
 217+ var cache_file_digest;
 218+ try {
 219+ console.log( "Looking for cache file " + cache_file );
 220+ cache_content = fs.readFileSync( cache_file, 'utf8' );
 221+ // Fetch previous digest
 222+ cache_file_digest = cache_content.match( /^CACHE: (\w+)\n/ )[1];
 223+ } catch(e) {
 224+ // cache file does not exist
 225+ }
 226+
 227+ if( cache_file_digest === sha1 ) {
 228+ // cache file match our digest.
 229+ console.log( "Loaded tests cases from cache file" );
 230+ // Return contained object after removing first line (CACHE: <sha1>)
 231+ return JSON.parse( cache_content.replace( /.*\n/, '' ) );
 232+ } else {
 233+ // Write new file cache, content preprended with current digest
 234+ console.log( "Cache file either inexistant or outdated" );
 235+ var parse = parseTestCase( testFile )
 236+ console.log( "Writing parse result to " +cache_file );
 237+ fs.writeFileSync( cache_file,
 238+ "CACHE: " + sha1 + "\n" + JSON.stringify( parse ),
 239+ 'utf8'
 240+ );
 241+
 242+ // We can now return the parsed object
 243+ return parse;
 244+ }
175245 }
176246
177 -console.log( "Parsing tests case from file, this takes a few seconds ..." );
178 -try {
179 - var cases = testParser.parse(testFile);
180 - console.log( "Done parsing." );
181 -} catch (e) {
182 - console.log(e);
 247+/**
 248+ * Parse given tests cases given as plaintext
 249+ */
 250+function parseTestCase( content ) {
 251+ console.log( "Parsing tests case from file, this takes a few seconds ..." );
 252+ try {
 253+ return testParser.parse(content);
 254+ console.log( "Done parsing." );
 255+ } catch (e) {
 256+ console.log(e);
 257+ }
183258 }
184259
 260+var cases = getTests();
 261+
185262 var articles = {};
186263
187264 function normalizeTitle(name) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r104964Ignore parserTests.cache...hashar12:43, 2 December 2011

Status & tagging log