Skip to content

Commit

Permalink
chore: fixing monorepo use case (#50)
Browse files Browse the repository at this point in the history
* chore: fixing monorepo use case

* chore: fixing types
  • Loading branch information
jpb06 authored Feb 22, 2023
1 parent 03b67a7 commit 2101eb2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/logic/jest/doBadgesExist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { doBadgesExist } from './doBadgesExist';
jest.mock('fs-extra');

describe('doBadgesExist function', () => {
const outputPath = './badges';

it('should return false if one file does not exist', async () => {
jest
.mocked(pathExists)
Expand All @@ -14,7 +16,7 @@ describe('doBadgesExist function', () => {
.mockImplementationOnce(() => true as never)
.mockImplementationOnce(() => true as never);

const result = await doBadgesExist();
const result = await doBadgesExist(outputPath);

expect(result).toBe(false);
});
Expand All @@ -28,7 +30,7 @@ describe('doBadgesExist function', () => {
.mockImplementationOnce(() => true as never)
.mockImplementationOnce(() => true as never);

const result = await doBadgesExist();
const result = await doBadgesExist(outputPath);

expect(result).toBe(true);
});
Expand Down
4 changes: 1 addition & 3 deletions src/logic/jest/doBadgesExist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { pathExists } from 'fs-extra';

export const doBadgesExist = async (
outputPath = './badges',
): Promise<boolean> => {
export const doBadgesExist = async (outputPath: string): Promise<boolean> => {
const files = [
'coverage-branches.svg',
'coverage-functions.svg',
Expand Down
8 changes: 5 additions & 3 deletions src/logic/jest/hasCoverageEvolved.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import { hasCoverageEvolved } from './hasCoverageEvolved';
jest.mock('@actions/exec');

describe('hasCoverageEvolved function', () => {
const outputPath = './badges';

it('should return true if coverage folder does not exist', async () => {
const result = await hasCoverageEvolved(false);
const result = await hasCoverageEvolved(false, outputPath);

expect(result).toBe(true);
});

it('should return true if diff returns one', async () => {
jest.mocked(exec).mockResolvedValueOnce(1);

const result = await hasCoverageEvolved(true);
const result = await hasCoverageEvolved(true, outputPath);

expect(result).toBe(true);
});

it('should return false if diff returns zero', async () => {
jest.mocked(exec).mockResolvedValueOnce(0);

const result = await hasCoverageEvolved(true);
const result = await hasCoverageEvolved(true, outputPath);

expect(result).toBe(false);
});
Expand Down
3 changes: 2 additions & 1 deletion src/logic/jest/hasCoverageEvolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { exec } from '@actions/exec';

export const hasCoverageEvolved = async (
badgesExist: boolean,
outputPath: string,
): Promise<boolean> => {
if (!badgesExist) {
return true;
}

const code = await exec('git diff', ['--quiet', './badges/*'], {
const code = await exec('git diff', ['--quiet', `${outputPath}/*`], {
ignoreReturnCode: true,
});

Expand Down
9 changes: 6 additions & 3 deletions src/workflow/actionWorkflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,23 @@ describe('actionWorkflow function', () => {
});

it('should generate badges from the default summary path', async () => {
const summaryPath = './coverage/coverage-summary.json';
const outputPath = './badges';

jest.mocked(isBranchValidForBadgesGeneration).mockReturnValueOnce(true);
jest.mocked(isJestCoverageReportAvailable).mockResolvedValueOnce(true);
jest.mocked(doBadgesExist).mockResolvedValueOnce(true);
jest.mocked(hasCoverageEvolved).mockResolvedValueOnce(true);
jest
.mocked(getInput)
.mockReturnValueOnce('')
.mockReturnValueOnce('')
.mockReturnValueOnce('');
.mockReturnValueOnce(summaryPath)
.mockReturnValueOnce(outputPath);

await actionWorkflow();

expect(generateBadges).toHaveBeenCalledTimes(1);
expect(generateBadges).toHaveBeenCalledWith(undefined);
expect(generateBadges).toHaveBeenCalledWith(summaryPath, outputPath);
expect(setGitConfig).toHaveBeenCalledTimes(1);
expect(pushBadges).toHaveBeenCalledTimes(1);

Expand Down
15 changes: 6 additions & 9 deletions src/workflow/actionWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,22 @@ export const actionWorkflow = async (): Promise<void> => {
const summaryPathInput = getInput('coverage-summary-path');
const summaryPath = summaryPathInput === '' ? undefined : summaryPathInput;

const outputPath = getInput('output-folder');
// this must be checked before generating badges (duh!)
const badgesExist = await doBadgesExist(outputPath);

info(
`🔶 Generating badges from ${
summaryPath ? summaryPath : 'default coverage summary path'
}`,
);
const outputPath = getInput('output-folder');
if (outputPath === '') {
await generateBadges(summaryPath);
} else {
await generateBadges(summaryPath, outputPath);
}
await generateBadges(summaryPath, outputPath);

if (!shouldCommit) {
return info("🔶 `no-commit` set to true: badges won't be committed");
}

const badgesExist = await doBadgesExist(outputPath);

const hasEvolved = await hasCoverageEvolved(badgesExist);
const hasEvolved = await hasCoverageEvolved(badgesExist, outputPath);
if (!hasEvolved) {
return info('🔶 Coverage has not evolved, no action required.');
}
Expand Down

0 comments on commit 2101eb2

Please sign in to comment.