@@ -29,6 +29,252 @@ module.exports = function (grunt) {
29
29
if ( grunt . option ( 'complete' ) )
30
30
grunt . task . run ( 'compile-data' ) ;
31
31
} ) ;
32
+ grunt . registerTask ( 'update-tests' , 'refreshes the tests found in tests/test262' , updateTests ) ;
33
+
34
+
35
+ /**
36
+ * Refreshes the tests found in tests/test262.
37
+ */
38
+ function updateTests ( ) {
39
+ var gruntTaskDone = this . async ( ) ,
40
+ LIBS = {
41
+ async : require ( 'async' ) ,
42
+ fs : require ( 'fs' ) ,
43
+ http : require ( 'http' ) ,
44
+ path : require ( 'path' ) ,
45
+ vm : require ( 'vm' )
46
+ } ,
47
+ URL_BASE = 'http://hg.ecmascript.org' ,
48
+ testsURL ,
49
+ tempDir ,
50
+ testsTarball ,
51
+ testsDir ;
52
+
53
+ LIBS . async . series ( [
54
+ function ( asyncTaskDone ) {
55
+ var resErr ,
56
+ resBody = '' ;
57
+ grunt . log . writeln ( 'looking for tests tarball...' ) ;
58
+ /*
59
+ // DEBUGGING
60
+ testsURL = 'http://hg.ecmascript.org/tests/test262/archive/d067d2f0ca30.tar.gz';
61
+ grunt.log.ok('tests URL: ' + testsURL);
62
+ asyncTaskDone();
63
+ return;
64
+ */
65
+
66
+ LIBS . http . get ( URL_BASE + '/tests/test262/file/' , function ( res ) {
67
+ if ( 200 !== res . statusCode ) {
68
+ asyncTaskDone ( new Error ( 'failed to GET ' + URL_BASE + '/tests/test262/file/' ) ) ;
69
+ return ;
70
+ }
71
+ res . on ( 'data' , function ( data ) {
72
+ resBody += data . toString ( ) ;
73
+ } ) ;
74
+ res . on ( 'error' , function ( err ) {
75
+ resErr = err ;
76
+ } ) ;
77
+ res . on ( 'end' , function ( ) {
78
+ var matches ;
79
+ matches = resBody . match ( / < a h r e f = " ( \/ t e s t s \/ t e s t 2 6 2 \/ a r c h i v e \/ [ ^ . ] + .t a r .g z ) " > g z < \/ a > / ) ;
80
+ testsURL = matches [ 1 ] ;
81
+ if ( ! testsURL ) {
82
+ asyncTaskDone ( new Error ( 'failed to find tar.gz of tests' ) ) ;
83
+ return ;
84
+ }
85
+ if ( '/' === testsURL [ 0 ] ) {
86
+ testsURL = URL_BASE + testsURL ;
87
+ }
88
+ grunt . log . ok ( 'tests URL: ' + testsURL ) ;
89
+ asyncTaskDone ( resErr ) ;
90
+ } ) ;
91
+ } ) . end ( ) ;
92
+ } ,
93
+
94
+ function ( asyncTaskDone ) {
95
+ grunt . log . writeln ( 'making temporary directory...' ) ;
96
+ /*
97
+ // DEBUGGING
98
+ tempDir = '/tmp/grunt.o4xlDE3o';
99
+ grunt.log.ok('temporary directory: ' + tempDir);
100
+ asyncTaskDone();
101
+ return;
102
+ */
103
+ grunt . util . spawn ( {
104
+ cmd : 'mktemp' ,
105
+ args : [ '-d' , '/tmp/grunt.XXXXXXXX' ]
106
+ } , function ( err , results ) {
107
+ tempDir = results . stdout ;
108
+ grunt . log . ok ( 'temporary directory: ' + tempDir ) ;
109
+ asyncTaskDone ( err ) ;
110
+ } ) ;
111
+ } ,
112
+
113
+ function ( asyncTaskDone ) {
114
+ var resErr ,
115
+ resBody = new Buffer ( 0 ) ,
116
+ reportEveryBytes = 300000 ,
117
+ reportBytes = 0 ;
118
+ grunt . log . writeln ( 'downloading tests tarball...' ) ;
119
+ /*
120
+ // DEBUGGING
121
+ testsTarball = tempDir + '/d067d2f0ca30.tar.gz';
122
+ grunt.log.ok('tests tarball: ' + testsTarball);
123
+ asyncTaskDone();
124
+ return;
125
+ */
126
+ LIBS . http . get ( testsURL , function ( res ) {
127
+ if ( 200 !== res . statusCode ) {
128
+ asyncTaskDone ( new Error ( 'failed to GET ' + testsUR ) ) ;
129
+ return ;
130
+ }
131
+ res . on ( 'data' , function ( data ) {
132
+ // We need to use the Buffer class to safely handle binary
133
+ // data (octet streams). Alas, it's not resizable so we
134
+ // need to reallocate as we go along.
135
+ var newBuffer = Buffer ( resBody . length + data . length ) ;
136
+ resBody . copy ( newBuffer , 0 ) ;
137
+ data . copy ( newBuffer , resBody . length ) ;
138
+ resBody = newBuffer ;
139
+ reportBytes += data . length ;
140
+ if ( reportBytes >= reportEveryBytes ) {
141
+ grunt . log . ok ( 'got ' + resBody . length + ' bytes' ) ;
142
+ reportBytes = 0 ;
143
+ }
144
+ } ) ;
145
+ res . on ( 'error' , function ( err ) {
146
+ resErr = err ;
147
+ } ) ;
148
+ res . on ( 'end' , function ( ) {
149
+ testsTarball = LIBS . path . resolve ( tempDir , LIBS . path . basename ( testsURL ) ) ;
150
+ grunt . file . write ( testsTarball , resBody . toString ( 'binary' ) , { encoding : 'binary' } ) ;
151
+ grunt . log . ok ( 'tests tarball: ' + testsTarball ) ;
152
+ asyncTaskDone ( resErr ) ;
153
+ } ) ;
154
+ } ) . end ( ) ;
155
+ } ,
156
+
157
+ function ( asyncTaskDone ) {
158
+ grunt . log . writeln ( 'expanding tests tarball...' ) ;
159
+ /*
160
+ // DEBUGGING
161
+ testsDir = tempDir + '/test262-d067d2f0ca30';
162
+ grunt.log.ok('tests directory: ' + testsDir);
163
+ asyncTaskDone();
164
+ return;
165
+ */
166
+ grunt . util . spawn ( {
167
+ cmd : 'tar' ,
168
+ args : [ 'xfz' , LIBS . path . basename ( testsTarball ) ] ,
169
+ opts : {
170
+ cwd : tempDir
171
+ }
172
+ } , function ( err , results ) {
173
+ testsDir = LIBS . path . resolve ( tempDir , 'test262-' + LIBS . path . basename ( testsTarball ) . split ( '.' ) [ 0 ] ) ;
174
+ grunt . log . ok ( 'tests directory: ' + testsDir ) ;
175
+ asyncTaskDone ( err ) ;
176
+ } ) ;
177
+ } ,
178
+
179
+ function ( asyncTaskDone ) {
180
+ grunt . log . writeln ( 'clearing old tests/test262...' ) ;
181
+ var doomed = grunt . file . expand ( __dirname + '/tests/test262/*' ) ;
182
+ doomed . forEach ( function ( path ) {
183
+ grunt . file . delete ( path ) ;
184
+ } ) ;
185
+ grunt . log . ok ( 'done' ) ;
186
+ asyncTaskDone ( ) ;
187
+ } ,
188
+
189
+ function ( asyncTaskDone ) {
190
+ grunt . log . writeln ( 'copying from tarball to tests/test262...' ) ;
191
+ grunt . file . copy (
192
+ LIBS . path . resolve ( testsDir , 'LICENSE' ) ,
193
+ LIBS . path . resolve ( __dirname , 'tests/test262/LICENSE' )
194
+ ) ;
195
+
196
+ [ 'tools' , 'test' ] . forEach ( function ( dir ) {
197
+ grunt . log . ok ( dir ) ;
198
+ var files = grunt . file . expand (
199
+ LIBS . path . resolve ( testsDir , dir ) + '/**'
200
+ )
201
+ files . forEach ( function ( srcPath ) {
202
+ if ( ! grunt . file . isFile ( srcPath ) ) {
203
+ return ;
204
+ }
205
+ var destPath = srcPath . replace ( testsDir , LIBS . path . resolve ( __dirname , 'tests/test262' ) ) ;
206
+ grunt . file . copy ( srcPath , destPath ) ;
207
+ } ) ;
208
+ } ) ;
209
+ grunt . log . ok ( 'done' ) ;
210
+ asyncTaskDone ( ) ;
211
+ } ,
212
+
213
+ function ( asyncTaskDone ) {
214
+ grunt . log . writeln ( 'removing `Collator` tests...' ) ;
215
+
216
+ // fixup constructor lists
217
+ [ 'tests/test262/test/harness/testIntl.js' ,
218
+ 'tests/test262/test/suite/intl402/ch08/8.0/8.0_L15.js'
219
+ ] . forEach ( function ( path ) {
220
+ grunt . log . ok ( 'adjusting ' + path ) ;
221
+ var contents = grunt . file . read ( path ) ;
222
+ contents = contents . replace ( / ( \[ ) ( " C o l l a t o r " , ) / , '$1/*$2*/' ) ;
223
+ grunt . file . write ( path , contents ) ;
224
+ } ) ;
225
+
226
+ // these are just trouble
227
+ [ 'tests/test262/test/suite/intl402/ch09/9.2/9.2.5_11_g_ii_2.js' ,
228
+ 'tests/test262/test/suite/intl402/ch10' ,
229
+ 'tests/test262/test/suite/intl402/ch13/13.1'
230
+ ] . forEach ( function ( path ) {
231
+ grunt . log . ok ( 'removing ' + path ) ;
232
+ grunt . file . delete ( path ) ;
233
+ } ) ;
234
+
235
+ asyncTaskDone ( ) ;
236
+ } ,
237
+
238
+ function ( asyncTaskDone ) {
239
+ grunt . log . writeln ( 'rebuilding tests/test262/pages...' ) ;
240
+ var path = 'tests/build-pages.js' ,
241
+ content = grunt . file . read ( path ) ,
242
+ sandbox = {
243
+ __dirname : LIBS . path . join ( __dirname , 'tests' ) ,
244
+ require : require ,
245
+ console : {
246
+ log : function ( msg ) {
247
+ grunt . log . ok ( msg ) ;
248
+ }
249
+ }
250
+ } ;
251
+ try {
252
+ LIBS . vm . runInNewContext ( content , sandbox , path ) ;
253
+ } catch ( err ) {
254
+ asyncTaskDone ( err ) ;
255
+ }
256
+ grunt . log . ok ( 'done' ) ;
257
+ asyncTaskDone ( ) ;
258
+ } ,
259
+
260
+ function ( asyncTaskDone ) {
261
+ grunt . log . writeln ( 'cleaning up temporary directory...' ) ;
262
+ grunt . util . spawn ( {
263
+ cmd : 'rm' ,
264
+ args : [ '-rf' , tempDir ]
265
+ } , function ( err ) {
266
+ asyncTaskDone ( err ) ;
267
+ } ) ;
268
+ }
269
+
270
+ ] , function ( err ) {
271
+ if ( err ) {
272
+ grunt . log . error ( err . message ) ;
273
+ }
274
+ gruntTaskDone ( err ) ;
275
+ } ) ;
276
+ }
277
+
32
278
33
279
/**
34
280
* Compiles all JSON data into the polyfill and saves it as Intl.complete.js
0 commit comments