Skip to content

Commit

Permalink
Merge pull request #220 from arareko/add-working-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
paambaati authored Sep 22, 2020
2 parents dce6623 + 5c19210 commit 73ef252
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This action requires that you set the [`CC_TEST_REPORTER_ID`](https://docs.codec
| Input | Default | Description |
| ------------------- | --------------- | ---------------------------------------------------------------------------------- |
| `coverageCommand` | `yarn coverage` | The actual command that should be executed to run your tests and capture coverage. |
| `workingDirectory` | | Specify a custom working directory where the coverage command should be executed. |
| `debug` | `false` | Enable Code Coverage debug output when set to `true`. |
| `coverageLocations` | | Locations to find code coverage as a multiline string.<br>Each line should be of the form `<location>:<type>`. See examples below.
| `prefix` | `undefined` | See [`--prefix`](https://docs.codeclimate.com/docs/configuring-test-coverage) |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ inputs:
required: false
description: 'Coverage command to execute'
default: 'yarn coverage'
workingDirectory:
required: false
description: 'Custom working directory for executing the coverage command'
default: ''
debug:
required: false
description: 'Enable debugging logs for the Code Climate test reporter'
Expand Down
30 changes: 25 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { platform } from 'os';
import { createWriteStream } from 'fs';
import { chdir } from 'process';
import fetch from 'node-fetch';
import { debug, error, setFailed, warning, info } from '@actions/core';
import { exec } from '@actions/exec';
Expand All @@ -11,6 +12,7 @@ import { getOptionalString } from './utils';
const DOWNLOAD_URL = `https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${platform()}-amd64`;
const EXECUTABLE = './cc-reporter';
const DEFAULT_COVERAGE_COMMAND = 'yarn coverage';
const DEFAULT_WORKING_DIRECTORY = '';
const DEFAULT_CODECLIMATE_DEBUG = 'false';
const DEFAULT_COVERAGE_LOCATIONS = '';

Expand Down Expand Up @@ -56,12 +58,25 @@ export function run(
downloadUrl: string = DOWNLOAD_URL,
executable: string = EXECUTABLE,
coverageCommand: string = DEFAULT_COVERAGE_COMMAND,
workingDirectory: string = DEFAULT_WORKING_DIRECTORY,
codeClimateDebug: string = DEFAULT_CODECLIMATE_DEBUG,
coverageLocationsParam: string = DEFAULT_COVERAGE_LOCATIONS,
coveragePrefix?: string
): Promise<void> {
return new Promise(async (resolve, reject) => {
let lastExitCode = 1;
if (workingDirectory) {
debug(`Changing working directory to: ${workingDirectory}`);
try {
chdir(workingDirectory);
lastExitCode = 0;
debug('✅ Changing working directory completed...');
} catch (err) {
error(err.message);
setFailed('🚨 Changing working directory failed!');
return reject(err);
}
}
try {
debug(`ℹ️ Downloading CC Reporter from ${downloadUrl} ...`);
await downloadToFile(downloadUrl, executable);
Expand All @@ -87,7 +102,7 @@ export function run(
}
debug('✅ CC Reporter before-build checkin completed...');
} catch (err) {
error(err);
error(err.message);
setFailed('🚨 CC Reporter before-build checkin failed!');
return reject(err);
}
Expand Down Expand Up @@ -153,7 +168,7 @@ export function run(
);
}
} catch (err) {
error(err);
error(err.message);
setFailed('🚨 CC Reporter coverage formatting failed!');
return reject(err);
}
Expand All @@ -178,7 +193,7 @@ export function run(
);
}
} catch (err) {
error(err);
error(err.message);
setFailed('🚨 CC Reporter coverage sum failed!');
return reject(err);
}
Expand All @@ -194,7 +209,7 @@ export function run(
debug('✅ CC Reporter upload coverage completed!');
return resolve();
} catch (err) {
error(err);
error(err.message);
setFailed('🚨 CC Reporter coverage upload failed!');
return reject(err);
}
Expand All @@ -212,7 +227,7 @@ export function run(
debug('✅ CC Reporter after-build checkin completed!');
return resolve();
} catch (err) {
error(err);
error(err.message);
setFailed('🚨 CC Reporter after-build checkin failed!');
return reject(err);
}
Expand All @@ -224,6 +239,10 @@ if ((require.main?.children?.length as number) < 1) {
'coverageCommand',
DEFAULT_COVERAGE_COMMAND
);
const workingDirectory = getOptionalString(
'workingDirectory',
DEFAULT_WORKING_DIRECTORY
);
const codeClimateDebug = getOptionalString(
'debug',
DEFAULT_CODECLIMATE_DEBUG
Expand All @@ -238,6 +257,7 @@ if ((require.main?.children?.length as number) < 1) {
DOWNLOAD_URL,
EXECUTABLE,
coverageCommand,
workingDirectory,
codeClimateDebug,
coverageLocations,
coveragePrefix
Expand Down

0 comments on commit 73ef252

Please sign in to comment.