|
| 1 | +import test from 'ava'; |
| 2 | +import {stub} from 'sinon'; |
| 3 | +import {WritableStreamBuffer} from 'stream-buffers'; |
| 4 | +import {addChannel} from '..'; |
| 5 | + |
| 6 | +test.beforeEach(t => { |
| 7 | + t.context.stdout = new WritableStreamBuffer(); |
| 8 | + t.context.stderr = new WritableStreamBuffer(); |
| 9 | + // Mock logger |
| 10 | + t.context.log = stub(); |
| 11 | + t.context.error = stub(); |
| 12 | + t.context.logger = {log: t.context.log, error: t.context.error}; |
| 13 | +}); |
| 14 | + |
| 15 | +test('Parse JSON returned by addChannel script', async t => { |
| 16 | + const pluginConfig = { |
| 17 | + addChannelCmd: |
| 18 | + './test/fixtures/echo-args.sh {\\"name\\": \\"Release name\\", \\"url\\": \\"https://host.com/release/1.0.0\\"}', |
| 19 | + }; |
| 20 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 21 | + |
| 22 | + const result = await addChannel(pluginConfig, context); |
| 23 | + t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'}); |
| 24 | +}); |
| 25 | + |
| 26 | +test('Return "undefined" if the addChannel script wrtite invalid JSON to stdout (with "publishCmd")', async t => { |
| 27 | + const pluginConfig = {addChannelCmd: './test/fixtures/echo-args.sh invalid_json'}; |
| 28 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 29 | + |
| 30 | + const result = await addChannel(pluginConfig, context); |
| 31 | + t.is(result, undefined); |
| 32 | +}); |
| 33 | + |
| 34 | +test('Return "undefined" if the addChannel script wrtite invalid JSON to stdout (with "cmd")', async t => { |
| 35 | + const pluginConfig = {cmd: './test/fixtures/echo-args.sh invalid_json'}; |
| 36 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 37 | + |
| 38 | + const result = await addChannel(pluginConfig, context); |
| 39 | + t.is(result, undefined); |
| 40 | +}); |
| 41 | + |
| 42 | +test('Return "undefined" if the addChannel script wrtite nothing to stdout', async t => { |
| 43 | + const pluginConfig = {addChannelCmd: './test/fixtures/echo-args.sh'}; |
| 44 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 45 | + |
| 46 | + const result = await addChannel(pluginConfig, context); |
| 47 | + t.is(result, undefined); |
| 48 | +}); |
| 49 | + |
| 50 | +test('Throw "Error" if the addChannel script does not returns 0', async t => { |
| 51 | + const pluginConfig = {addChannelCmd: 'exit 1'}; |
| 52 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger, options: {}}; |
| 53 | + |
| 54 | + await t.throwsAsync(addChannel(pluginConfig, context), Error); |
| 55 | +}); |
| 56 | + |
| 57 | +test('Use "cmd" if defined and "addChannelCmd" is not', async t => { |
| 58 | + const pluginConfig = { |
| 59 | + cmd: |
| 60 | + './test/fixtures/echo-args.sh {\\"name\\": \\"Release name\\", \\"url\\": \\"https://host.com/release/1.0.0\\"}', |
| 61 | + }; |
| 62 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 63 | + |
| 64 | + const result = await addChannel(pluginConfig, context); |
| 65 | + t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'}); |
| 66 | +}); |
| 67 | + |
| 68 | +test('Use "addChannelCmd" even if "cmd" is defined', async t => { |
| 69 | + const pluginConfig = { |
| 70 | + addChannelCmd: |
| 71 | + './test/fixtures/echo-args.sh {\\"name\\": \\"Release name\\", \\"url\\": \\"https://host.com/release/1.0.0\\"}', |
| 72 | + cmd: 'exit 1', |
| 73 | + }; |
| 74 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 75 | + |
| 76 | + const result = await addChannel(pluginConfig, context); |
| 77 | + t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'}); |
| 78 | +}); |
| 79 | + |
| 80 | +test('Return "false" if neither "addChannelCmd" nor "cmd" is defined', async t => { |
| 81 | + const pluginConfig = {}; |
| 82 | + const context = {stdout: t.context.stdout, stderr: t.context.stderr, logger: t.context.logger}; |
| 83 | + |
| 84 | + const result = await addChannel(pluginConfig, context); |
| 85 | + t.is(result, false); |
| 86 | +}); |
0 commit comments