From 54383578fb4788ecffeee04c835354d83f45424a Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 11 Apr 2019 09:58:20 -0700 Subject: [PATCH] test: configurable and unique test directory path - Multiple parallel runs of the same test will not conflict as a unique identifier is added to the test directory. - The base test directory can be configured for various environments, and can be changed via the environment variable `TEMP`, see the implementation of `os.tmpdir()` for specific details. --- test/blockstore-test.js | 8 ++++---- test/util/common.js | 11 ++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/blockstore-test.js b/test/blockstore-test.js index 048c438b..39a777dc 100644 --- a/test/blockstore-test.js +++ b/test/blockstore-test.js @@ -9,7 +9,7 @@ const assert = require('./util/assert'); const common = require('./util/common'); const {resolve} = require('path'); const fs = require('bfile'); -const {rimraf} = require('./util/common'); +const {rimraf, testdir} = require('./util/common'); const random = require('bcrypto/lib/random'); const vectors = [ @@ -618,7 +618,7 @@ describe('BlockStore', function() { }); describe('FileBlockStore (Integration 1)', function() { - const location = '/tmp/bcoin-blockstore-test'; + const location = testdir('blockstore'); let store = null; beforeEach(async () => { @@ -948,7 +948,7 @@ describe('BlockStore', function() { }); describe('FileBlockStore (Integration 2)', function() { - const location = '/tmp/bcoin-blockstore-test'; + const location = testdir('blockstore'); let store = null; beforeEach(async () => { @@ -1101,7 +1101,7 @@ describe('BlockStore', function() { }); describe('LevelBlockStore', function() { - const location = '/tmp/bcoin-blockstore-test'; + const location = testdir('blockstore'); let store = null; beforeEach(async () => { diff --git a/test/util/common.js b/test/util/common.js index bdaa8620..39177fc5 100644 --- a/test/util/common.js +++ b/test/util/common.js @@ -1,9 +1,11 @@ 'use strict'; const assert = require('assert'); +const {tmpdir} = require('os'); const path = require('path'); const fs = require('bfile'); const bio = require('bufio'); +const {randomBytes} = require('bcrypto/lib/random'); const Block = require('../../lib/primitives/block'); const MerkleBlock = require('../../lib/primitives/merkleblock'); const Headers = require('../../lib/primitives/headers'); @@ -85,8 +87,15 @@ common.writeTX = function writeTX(name, tx, view) { common.writeFile(`${name}-undo.raw`, undoRaw); }; +common.testdir = function(name) { + assert(/^[a-z]+$/.test(name), 'Invalid name'); + + const uniq = randomBytes(4).toString('hex'); + return path.join(tmpdir(), `bcoin-test-${name}-${uniq}`); +}; + common.rimraf = async function(p) { - const allowed = new RegExp('^\/tmp\/(.*)$'); + const allowed = /bcoin\-test\-[a-z]+\-[a-f0-9]{8}(\/[a-z]+)?$/; if (!allowed.test(p)) throw new Error(`Path not allowed: ${p}.`);