Skip to content

Commit

Permalink
Improve tests (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest authored Jan 10, 2023
1 parent 15377a9 commit 88adafa
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/having-a-question-.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ assignees: ''

---

Please ask questions in the [Discussions](https://github.com/UnlyEd/github-action-await-vercel/discussions)! :)
Please ask questions in the [Discussions](https://github.com/UnlyEd/github-action-store-variable/discussions)! :)
2 changes: 2 additions & 0 deletions .github/workflows/auto-git-release-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ jobs:
files: |
CHANGELOG.md
# TODO les versions majeures ne sont pas màj
# la version dans GHA marketplace est pas màj
- name: Updating Git release tag for the major "v${{steps.next_semantic_version.outputs.major}}" version
uses: softprops/action-gh-release@v1
with: # See https://github.com/softprops/action-gh-release#-customizing
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ jobs:
- run: |
yarn
yarn build:once
yarn test:once
yarn lint
yarn format:preview
49 changes: 39 additions & 10 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function exec_lib(options: cp.ExecFileSyncOptions): string {
/**
* Path of the compiled version of the Action file entrypoint.
*
* @example .../github-action-await-vercel/lib/main.js
* @example .../github-action-store-variable/lib/main.js
*/
const mainFilePath = path.join(__dirname, '..', BUILD_DIR, BUILD_MAIN_FILENAME);

Expand All @@ -48,26 +48,55 @@ function exec_lib(options: cp.ExecFileSyncOptions): string {
}

describe('Functional test', () => {
const CORRECT_DOMAIN = `${process.env.VERCEL_DOMAIN}`;
const WRONG_DOMAIN = 'i-am-wrong.vercel.app';

describe('should pass when', () => {
beforeEach(() => {
// @ts-ignore
global.console = global.unmuteConsole();
});

describe('using special delimiter', () => {
describe('when storing variables using a special delimiter', () => {
const options: cp.ExecFileSyncOptions = {
env: {
INPUT_VARIABLES: 'VAR=TEST,OTHER_VAR=OTHER_TEST,RETRIEVE',
INPUT_VARIABLES: 'VAR=TEST,OTHER_VAR=OTHER_TEST,UNKNOWN_VAR',
INPUT_DELIMITER: ',',
},
};
const filteredContent = exec_lib(options);
test('test', () => {
expect(filteredContent.includes(',')).toBe(true);
console.log(filteredContent);
const output = exec_lib(options);
console.log('output:\n', output);

test('output should display all received variables', () => {
expect(output.includes('::debug::Received variables: VAR=TEST,OTHER_VAR=OTHER_TEST,UNKNOWN_VAR')).toBe(true);
});

test('output should display used delimiter', () => {
expect(output.includes('::debug::Using delimiter: ","')).toBe(true);
});

test('output should display warning about UNKNOWN_VAR not being found', () => {
expect(output.includes('::warning::Cannot retrieve variable UNKNOWN_VAR')).toBe(true);
});
});

describe('when storing variables', () => {
const options: cp.ExecFileSyncOptions = {
env: {
INPUT_VARIABLES: 'VAR1=1,VAR2=SOME_STRING',
INPUT_DELIMITER: ',',
},
};
const output = exec_lib(options);
console.log('output:\n', output);

test('output should display all received variables', () => {
expect(output.includes('::debug::Received variables: VAR1=1,VAR2=SOME_STRING')).toBe(true);
});

test('output should display used delimiter', () => {
expect(output.includes('::debug::Using delimiter: ","')).toBe(true);
});

test('output should NOT display warning about UNKNOWN_VAR not being found (because we did not try to read it)', () => {
expect(output.includes('::warning::Cannot retrieve variable UNKNOWN_VAR')).toBe(false);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion github-action-runtime/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion github-action-runtime/index.js.map

Large diffs are not rendered by default.

16 changes: 2 additions & 14 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const manageArtifacts_1 = __importDefault(require("./manageArtifacts"));
/**
* Runs configuration checks to make sure everything is properly configured.
* If anything isn't properly configured, will stop the workflow.
*/
const runConfigChecks = () => {
if (!process.env.VERCEL_TOKEN) {
const message = process.env.NODE_ENV === 'test'
? `VERCEL_TOKEN environment variable is not defined. Please define it in the ".env.test" file. See https://vercel.com/account/tokens`
: `VERCEL_TOKEN environment variable is not defined. Please create a GitHub "VERCEL_TOKEN" secret. See https://vercel.com/account/tokens`;
core.setFailed(message);
throw new Error(message);
}
};
/**
* Runs the GitHub Action.
*/
const run = () => __awaiter(void 0, void 0, void 0, function* () {
if (!core.isDebug()) {
core.info('Debug mode is disabled. Read more at https://github.com/UnlyEd/github-action-await-vercel#how-to-enable-debug-logs');
core.info('Debug mode is disabled. Read more at https://github.com/UnlyEd/github-action-store-variable#how-to-enable-debug-logs');
}
try {
const variables = core.getInput('variables');
const delimiter = core.getInput('delimiter');
const failIfNotFound = core.getInput('failIfNotFound') == 'true';
core.debug(`Received variables: ${variables}`); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true https://github.com/actions/toolkit/blob/master/docs/action-debugging.md#how-to-access-step-debug-logs
core.debug(`Using delimiter: "${delimiter}"`);
core.debug(`Using failIfNotFound: "${failIfNotFound}"`);
yield (0, manageArtifacts_1.default)(variables, delimiter, failIfNotFound);
}
catch (error) {
Expand Down
17 changes: 14 additions & 3 deletions lib/manageArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const fs_1 = require("fs");
const core = __importStar(require("@actions/core"));
const config_1 = require("./config");
const rimraf_1 = __importDefault(require("rimraf"));
// eslint-disable-next-line @typescript-eslint/no-var-requires
const artifact = require('@actions/artifact');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const io = require('@actions/io');
const defineVariableOperation = (variable) => {
try {
Expand Down Expand Up @@ -138,8 +140,17 @@ const manageArtifacts = (variables, delimiter, failIfNotFound) => __awaiter(void
console.log(error);
}
}
yield storeArtifact(variablesDetail.filter((variable) => variable.operationToProceed === 0).map((variable) => variable.detail), failIfNotFound);
yield retrieveArtifact(variablesDetail.filter((variable) => variable.operationToProceed === 1).map((variable) => variable.detail), failIfNotFound);
const variablesResult = variablesDetail.reduce((variablesObject, variableToExport) => (Object.assign(Object.assign({}, variablesObject), { [variableToExport.detail.key]: variableToExport.detail.value })), {});
const artifactToStore = variablesDetail
.filter((variable) => variable.operationToProceed === 0)
.map((variable) => variable.detail);
core.debug(`Artifact to store: ${JSON.stringify(artifactToStore)}`);
yield storeArtifact(artifactToStore, failIfNotFound);
const artifactToRetrieve = variablesDetail
.filter((variable) => variable.operationToProceed === 1)
.map((variable) => variable.detail);
core.debug(`Artifact to retrieve: ${JSON.stringify(artifactToStore)}`);
yield retrieveArtifact(artifactToRetrieve, failIfNotFound);
const result = variablesDetail.reduce((variablesObject, variableToExport) => (Object.assign(Object.assign({}, variablesObject), { [variableToExport.detail.key]: variableToExport.detail.value })), {});
core.debug(`result: ${JSON.stringify(result)}`);
});
exports.default = manageArtifacts;
19 changes: 3 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
import * as core from '@actions/core';
import manageArtifacts from './manageArtifacts';

/**
* Runs configuration checks to make sure everything is properly configured.
* If anything isn't properly configured, will stop the workflow.
*/
const runConfigChecks = () => {
if (!process.env.VERCEL_TOKEN) {
const message =
process.env.NODE_ENV === 'test'
? `VERCEL_TOKEN environment variable is not defined. Please define it in the ".env.test" file. See https://vercel.com/account/tokens`
: `VERCEL_TOKEN environment variable is not defined. Please create a GitHub "VERCEL_TOKEN" secret. See https://vercel.com/account/tokens`;
core.setFailed(message);
throw new Error(message);
}
};

/**
* Runs the GitHub Action.
*/
const run = async (): Promise<void> => {
if (!core.isDebug()) {
core.info('Debug mode is disabled. Read more at https://github.com/UnlyEd/github-action-await-vercel#how-to-enable-debug-logs');
core.info('Debug mode is disabled. Read more at https://github.com/UnlyEd/github-action-store-variable#how-to-enable-debug-logs');
}

try {
Expand All @@ -30,6 +15,8 @@ const run = async (): Promise<void> => {
const failIfNotFound: boolean = core.getInput('failIfNotFound') == 'true';
core.debug(`Received variables: ${variables}`); // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true https://github.com/actions/toolkit/blob/master/docs/action-debugging.md#how-to-access-step-debug-logs
core.debug(`Using delimiter: "${delimiter}"`);
core.debug(`Using failIfNotFound: "${failIfNotFound}"`);

await manageArtifacts(variables, delimiter, failIfNotFound);
} catch (error) {
core.setFailed(error.message);
Expand Down
29 changes: 18 additions & 11 deletions src/manageArtifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import rimraf from 'rimraf';
import { ArtifactClient, UploadOptions } from '@actions/artifact';
import { UploadResponse } from '@actions/artifact/lib/internal/upload-response';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const artifact = require('@actions/artifact');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const io = require('@actions/io');

const defineVariableOperation = (variable: string): VariableStatus => {
Expand Down Expand Up @@ -64,7 +66,7 @@ const storeArtifact = async (variables: VariableDetail[], failIfNotFound: boolea
core.debug(`Imported ${variable.key}=${variable.value} and exported it back as ENV var`);
}
} catch (error) {
const message: string = `Error while uploading artifact: ${error?.message}`;
const message = `Error while uploading artifact: ${error?.message}`;
if (failIfNotFound) {
core.setFailed(message);
} else {
Expand All @@ -86,7 +88,7 @@ const retrieveArtifact = async (variables: VariableDetail[], failIfNotFound: boo
core.exportVariable(variable.key, variable.value);
core.debug(`Exported ${variable.key}=${variable.value} as ENV var`);
} catch (error) {
const message: string = `Cannot retrieve variable ${variable.key}`;
const message = `Cannot retrieve variable ${variable.key}`;
if (failIfNotFound) {
core.setFailed(message);
} else {
Expand All @@ -106,22 +108,27 @@ const manageArtifacts = async (variables: string, delimiter: string, failIfNotFo
console.log(error);
}
}
await storeArtifact(
variablesDetail.filter((variable: VariableStatus) => variable.operationToProceed === 0).map((variable: VariableStatus) => variable.detail),
failIfNotFound,
);
await retrieveArtifact(
variablesDetail.filter((variable: VariableStatus) => variable.operationToProceed === 1).map((variable: VariableStatus) => variable.detail),
failIfNotFound,
);

const variablesResult = variablesDetail.reduce(
const artifactToStore = variablesDetail
.filter((variable: VariableStatus) => variable.operationToProceed === 0)
.map((variable: VariableStatus) => variable.detail);
core.debug(`Artifact to store: ${JSON.stringify(artifactToStore)}`);
await storeArtifact(artifactToStore, failIfNotFound);

const artifactToRetrieve = variablesDetail
.filter((variable: VariableStatus) => variable.operationToProceed === 1)
.map((variable: VariableStatus) => variable.detail);
core.debug(`Artifact to retrieve: ${JSON.stringify(artifactToStore)}`);
await retrieveArtifact(artifactToRetrieve, failIfNotFound);

const result = variablesDetail.reduce(
(variablesObject, variableToExport) => ({
...variablesObject,
[variableToExport.detail.key]: variableToExport.detail.value,
}),
{},
);
core.debug(`result: ${JSON.stringify(result)}`);
};

export default manageArtifacts;

0 comments on commit 88adafa

Please sign in to comment.