Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js |
— | — | @@ -20,6 +20,9 @@ |
21 | 21 | colors = require('colors'), |
22 | 22 | HTML5 = require('html5').HTML5; |
23 | 23 | |
| 24 | +// Name of file used to cache the parser tests cases |
| 25 | +var cache_file = "parserTests.cache"; |
| 26 | + |
24 | 27 | // XXX: avoid a global here! |
25 | 28 | global.PEG = require('pegjs'); |
26 | 29 | |
— | — | @@ -42,6 +45,11 @@ |
43 | 46 | boolean: true, |
44 | 47 | default: true, |
45 | 48 | }, |
| 49 | + 'cache': { |
| 50 | + description: 'Get tests cases from cache file ' + cache_file, |
| 51 | + boolean: true, |
| 52 | + default: false, |
| 53 | + }, |
46 | 54 | 'filter': { |
47 | 55 | description: 'Only run tests whose descriptions which match given regex', |
48 | 56 | alias: 'regex', |
— | — | @@ -96,11 +104,16 @@ |
97 | 105 | |
98 | 106 | // @fixme wrap more or this setup in a common module |
99 | 107 | |
| 108 | +// track files imported / required |
| 109 | +var fileDependencies = []; |
| 110 | + |
100 | 111 | // Fetch up some of our wacky parser bits... |
101 | 112 | |
102 | 113 | var basePath = path.join(path.dirname(path.dirname(process.cwd())), 'modules'); |
103 | 114 | 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 ); |
105 | 118 | } |
106 | 119 | |
107 | 120 | function _import(filename, symbols) { |
— | — | @@ -110,6 +123,7 @@ |
111 | 124 | }); |
112 | 125 | } |
113 | 126 | |
| 127 | + |
114 | 128 | // needed for html5 parser adapter |
115 | 129 | //var events = require('events'); |
116 | 130 | |
— | — | @@ -162,25 +176,88 @@ |
163 | 177 | console.log(e); |
164 | 178 | } |
165 | 179 | |
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 | + } |
174 | 200 | } |
| 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 | + } |
175 | 245 | } |
176 | 246 | |
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 | + } |
183 | 258 | } |
184 | 259 | |
| 260 | +var cases = getTests(); |
| 261 | + |
185 | 262 | var articles = {}; |
186 | 263 | |
187 | 264 | function normalizeTitle(name) { |