Skip to content

Commit 2a9bd63

Browse files
Use spawn() instead of exec() to run the docker commands. Check if matchstick image already exists and skip docker build. Build the args for the docker run command
1 parent b5e3542 commit 2a9bd63

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

src/commands/test.js

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const os = require('os')
22
const chalk = require('chalk')
33
const fetch = require('node-fetch')
44
const semver = require('semver')
5-
const { exec } = require('child_process')
5+
const { spawn, exec } = require('child_process')
66
const fs = require('fs')
77

88
const HELP = `
@@ -11,8 +11,9 @@ ${chalk.bold('graph test')} ${chalk.dim('[options]')} ${chalk.bold('<datasource>
1111
${chalk.dim('Options:')}
1212
-h, --help Show usage information
1313
-v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
14-
-c, --flags <flags>
14+
-f, --flags <flags>
1515
`
16+
// -r, --root The root folder of the subgraph project. Default is the folder where the 'graph test' command is executed from.
1617

1718
module.exports = {
1819
description: 'Runs rust binary for subgraph testing',
@@ -97,38 +98,45 @@ CMD ../binary-linux-20 \${ARGS}
9798
}
9899
})
99100

100-
exec(`docker build -t matchstick .`, (error, stdout, stderr) => {
101-
print.info('Building Matchstick image...');
101+
// TODO:
102+
// 1. Check if image exists - done
103+
// 2. Check if ARGS are passed. Add them to the options there are ARGS - done
104+
// ?? 3. Add option to pass root/host folder
102105

103-
if (error) {
104-
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.');
105-
print.info(`error: ${error.message}`)
106+
exec('docker images -q matchstick', (error, stdout, stderr) => {
107+
let current_folder = process.cwd();
108+
let args = '';
109+
110+
if(datasource) {
111+
args = args + datasource
106112
}
107-
if (stderr) {
108-
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.');
109-
print.info(`stderr: ${stderr}`)
113+
114+
if(coverage) {
115+
args = args + ' ' + '-c'
110116
}
111117

112-
//docker run -it --rm --mount type=bind,source=$PWD,target=/matchstick -e ARGS="-c" matchstick
113-
let runCommand = `docker run --rm --mount type=bind,source=$PWD,target=/matchstick -e ARGS="${datasource || ''}${coverage ? '-c' : ''}" matchstick`;
114-
115-
exec(runCommand, (error, stdout, stderr) => {
116-
print.info('Running Matchstick image...');
117-
118-
if (error) {
119-
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.');
120-
print.info(`error: ${error.message}`)
121-
process.exit(1);
122-
}
123-
if (stderr) {
124-
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.');
125-
print.info(`stderr: ${stderr}`)
126-
process.exit(1);
127-
}
128-
print.info(stdout)
129-
process.exit();
130-
})
131-
})
118+
let options = ['run', '-it', '--rm', '--mount', `type=bind,source=${current_folder},target=/matchstick`];
119+
120+
if(args !== '') {
121+
options.push('-e')
122+
options.push(`ARGS=${args}`);
123+
}
132124

125+
options.push('matchstick')
126+
127+
if(stdout === "") {
128+
spawn(
129+
'docker',
130+
['build', '-f', 'tests/.docker/Dockerfile', '-t', 'matchstick', '.'],
131+
{ stdio: 'inherit' }
132+
).on('close', code => {
133+
if (code === 0) {
134+
spawn('docker', options, { stdio: 'inherit' });
135+
}
136+
})
137+
} else {
138+
spawn('docker', options, { stdio: 'inherit' });
139+
}
140+
})
133141
},
134-
}
142+
}

0 commit comments

Comments
 (0)