Skip to content

Commit

Permalink
test: Add exception test
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Jan 5, 2020
1 parent dd67832 commit 368f190
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
20 changes: 12 additions & 8 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ afterEach(() => {
delete process.env['INPUT_MDBOOK-VERSION'];
});

describe('run()', () => {
test('Integration testing (custom version)', async () => {
describe('Integration testing run()', () => {
test('should install custom version', async () => {
const testVersion: string = '0.3.4';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
const result: main.actionResult = await main.run();
expect(result.output).toMatch(`mdbook v${testVersion}`);
});

test('Integration testing (latest version)', async () => {
test('should install latest version', async () => {
const testVersion: string = 'latest';
process.env['INPUT_MDBOOK-VERSION'] = testVersion;
const result: main.actionResult = await main.run();
Expand All @@ -29,16 +29,20 @@ describe('run()', () => {
describe('showVersion()', () => {
let result: main.actionResult = {
exitcode: 0,
output: '',
error: ''
output: ''
};

test('Success case', async () => {
test('should return version', async () => {
result = await main.showVersion('git', ['--version']);
expect(result.exitcode).toBe(0);
expect(result.output).toMatch(/git version/);
});

// test('Failure case', async () => {
// });
test('should return exception', async () => {
try {
result = await main.showVersion('gitgit', ['--version']);
} catch (e) {
expect(e).toThrow(Error);
}
});
});
45 changes: 21 additions & 24 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,35 @@ import {installer} from './installer';
export interface actionResult {
exitcode: number;
output: string;
error: string;
}

export async function showVersion(
cmd: string,
args: string[]
): Promise<actionResult> {
let result: actionResult = {
exitcode: 0,
output: '',
error: ''
};
try {
let result: actionResult = {
exitcode: 0,
output: ''
};

const options = {
listeners: {
stdout: (data: Buffer) => {
result.output += data.toString();
},
stderr: (data: Buffer) => {
result.error += data.toString();
const options = {
listeners: {
stdout: (data: Buffer) => {
result.output += data.toString();
}
}
}
};
};

result.exitcode = await exec.exec(cmd, args, options);
core.debug(`
exit code: ${result.exitcode}
stdout: ${result.output}
stderr: ${result.error}
`);
return result;
result.exitcode = await exec.exec(cmd, args, options);
core.debug(`
exit code: ${result.exitcode}
stdout: ${result.output}
`);
return result;
} catch (e) {
return e;
}
}

// most @actions toolkit packages have async methods
Expand All @@ -47,8 +45,7 @@ export async function run(): Promise<any> {

let result: actionResult = {
exitcode: 0,
output: '',
error: ''
output: ''
};

if (toolVersion === '' || toolVersion === 'latest') {
Expand Down

0 comments on commit 368f190

Please sign in to comment.