Skip to content

Commit

Permalink
exit one and error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim committed Sep 25, 2023
1 parent 4595a15 commit 091632f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
10 changes: 8 additions & 2 deletions projects/optic/src/commands/capture/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ const getCaptureAction =
);

if (!captureOutput) {
process.exitCode = 1;
return;
}
const {
unmatchedInteractions,
totalInteractions,
coverage,
endpointsAdded,
hasAnyDiffs,
endpointCounts,
} = captureOutput;

Expand Down Expand Up @@ -332,6 +334,9 @@ const getCaptureAction =
}. View your spec at ${specUrl}`
);
}
if (hasAnyDiffs) {
process.exitCode = 1;
}

if (
unmatchedInteractions &&
Expand Down Expand Up @@ -442,12 +447,12 @@ export async function processCaptures(
? bufferedOutput.push(JSON.stringify(captureConfig?.requests?.send))
: logger.error(captureConfig?.requests?.send);
}
process.exitCode = 1;
return;
}

// update existing endpoints
const coverage = new ApiCoverageCounter(spec.jsonLike);
let hasAnyDiffs = false;
let diffCount = 0;
let endpointsAdded = 0;
// Handle interactions for documented endpoints first
Expand Down Expand Up @@ -492,7 +497,7 @@ export async function processCaptures(
if (!hasDiffs) {
spinner?.succeed(endpointText);
} else {
process.exitCode = 1;
hasAnyDiffs = true;
spinner?.fail(endpointText);
}
}
Expand Down Expand Up @@ -609,5 +614,6 @@ export async function processCaptures(
endpointsAdded,
endpointCounts,
bufferedOutput,
hasAnyDiffs,
};
}
52 changes: 39 additions & 13 deletions projects/optic/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { GroupedCaptures } from './capture/interactions/grouped-interactions';
import { getCaptureStorage } from './capture/storage';
import { captureRequestsFromProxy } from './capture/actions/captureRequests';
import { processCaptures } from './capture/capture';
import { uploadCoverage } from './capture/actions/upload-coverage';

const usage = () => `
Expand Down Expand Up @@ -220,7 +221,12 @@ type SpecReport = {
title?: string;
error?: string;
changelogLink?: string;
capture?: {};
capture?: {
bufferedOutput: string[];
unmatchedInteractions: number;
hasAnyDiffs: boolean;
coverage: string;
};
diffs?: number;
endpoints?: {
added: number;
Expand Down Expand Up @@ -254,6 +260,19 @@ function report(report: SpecReport) {
if (report.changelogLink) {
logger.info(`| View report: ${report.changelogLink}`);
}
if (report.capture) {
const { bufferedOutput, coverage, unmatchedInteractions } = report.capture;
logger.info(
`| ${coverage}% API test coverage ${
unmatchedInteractions
? `(${unmatchedInteractions} unmatched interactions)`
: ''
}`
);
bufferedOutput.forEach((output) => {
logger.info(`| ` + output);
});
}
logger.info('');
}

Expand Down Expand Up @@ -541,19 +560,21 @@ export const getRunAction =
verbose: false,
}
);
if (!captureResults) continue;
const {
unmatchedInteractions,
totalInteractions,
coverage,
endpointsAdded,
endpointCounts,
bufferedOutput,
} = captureResults;
if (!captureResults) {
process.exitCode = 1;
continue;
}
const { unmatchedInteractions, hasAnyDiffs, coverage, bufferedOutput } =
captureResults;

// TODO add this to spec report somehow
specReport.capture = {
bufferedOutput,
coverage: String(coverage.calculateCoverage().percent),
unmatchedInteractions,
hasAnyDiffs,
};

// TODO add upload here
await uploadCoverage(localSpec, coverage, specDetails, config);
}

let upload: Awaited<ReturnType<typeof uploadDiff>>;
Expand Down Expand Up @@ -639,7 +660,12 @@ export const getRunAction =

const hasFailures =
allChecks.some((checks) => checks.failed.error > 0) ||
specReports.some((r) => r.error);
specReports.some((r) => r.error) ||
specReports.some(
(r) =>
r.capture &&
(r.capture.unmatchedInteractions > 0 || r.capture.hasAnyDiffs)
);

const exit1 = options.severity === 'error' && hasFailures;

Expand Down

0 comments on commit 091632f

Please sign in to comment.