Skip to content

Commit 584e644

Browse files
committed
tasks: refactor pretest
- group sub-tasks in subroutines - DRY up using common module - add image test container setup sub-task such that `npm run pretest` on CircleCI runs the docker container and sets it up once and for all
1 parent 640bdb4 commit 584e644

File tree

1 file changed

+66
-33
lines changed

1 file changed

+66
-33
lines changed

tasks/pretest.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,83 @@
11
var fs = require('fs');
2-
var constants = require('./util/constants');
32

3+
var constants = require('./util/constants');
4+
var common = require('./util/common');
5+
var containerCommands = require('./util/container_commands');
6+
var isCI = process.env.CIRCLECI;
47

8+
// main
9+
makeCredentialsFile();
10+
makeSetPlotConfigFile();
11+
makeTestImageFolders();
12+
if(isCI) runAndSetupImageTestContainer();
513

614
// Create a credentials json file,
715
// to be required in jasmine test suites and test dashboard
8-
var credentials = JSON.stringify({
9-
MAPBOX_ACCESS_TOKEN: mapboxAccessToken
10-
}, null, 2);
16+
function makeCredentialsFile() {
17+
var credentials = JSON.stringify({
18+
MAPBOX_ACCESS_TOKEN: constants.mapboxAccessToken
19+
}, null, 2);
1120

12-
fs.writeFile(constants.pathToCredentials, credentials, function(err) {
13-
if(err) throw err;
14-
});
21+
common.writeFile(constants.pathToCredentials, credentials);
22+
logger('make build/credentials.json');
23+
}
1524

1625
// Create a 'set plot config' file,
1726
// to be included in the image test index
18-
var setPlotConfig = [
19-
'\'use strict\';',
20-
'',
21-
'/* global Plotly:false */',
22-
'',
23-
'Plotly.setPlotConfig({',
24-
' mapboxAccessToken: \'' + mapboxAccessToken + '\'',
25-
'});',
26-
''
27-
].join('\n');
28-
29-
fs.writeFile(constants.pathToSetPlotConfig, setPlotConfig, function(err) {
30-
if(err) throw err;
31-
});
32-
33-
// make artifact folders for image tests
34-
if(!doesDirExist(constants.pathToTestImagesDiff)) {
35-
fs.mkdirSync(constants.pathToTestImagesDiff);
27+
function makeSetPlotConfigFile() {
28+
var setPlotConfig = [
29+
'\'use strict\';',
30+
'',
31+
'/* global Plotly:false */',
32+
'',
33+
'Plotly.setPlotConfig({',
34+
' mapboxAccessToken: \'' + constants.mapboxAccessToken + '\'',
35+
'});',
36+
''
37+
].join('\n');
38+
39+
common.writeFile(constants.pathToSetPlotConfig, setPlotConfig);
40+
logger('make build/set_plot_config.js');
3641
}
37-
if(!doesDirExist(constants.pathToTestImages)) {
38-
fs.mkdirSync(constants.pathToTestImages);
42+
43+
// Make artifact folders for image tests
44+
function makeTestImageFolders() {
45+
46+
function makeOne(folderPath, info) {
47+
if(!common.doesDirExist(folderPath)) {
48+
fs.mkdirSync(folderPath);
49+
logger('initialize ' + info);
50+
}
51+
else logger(info + ' is present');
52+
}
53+
54+
makeOne(constants.pathToTestImages, 'test image folder');
55+
makeOne(constants.pathToTestImagesDiff, 'test image diff folder');
3956
}
4057

41-
function doesDirExist(dirPath) {
42-
try {
43-
if(fs.statSync(dirPath).isDirectory()) return true;
58+
// On CircleCI, run and setup image test container once an for all
59+
function runAndSetupImageTestContainer() {
60+
61+
function run() {
62+
var cmd = containerCommands.dockerRun;
63+
common.execCmd(cmd, function() {
64+
logger('run docker container');
65+
setup();
66+
});
4467
}
45-
catch(e) {
46-
return false;
68+
69+
function setup() {
70+
var cmd = containerCommands.getRunCmd(isCI, [
71+
containerCommands.setup
72+
]);
73+
common.execCmd(cmd, function() {
74+
logger('setup docker container');
75+
});
4776
}
4877

49-
return false;
78+
run();
79+
}
80+
81+
function logger(task) {
82+
console.log('ok ' + task);
5083
}

0 commit comments

Comments
 (0)