From 79947337e438af0dc5e78a25acaf80978145109e Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Fri, 7 Aug 2015 21:32:40 +0100 Subject: [PATCH] Fix sporadic conflict between agile_keychain test and CLI test Both test suites contained tests that tried to create a vault in /tmp/new-vault.agilekeychain Fix this by adding a utility function for getting a per-suite temp dir name for tests under /tmp/passcards-tests/ and using that. --- lib/agile_keychain_test.ts | 22 +++++++++++----------- lib/base/streamutil_test.ts | 8 +++++--- lib/sync_int_test.ts | 2 +- lib/test.ts | 17 +++++++++++++++++ lib/vfs/util_test.ts | 2 +- lib/vfs/vfs_test.ts | 4 ++-- 6 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/agile_keychain_test.ts b/lib/agile_keychain_test.ts index 2fa249c..9cf3f78 100644 --- a/lib/agile_keychain_test.ts +++ b/lib/agile_keychain_test.ts @@ -35,7 +35,7 @@ class TestCase { itemDataPath: string; } -var TEST_VAULTS: TestCase[] = [ +const TEST_VAULTS: TestCase[] = [ { path: 'test.agilekeychain', password: 'logMEin', @@ -43,7 +43,7 @@ var TEST_VAULTS: TestCase[] = [ } ]; -var fs = new nodefs.FileVFS('lib/test-data'); +let fs = new nodefs.FileVFS('lib/test-data'); class ItemAndContent { item: item_store.Item; @@ -57,7 +57,7 @@ function createEmptyVault() { let fs = new nodefs.FileVFS('/'); let vault: agile_keychain.Vault; - return vfs_util.mktemp(fs, '/tmp', 'vault.XXX').then(path => { + return vfs_util.mktemp(fs, testLib.tempDir(), 'vault.XXX').then(path => { return agile_keychain.Vault.createVault(fs, path, VAULT_PASS, '', VAULT_PASS_ITER); }).then(vault_ => { vault = vault_; @@ -67,7 +67,7 @@ function createEmptyVault() { function createTestVault() { let sourcePath = path.resolve('lib/test-data'); - let copyPath = '/tmp/copy.agilekeychain'; + let copyPath = testLib.tempDir() + '/copy.agilekeychain'; let vault: agile_keychain.Vault; let fs = new nodefs.FileVFS('/'); @@ -489,12 +489,12 @@ testLib.addAsyncTest('Encrypt/decrypt key (async)', (assert) => { }); testLib.addAsyncTest('Create new vault', (assert) => { - var fs = new nodefs.FileVFS('/tmp'); - var pass = 'test-new-vault-pass'; - var hint = 'the-password-hint'; - var vault: agile_keychain.Vault; - var keyIterations = 100; - var vaultDir = '/new-vault'; + let fs = new nodefs.FileVFS(testLib.tempDir()); + let pass = 'test-new-vault-pass'; + let hint = 'the-password-hint'; + let vault: agile_keychain.Vault; + let keyIterations = 100; + let vaultDir = '/new-vault'; return vfs_util.rmrf(fs, vaultDir + '.agilekeychain').then(() => { return agile_keychain.Vault.createVault(fs, vaultDir, pass, hint, keyIterations) @@ -618,7 +618,7 @@ testLib.addTest('Default item properties', (assert) => { }); testLib.addTest('createVault() fails if directory exists', (assert) => { - var fs = new nodefs.FileVFS('/tmp'); + var fs = new nodefs.FileVFS(testLib.tempDir()); var pass = 'pass-1'; var hint = 'test-new-vault-hint'; var keyIterations = 100; diff --git a/lib/base/streamutil_test.ts b/lib/base/streamutil_test.ts index 56edbd1..b60e6b6 100644 --- a/lib/base/streamutil_test.ts +++ b/lib/base/streamutil_test.ts @@ -4,9 +4,11 @@ import streamutil = require('./streamutil'); import testLib = require('../test'); testLib.addAsyncTest('read binary stream', (assert) => { - var PNG_MAGIC = [0x89, 0x50, 0x4e, 0x47]; - fs.writeFileSync('/tmp/test.png', new Buffer(PNG_MAGIC)); - var testFile = fs.createReadStream('/tmp/test.png'); + const PNG_MAGIC = [0x89, 0x50, 0x4e, 0x47]; + const TEST_PATH = `${testLib.tempDir() }/test.png`; + + fs.writeFileSync(TEST_PATH, new Buffer(PNG_MAGIC)); + let testFile = fs.createReadStream(TEST_PATH); return streamutil.readAll(testFile).then((content) => { assert.equal(content, '\x89PNG'); }); diff --git a/lib/sync_int_test.ts b/lib/sync_int_test.ts index fc18d1a..0b8879a 100644 --- a/lib/sync_int_test.ts +++ b/lib/sync_int_test.ts @@ -17,7 +17,7 @@ global.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange'); testLib.addAsyncTest('saves and syncs items', assert => { let db = new key_value_store.IndexedDBDatabase(); - let fs = new vfs_node.FileVFS('/tmp/sync-integration-test'); + let fs = new vfs_node.FileVFS(`${testLib.tempDir() }/sync-integration-test`); let keyAgent = new key_agent.SimpleKeyAgent(); let agileKeychainStore: item_store.Store; let localStore = new local_store.Store(db, 'local', keyAgent); diff --git a/lib/test.ts b/lib/test.ts index 9084ead..fcde00d 100644 --- a/lib/test.ts +++ b/lib/test.ts @@ -21,6 +21,7 @@ import assert = require('assert'); import argparse = require('argparse'); import colors = require('colors'); import fs = require('fs'); +import mkdirp = require('mkdirp'); import path = require('path'); import underscore = require('underscore'); import xdiff = require('xdiff'); @@ -291,6 +292,22 @@ export function start(args?: string[]) { } } +/** Returns the path to a temporary data directory for + * use by the current test suite. + */ +export function tempDir() { + let tmpDir: string; + if (process.env.TMPDIR) { + tmpDir = process.env.TMPDIR; + } else { + tmpDir = '/tmp'; + } + let testSuiteName = path.basename(process.argv[1]); + let dirPath = `${tmpDir}/passcards-tests/${testSuiteName}`; + mkdirp.sync(dirPath); + return dirPath; +} + function run(tests: TestCase[]) { // randomize ordering of tests tests = underscore(tests).shuffle(); diff --git a/lib/vfs/util_test.ts b/lib/vfs/util_test.ts index 31ca965..db5178b 100644 --- a/lib/vfs/util_test.ts +++ b/lib/vfs/util_test.ts @@ -7,7 +7,7 @@ import asyncutil = require('../base/asyncutil'); var fs_extra = require('fs-extra'); testLib.addAsyncTest('mktemp', (assert) => { - const TEST_DIR = '/tmp/vfs-util-test'; + const TEST_DIR = `${testLib.tempDir() }/vfs-util-test`; const ITER_COUNT = 10; fs_extra.emptyDirSync(TEST_DIR); diff --git a/lib/vfs/vfs_test.ts b/lib/vfs/vfs_test.ts index 31faad5..057eaa3 100644 --- a/lib/vfs/vfs_test.ts +++ b/lib/vfs/vfs_test.ts @@ -12,8 +12,8 @@ class StorageEntry { } var createNodeFs = () => { - var TEST_DIR = '/tmp/vfs-test'; - var fs = new nodefs.FileVFS(TEST_DIR); + let TEST_DIR = `${testLib.tempDir() }/vfs-test`; + let fs = new nodefs.FileVFS(TEST_DIR); return vfs_util.rmrf(fs, '') .then(() => { return fs.mkpath('');