Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy over node buffer tests #51

Merged
merged 15 commits into from
Dec 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions bin/download-node-tests.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
Loading