]> zoso.dev Git - buffer.git/commitdiff
add downloader to pull node's buffer tests
authorJesse Tane <jesse.tane@gmail.com>
Mon, 15 Dec 2014 23:50:35 +0000 (18:50 -0500)
committerJesse Tane <jesse.tane@gmail.com>
Mon, 22 Dec 2014 22:44:15 +0000 (17:44 -0500)
bin/download-node-tests.js [new file with mode: 0755]
package.json

diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js
new file mode 100755 (executable)
index 0000000..5ff977a
--- /dev/null
@@ -0,0 +1,120 @@
+#!/usr/bin/env node
+
+var hyperquest = require('hyperquest')
+var concat = require('concat-stream')
+var split  = require('split')
+var thru = require('through2')
+var fs = require('fs')
+
+var url = 'https://api.github.com/repos/iojs/io.js/contents'
+var dirs = [
+  '/test/parallel',
+  '/test/pummel'
+]
+
+var httpOpts = {
+  headers: {
+    'User-Agent': null,
+
+    // auth if github rate-limits you...
+    // 'Authorization': 'Basic ' + Buffer('username:password').toString('base64'),
+  }
+}
+
+dirs.forEach(function(dir) {
+  var req = hyperquest(url + dir, httpOpts)
+  req.pipe(concat(function(data) {
+    if (req.response.statusCode !== 200) {
+      throw new Error(url + dir + ': ' + data.toString())
+    }
+
+    downloadBufferTests(dir, JSON.parse(data));
+  }))
+})
+
+function downloadBufferTests(dir, files) {
+  files.forEach(function(file) {
+    if (!/test-buffer.*/.test(file.name)) return
+
+    hyperquest(file.download_url, httpOpts)
+      .pipe(split())
+      .pipe(testfixer(file.name))
+      .pipe(fs.createWriteStream(__dirname + '/../test/node-' + file.name))
+  })
+}
+
+function testfixer(filename) {
+  var firstline = true
+
+  return thru(function(line, enc, cb) {
+    line = line.toString()
+
+    if (firstline) {
+
+      // require buffer explicitly and wrap in tape test
+      line = 'var Buffer = require(\'../\').Buffer\n' +
+             'var test = require(\'tape\')\n' +
+             'if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false\n' +
+             'test(\'' + filename + '\', function(t) {\n' + line
+
+      firstline = false
+    }
+
+    // use tape not assert
+    if (/^var assert/.test(line)) {
+      line = line.replace(/(^var assert.*)/, '// $1')
+    }
+    else {
+
+      // use "ok" instead of shortcut
+      line = line.replace(/assert\(/, 'assert.ok(')
+
+      // use tape
+      line = line.replace(/assert/, 't')
+    }
+
+    // comment out require('common')
+    line = line.replace(/^(var common = require.*)/, '// $1')
+
+    // require browser buffer
+    line = line.replace(/(.*)require\('buffer'\)(.*)/, '$1require(\'../\')$2')
+
+    // smalloc is only used for kMaxLength
+    line = line.replace(/require\('smalloc'\)/, '{ kMaxLength: 0x3FFFFFFF }')
+
+    // comment out console logs
+    line = line.replace(/(.*console\..*)/, '// $1')
+
+    // tape's equal can't compare strings and buffers
+    line = line.replace(/t.equal\(buf.slice\(([^\)]*)\), /, 't.equal(buf.slice($1).toString(), ')
+
+    // we can't reliably test typed array max-sizes in the browser
+    if (filename === 'test-buffer-big.js') {
+      line = line.replace(/(.*new Int8Array.*RangeError.*)/, '// $1')
+      line = line.replace(/(.*new ArrayBuffer.*RangeError.*)/, '// $1')
+      line = line.replace(/(.*new Float64Array.*RangeError.*)/, '// $1')
+    }
+
+    // https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L38
+    // we can't run this because we need to support
+    // browsers that don't have typed arrays
+    if (filename === 'test-buffer.js') {
+      line = line.replace(/b\[0\] = -1;/, 'b[0] = 255;')
+    }
+
+    // https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L1138
+    // unfortunately we can't run this as it touches
+    // node streams which do an instanceof check
+    if (filename === 'test-buffer.js') {
+      line = line.replace(/(crypto.createHash.*\))/, '1 /*$1*/')
+    }
+
+    cb(null, line + '\n')
+  }, function(cb) {
+
+    // close tape wrapper
+    this.push('\nt.end()\n})')
+
+    cb()
+  })
+}
index 5a9a1cdeb13a8fb6364132af6b1974076ee5cdac..d23323a9f9a08030c219c715b51768120338f987 100644 (file)
   "devDependencies": {
     "benchmark": "^1.0.0",
     "browserify": "^6.2.0",
+    "concat-stream": "^1.4.7",
+    "hyperquest": "^1.0.1",
     "is-nan": "^1.0.1",
+    "split": "^0.3.2",
     "tape": "^3.0.1",
+    "through2": "^0.6.3",
     "zuul": "^1.12.0"
   },
   "homepage": "https://github.com/feross/buffer",