1
0
Fork 0
toolkit/packages/core/__tests__/lib.test.ts

93 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-05-16 20:40:21 +00:00
'use strict';
2019-05-17 14:23:39 +00:00
import * as core from '../src/core';
import * as os from 'os';
2019-05-16 20:40:21 +00:00
describe('@actions/core', () => {
2019-05-17 14:23:39 +00:00
beforeAll(() => {
// Set inputs
process.env['INPUT_MY_INPUT'] = 'val';
process.env['INPUT_MISSING'] = '';
process.env['INPUT_SPECIAL_CHARS_\'\t\"\\'] = '\'\t\"\\ repsonse ';
2019-05-16 21:16:39 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput gets non-required input', () => {
expect(core.getInput('my input')).toBe('val');
2019-05-16 21:16:39 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput gets required input', () => {
expect(core.getInput('my input', {required: true})).toBe('val');
2019-05-16 21:16:39 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput throws on missing required input', () => {
expect(() => core.getInput('missing', {required: true})).toThrow('Input required and not supplied: missing');
2019-05-16 21:16:39 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput doesnt throw on missing non-required input', () => {
expect(core.getInput('missing', {required: false})).toBe('');
2019-05-16 21:16:39 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput is case insensitive', () => {
expect(core.getInput('My InPuT')).toBe('val');
2019-05-16 20:40:21 +00:00
});
2019-05-17 14:23:39 +00:00
it('getInput handles special characters', () => {
expect(core.getInput('special chars_\'\t\"\\')).toBe('\'\t\"\\ repsonse');
});
it('setNeutral sets the correct exit code', () => {
core.setFailed('Failure message');
expect(process.exitCode).toBe(1);
});
2019-05-16 20:40:21 +00:00
2019-05-17 14:23:39 +00:00
it('setFailure sets the correct exit code and failure message', () => {
// Override stdout and append to output so that we capture the command that is sent
let output = '';
process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => {
output += p1;
return true;
}
2019-05-16 20:40:21 +00:00
2019-05-17 14:23:39 +00:00
core.setFailed('Failure message');
expect(process.exitCode).toBe(1);
expect(output).toBe('##[error]Failure message' + os.EOL);
});
it('error sets the correct error message', () => {
// Override stdout and append to output so that we capture the command that is sent
let output = '';
process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => {
output += p1;
return true;
}
core.error('Error message');
expect(output).toBe('##[error]Error message' + os.EOL);
});
it('warning sets the correct message', () => {
// Override stdout and append to output so that we capture the command that is sent
let output = '';
process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => {
output += p1;
return true;
}
core.warning('Warning');
expect(output).toBe('##[warning]Warning' + os.EOL);
});
it('debug sets the correct message', () => {
// Override stdout and append to output so that we capture the command that is sent
let output = '';
process.stdout.write = (p1: string | Buffer | Uint8Array, p2?: string | ((err?: Error) => void), p3?: (err?: Error) => void): boolean => {
output += p1;
return true;
}
core.debug('Debug');
expect(output).toBe('##[debug]Debug' + os.EOL);
});
2019-05-16 20:40:21 +00:00
2019-05-17 14:23:39 +00:00
// TODO - test escaping for all commands
});