Skip to content
This repository was archived by the owner on Jul 3, 2019. It is now read-only.

Commit 6ab8132

Browse files
authored
fix(tests): use safe-buffer because omfg (#69)
1 parent 45ad77a commit 6ab8132

File tree

8 files changed

+25
-51
lines changed

8 files changed

+25
-51
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"glob": "^7.1.1",
6666
"nyc": "^10.0.0",
6767
"require-inject": "^1.4.0",
68+
"safe-buffer": "^5.0.1",
6869
"standard": "^9.0.0",
6970
"standard-version": "^4.0.0",
7071
"tacks": "^1.2.2",

test/content.read.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3+
const Buffer = require('safe-buffer').Buffer
34
const Promise = require('bluebird')
45

5-
const bufferise = require('./util/bufferise')
66
const crypto = require('crypto')
77
const path = require('path')
88
const Tacks = require('tacks')
@@ -15,7 +15,7 @@ const CacheContent = require('./util/cache-content')
1515
const read = require('../lib/content/read')
1616

1717
test('read: returns a Promise with cache content data', function (t) {
18-
const CONTENT = bufferise('foobarbaz')
18+
const CONTENT = Buffer.from('foobarbaz')
1919
const DIGEST = crypto.createHash('sha512').update(CONTENT).digest('hex')
2020
const fixture = new Tacks(CacheContent({
2121
[DIGEST]: CONTENT
@@ -27,7 +27,7 @@ test('read: returns a Promise with cache content data', function (t) {
2727
})
2828

2929
test('read.stream: returns a stream with cache content data', function (t) {
30-
const CONTENT = bufferise('foobarbaz')
30+
const CONTENT = Buffer.from('foobarbaz')
3131
const DIGEST = crypto.createHash('sha512').update(CONTENT).digest('hex')
3232
const fixture = new Tacks(CacheContent({
3333
[DIGEST]: CONTENT
@@ -39,7 +39,7 @@ test('read.stream: returns a stream with cache content data', function (t) {
3939
stream.on('data', function (data) { buf += data })
4040
stream.on('end', function () {
4141
t.ok(true, 'stream completed successfully')
42-
t.deepEqual(bufferise(buf), CONTENT, 'cache contents read correctly')
42+
t.deepEqual(Buffer.from(buf), CONTENT, 'cache contents read correctly')
4343
t.end()
4444
})
4545
})

test/get.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const Buffer = require('safe-buffer').Buffer
34
const Promise = require('bluebird')
45

56
const crypto = require('crypto')
@@ -15,20 +16,14 @@ const testDir = require('./util/test-dir')(__filename)
1516
const CacheContent = require('./util/cache-content')
1617

1718
const CACHE = path.join(testDir, 'cache')
18-
const CONTENT = bufferise('foobarbaz')
19+
const CONTENT = Buffer.from('foobarbaz', 'utf8')
1920
const KEY = 'my-test-key'
2021
const ALGO = 'sha512'
2122
const DIGEST = crypto.createHash(ALGO).update(CONTENT).digest('hex')
2223
const METADATA = { foo: 'bar' }
2324

2425
var get = require('../get')
2526

26-
function bufferise (string) {
27-
return Buffer.from
28-
? Buffer.from(string, 'utf8')
29-
: new Buffer(string, 'utf8')
30-
}
31-
3227
// Simple wrapper util cause this gets WORDY
3328
function streamGet (byDigest) {
3429
const args = [].slice.call(arguments, 1)

test/put.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const Buffer = require('safe-buffer').Buffer
34
const Promise = require('bluebird')
45

56
const crypto = require('crypto')
@@ -13,7 +14,7 @@ const test = require('tap').test
1314
const testDir = require('./util/test-dir')(__filename)
1415

1516
const CACHE = path.join(testDir, 'cache')
16-
const CONTENT = bufferise('foobarbaz')
17+
const CONTENT = Buffer.from('foobarbaz', 'utf8')
1718
const KEY = 'my-test-key'
1819
const ALGO = 'sha512'
1920
const DIGEST = crypto.createHash(ALGO).update(CONTENT).digest('hex')
@@ -22,12 +23,6 @@ const contentPath = require('../lib/content/path')
2223

2324
var put = require('../put')
2425

25-
function bufferise (string) {
26-
return Buffer.from
27-
? Buffer.from(string, 'utf8')
28-
: new Buffer(string, 'utf8')
29-
}
30-
3126
test('basic bulk insertion', t => {
3227
return put(CACHE, KEY, CONTENT).then(digest => {
3328
t.equal(digest, DIGEST, 'returned content digest')

test/rm.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const Buffer = require('safe-buffer').Buffer
34
const Promise = require('bluebird')
45

56
const crypto = require('crypto')
@@ -15,7 +16,7 @@ const testDir = require('./util/test-dir')(__filename)
1516

1617
const CacheContent = require('./util/cache-content')
1718
const CACHE = path.join(testDir, 'cache')
18-
const CONTENT = bufferise('foobarbaz')
19+
const CONTENT = Buffer.from('foobarbaz')
1920
const KEY = 'my-test-key'
2021
const ALGO = 'sha512'
2122
const DIGEST = crypto.createHash(ALGO).update(CONTENT).digest('hex')
@@ -26,12 +27,6 @@ const get = require('../get')
2627

2728
const rm = require('../rm')
2829

29-
function bufferise (string) {
30-
return Buffer.from
31-
? Buffer.from(string, 'utf8')
32-
: new Buffer(string, 'utf8')
33-
}
34-
3530
test('rm.entry removes entries, not content', t => {
3631
const fixture = new Tacks(CacheContent({
3732
[DIGEST]: CONTENT

test/strict.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
'use strict'
22

3-
var fs = require('fs')
4-
var glob = require('glob')
5-
var path = require('path')
6-
var test = require('tap').test
3+
const Buffer = require('safe-buffer').Buffer
4+
5+
const fs = require('fs')
6+
const glob = require('glob')
7+
const path = require('path')
8+
const test = require('tap').test
79

810
test('all JavaScript source files use strict mode', function (t) {
9-
var globStr = '**/*.js'
10-
var root = path.resolve(__dirname, '../')
11+
const globStr = '**/*.js'
12+
const root = path.resolve(__dirname, '../')
1113
glob(globStr, {
1214
cwd: root,
1315
ignore: 'node_modules/**/*.js'
1416
}, function (err, files) {
1517
if (err) { throw err }
16-
var line = "'use strict'\n"
17-
var bytecount = line.length
18-
// node@0.12 doesn't have Buffer.alloc
19-
var buf = Buffer.alloc ? Buffer.alloc(bytecount) : new Buffer(bytecount)
18+
const line = "'use strict'\n"
19+
const bytecount = line.length
20+
const buf = new Buffer(bytecount)
2021
files.forEach(function (f) {
21-
var fd = fs.openSync(path.join(root, f), 'r')
22+
const fd = fs.openSync(path.join(root, f), 'r')
2223
fs.readSync(fd, buf, 0, bytecount, 0)
2324
fs.closeSync(fd)
2425
t.equal(buf.toString('utf8'), line, f + ' is using strict mode.')

test/util/bufferise.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/verify.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const Buffer = require('safe-buffer').Buffer
34
const Promise = require('bluebird')
45

56
const crypto = require('crypto')
@@ -14,7 +15,7 @@ const testDir = require('./util/test-dir')(__filename)
1415
const CacheContent = require('./util/cache-content')
1516

1617
const CACHE = path.join(testDir, 'cache')
17-
const CONTENT = bufferise('foobarbaz')
18+
const CONTENT = Buffer.from('foobarbaz', 'utf8')
1819
const KEY = 'my-test-key'
1920
const ALGO = 'sha512'
2021
const DIGEST = crypto.createHash(ALGO).update(CONTENT).digest('hex')
@@ -23,12 +24,6 @@ const BUCKET = index._bucketPath(CACHE, KEY)
2324

2425
const verify = require('../verify')
2526

26-
function bufferise (string) {
27-
return Buffer.from
28-
? Buffer.from(string, 'utf8')
29-
: new Buffer(string, 'utf8')
30-
}
31-
3227
function mockCache () {
3328
const fixture = new Tacks(CacheContent({
3429
[DIGEST]: CONTENT

0 commit comments

Comments
 (0)