Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2081 from trufflesuite/update-test
Browse files Browse the repository at this point in the history
Internal improvement: Update truffle test code
  • Loading branch information
eggplantzzz committed Jun 19, 2019
2 parents 3ea1fef + faef71c commit acba61b
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 320 deletions.
161 changes: 77 additions & 84 deletions packages/truffle-core/lib/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const command = {
const promisifiedCopy = promisify(require("../copy"));
const Environment = require("../environment");

var config = Config.detect(options);
const config = Config.detect(options);

// if "development" exists, default to using that for testing
if (!config.network && config.networks.development) {
Expand All @@ -67,108 +67,101 @@ const command = {
Environment.detect(config).catch(done);
}

var ipcDisconnect;

var files = [];
let ipcDisconnect;
let files = [];

if (options.file) {
files = [options.file];
} else if (options._.length > 0) {
Array.prototype.push.apply(files, options._);
}

function getFiles(callback) {
if (files.length !== 0) {
return callback(null, files);
try {
if (files.length === 0) {
files = dir.files(config.test_directory, { sync: true });
}
} catch (error) {
return done(error);
}

dir.files(config.test_directory, callback);
files = files.filter(function(file) {
return file.match(config.test_file_extension_regexp) != null;
});

let temporaryDirectory;
try {
temporaryDirectory = temp.mkdirSync("test-");
} catch (error) {
return done(error);
}

getFiles(function(err, files) {
if (err) return done(err);
function runCallback() {
var args = arguments;
// Ensure directory cleanup.
done.apply(null, args);
if (ipcDisconnect) ipcDisconnect();
}

files = files.filter(function(file) {
return file.match(config.test_file_extension_regexp) != null;
});
function run() {
// Set a new artifactor; don't rely on the one created by Environments.
// TODO: Make the test artifactor configurable.
config.artifactor = new Artifactor(temporaryDirectory);

temp.mkdir("test-", function(err, temporaryDirectory) {
if (err) return done(err);
const testConfig = config.with({
test_files: files,
contracts_build_directory: temporaryDirectory
});
Test.run(testConfig)
.then(runCallback)
.catch(runCallback);
}

function runCallback() {
var args = arguments;
// Ensure directory cleanup.
done.apply(null, args);
if (ipcDisconnect) {
ipcDisconnect();
}
}
const environmentCallback = function() {
// Copy all the built files over to a temporary directory, because we
// don't want to save any tests artifacts. Only do this if the build directory
// exists.
try {
fs.statSync(config.contracts_build_directory);
} catch (_error) {
return run();
}

function run() {
// Set a new artifactor; don't rely on the one created by Environments.
// TODO: Make the test artifactor configurable.
config.artifactor = new Artifactor(temporaryDirectory);

Test.run(
config.with({
test_files: files,
contracts_build_directory: temporaryDirectory
}),
runCallback
);
}
promisifiedCopy(config.contracts_build_directory, temporaryDirectory)
.then(() => {
config.logger.log("Using network '" + config.network + "'." + OS.EOL);

const environmentCallback = function(err) {
if (err) return done(err);
// Copy all the built files over to a temporary directory, because we
// don't want to save any tests artifacts. Only do this if the build directory
// exists.
try {
fs.statSync(config.contracts_build_directory);
} catch (_error) {
return run();
}

promisifiedCopy(config.contracts_build_directory, temporaryDirectory)
.then(() => {
config.logger.log(
"Using network '" + config.network + "'." + OS.EOL
);

run();
})
.catch(done);
};
run();
})
.catch(done);
};

if (config.networks[config.network]) {
Environment.detect(config)
if (config.networks[config.network]) {
Environment.detect(config)
.then(() => environmentCallback())
.catch(done);
} else {
const ipcOptions = { network: "test" };

const ganacheOptions = {
host: "127.0.0.1",
port: 7545,
network_id: 4447,
mnemonic:
"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat",
gasLimit: config.gas,
noVMErrorsOnRPCResponse: true
};
Develop.connectOrStart(
ipcOptions,
ganacheOptions,
(started, disconnect) => {
ipcDisconnect = disconnect;
Environment.develop(config, ganacheOptions)
.then(() => environmentCallback())
.catch(environmentCallback);
} else {
const ipcOptions = { network: "test" };

const ganacheOptions = {
host: "127.0.0.1",
port: 7545,
network_id: 4447,
mnemonic:
"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat",
gasLimit: config.gas,
noVMErrorsOnRPCResponse: true
};
Develop.connectOrStart(
ipcOptions,
ganacheOptions,
(started, disconnect) => {
ipcDisconnect = disconnect;
Environment.develop(config, ganacheOptions)
.then(() => environmentCallback())
.catch(environmentCallback);
}
);
.catch(done);
}
});
});
);
}
}
};

Expand Down

0 comments on commit acba61b

Please sign in to comment.