This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
/
test.js
173 lines (151 loc) · 4.79 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var command = {
command: "test",
description: "Run JavaScript and Solidity tests",
builder: {
"show-events": {
describe: "Show all test logs",
type: "boolean",
default: false
}
},
help: {
usage:
"truffle test [<test_file>] [--compile-all] [--network <name>] [--verbose-rpc] [--show-events]",
options: [
{
option: "<test_file>",
description:
"Name of the test file to be run. Can include path information if the file " +
"does not exist in the\n current directory."
},
{
option: "--compile-all",
description:
"Compile all contracts instead of intelligently choosing which contracts need " +
"to be compiled."
},
{
option: "--network <name>",
description:
"Specify the network to use, using artifacts specific to that network. Network " +
"name must exist\n in the configuration."
},
{
option: "--verbose-rpc",
description:
"Log communication between Truffle and the Ethereum client."
},
{
option: "--show-events",
description: "Log all contract events."
}
]
},
run: function(options, done) {
var OS = require("os");
var dir = require("node-dir");
var temp = require("temp");
var Config = require("truffle-config");
var Artifactor = require("truffle-artifactor");
var Develop = require("../develop");
var Test = require("../test");
var fs = require("fs");
var copy = require("../copy");
var Environment = require("../environment");
var config = Config.detect(options);
// if "development" exists, default to using that for testing
if (!config.network && config.networks.development) {
config.network = "development";
}
if (!config.network) {
config.network = "test";
}
var ipcDisconnect;
var 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);
}
dir.files(config.test_directory, callback);
}
getFiles(function(err, files) {
if (err) return done(err);
files = files.filter(function(file) {
return file.match(config.test_file_extension_regexp) != null;
});
temp.mkdir("test-", function(err, temporaryDirectory) {
if (err) return done(err);
function cleanup() {
var args = arguments;
// Ensure directory cleanup.
temp.cleanup(() => {
// Ignore cleanup errors.
done.apply(null, args);
if (ipcDisconnect) {
ipcDisconnect();
}
});
}
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
}),
cleanup
);
}
var 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.
fs.stat(config.contracts_build_directory, function(err) {
if (err) return run();
copy(config.contracts_build_directory, temporaryDirectory, function(
err
) {
if (err) return done(err);
config.logger.log(
"Using network '" + config.network + "'." + OS.EOL
);
run();
});
});
};
if (config.networks[config.network]) {
Environment.detect(config, environmentCallback);
} else {
var ipcOptions = {
network: "test"
};
var 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, function(
started,
disconnect
) {
ipcDisconnect = disconnect;
Environment.develop(config, ganacheOptions, environmentCallback);
});
}
});
});
}
};
module.exports = command;