fix issue 42 by splitting compose options and command options

This commit is contained in:
Alexander Zeitler 2019-03-26 23:24:54 +01:00
parent 9646366e16
commit f7fa8b1c47
6 changed files with 37 additions and 6 deletions

1
index.d.ts vendored
View file

@ -25,6 +25,7 @@ declare module "docker-compose" {
config?: string | string[];
log?: boolean;
composeOptions?: string[] | Array<string | string[]>;
commandOptions?: string[] | Array<string | string[]>;
}
interface IDockerComposeLogOptions extends IDockerComposeOptions {

View file

@ -47,13 +47,16 @@ const composeOptionsToArgs = function (composeOptions) {
* @param {boolean} [options.log]
* @param {?(string|string[])} [options.config]
* @param {?object} [options.env]
* @param {?(string[]|Array<string|string[]>)} [options.commandOptions]
* @param {?(string[]|Array<string|string[]>)} [options.composeOptions]
*/
const execCompose = (command, args, options) => new Promise((resolve, reject) => {
const composeOptions = options.composeOptions || [];
let composeArgs = configToArgs(options.config).concat([ command ], args);
const commandOptions = options.commandOptions || [];
let composeArgs = composeOptionsToArgs(composeOptions);
composeArgs = composeArgs.concat(configToArgs(options.config).concat([ command ].concat(composeOptionsToArgs(commandOptions), args)));
composeArgs = composeArgs.concat(composeOptionsToArgs(composeOptions));
const cwd = options.cwd;
const env = options.env || null;
@ -186,6 +189,7 @@ const rm = function (options) {
* @return {object} std.out / std.err
*/
const exec = function (container, command, options) {
const args = command.split(/\s+/);
return execCompose('exec', [ '-T', container ].concat(args), options);

View file

@ -62,7 +62,8 @@ compose.exec('node', 'npm install', { cwd: path.join(__dirname) })
* `cwd {string}`: mandatory folder path to the `docker-compose.yml`
* `config {(string|string[])}`: custom and/or multiple yml files can be specified (relative to `cwd`)
* `[log] {boolean}`: optional setting to enable console logging (output of `docker-compose` `stdout`/`stderr` output)
* `[commandOptions] string[]|Array<string|string[]`: pass optional command options like `"--build"` or `[["--build"], ["--timeout", "5"]]` or `["--build", ["--timeout", "5"]]` for the `up` command.
* `[composeOptions] string[]|Array<string|string[]`: pass optional compose options like `"--verbose"` or `[["--verbose"], ["--log-level", "DEBUG"]]` or `["--verbose", ["--loglevel", "DEBUG"]]` for *all* commands.
* `[commandOptions] string[]|Array<string|string[]`: pass optional command options like `"--build"` or `[["--build"], ["--timeout", "5"]]` or `["--build", ["--timeout", "5"]]` for the `up` command. Viable `commandOptions` depend on the command (`up`, `down` etc.) itself
## Running the tests

View file

@ -0,0 +1,7 @@
version: '3'
services:
some-service:
image: alpine
volumes:
- ./volume:/mountedvolume

View file

@ -55,7 +55,7 @@ test('ensure container gets started', async assert => {
test('ensure container gets started with --build option', async assert => {
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', composeOptions: [ '--build' ]});
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [ '--build' ]});
assert.true(await isContainerRunning('/compose_test_mongodb'));
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
@ -64,7 +64,7 @@ test('ensure container gets started with --build option', async assert => {
test('ensure container gets started with --build and --timeout option', async assert => {
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', composeOptions: [[ '--build' ], [ '--timeout', '5' ]]});
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [[ '--build' ], [ '--timeout', '5' ]]});
assert.true(await isContainerRunning('/compose_test_mongodb'));
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
@ -73,13 +73,30 @@ test('ensure container gets started with --build and --timeout option', async as
test('ensure container gets started with --build and --timeout option', async assert => {
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', composeOptions: [ '--build', [ '--timeout', '5' ]]});
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [ '--build', [ '--timeout', '5' ]]});
assert.true(await isContainerRunning('/compose_test_mongodb'));
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
assert.end();
});
test('ensure container command executed with --workdir command option', async assert => {
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
assert.doesNotThrow(async () => {
await compose.run('some-service', 'cat hello.txt', {
cwd: path.join(__dirname),
log: true,
config: 'docker-compose-42.yml',
composeOptions: [ '--verbose' ],
commandOptions: [ '--workdir', '/mountedvolume/nested/dir' ]
});
});
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
assert.end();
});
test('ensure only single container gets started', async assert => {
await compose.down({ cwd: path.join(__dirname), log: true });
await compose.upOne('alpine', { cwd: path.join(__dirname), log: true });

View file

@ -0,0 +1 @@
HI!