Skip to content

Commit

Permalink
fix: error if file source is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Aug 21, 2022
1 parent 4d020ca commit f1ad86a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
74 changes: 39 additions & 35 deletions src/verifier/argumentMapper/arguments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logger from '../../logger';
import fs = require('fs');
import url = require('url');

import { FnMapping, FnValidationStatus } from './types';
import { InternalPactVerifierOptions } from '../types';
Expand Down Expand Up @@ -91,49 +92,52 @@ export const ffiFnMapping: FnMapping<
if (options.pactUrls) {
options.pactUrls.forEach((file) => {
logger.debug(`checking source type of given pactUrl: ${file}`);
try {
const u = new URL(file);

if (u.hostname) {
logger.debug(`adding ${file} as a Url source`);
ffi.pactffiVerifierUrlSource(
handle,
file,
options.pactBrokerUsername ||
process.env.PACT_BROKER_USERNAME ||
'',
options.pactBrokerPassword ||
process.env.PACT_BROKER_PASSWORD ||
'',
options.pactBrokerToken || process.env.PACT_BROKER_TOKEN || ''
);
}
} catch {
messages.push(`${file} is not a valid URL`);
}
if (/https?:/.test(url.parse(file).protocol || '')) {
try {
const u = new URL(file);

try {
const f = fs.lstatSync(file);
if (u.hostname) {
logger.debug(`adding ${file} as a Url source`);
ffi.pactffiVerifierUrlSource(
handle,
file,
options.pactBrokerUsername ||
process.env.PACT_BROKER_USERNAME ||
'',
options.pactBrokerPassword ||
process.env.PACT_BROKER_PASSWORD ||
'',
options.pactBrokerToken || process.env.PACT_BROKER_TOKEN || ''
);
}
} catch {
messages.push(`${file} is not a valid URL`);
}
} else {
try {
const f = fs.lstatSync(file);

if (f.isDirectory()) {
logger.debug(`adding ${file} as Directory source`);
ffi.pactffiVerifierAddDirectorySource(handle, file);
} else if (f.isFile() || f.isSymbolicLink()) {
logger.debug(`adding ${file} as File source`);
ffi.pactffiVerifierAddFileSource(handle, file);
if (f.isDirectory()) {
logger.debug(`adding ${file} as Directory source`);
ffi.pactffiVerifierAddDirectorySource(handle, file);
} else if (f.isFile() || f.isSymbolicLink()) {
logger.debug(`adding ${file} as File source`);
ffi.pactffiVerifierAddFileSource(handle, file);
}
} catch {
messages.push(
`'${file}' does not exist, or is not a file or directory`
);
}
} catch {
messages.push(
`'${file}' does not exist, or is not a file or directory`
);
}
});

return { status: FnValidationStatus.SUCCESS };
}
if (messages.length > 0) {
return { status: FnValidationStatus.FAIL, messages };
}

if (messages.length > 0) {
return { status: FnValidationStatus.FAIL, messages };
return { status: FnValidationStatus.SUCCESS };
}

return { status: FnValidationStatus.IGNORE };
Expand Down
9 changes: 5 additions & 4 deletions src/verifier/argumentMapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const setupVerification = (
const functionsToCall = invert(orderOfExecution);

order.map((k) => {
const validation = ffiFnMapping[functionsToCall[k]].validateAndExecute(
const fn = functionsToCall[k];
const validation = ffiFnMapping[fn].validateAndExecute(
ffi,
handle,
options
Expand All @@ -23,14 +24,14 @@ export const setupVerification = (
switch (validation.status) {
case FnValidationStatus.FAIL:
logErrorAndThrow(
`the required ffi function '${k}' failed validation with errors: ${
`the required ffi function '${fn}' failed validation with errors: ${
validation.messages || [].join(',')
}`
);
break;
case FnValidationStatus.IGNORE:
logger.debug(
`the optional ffi function '${k}' was not executed as it had non-fatal validation errors: ${
`the optional ffi function '${fn}' was not executed as it had non-fatal validation errors: ${
validation.messages || [].join(',')
}`
);
Expand All @@ -39,7 +40,7 @@ export const setupVerification = (
break;
default:
logCrashAndThrow(
`the ffi function '${k}' returned the following unrecognised validation signal: '${validation.status}'`
`the ffi function '${fn}' returned the following unrecognised validation signal: '${validation.status}'`
);
}
});
Expand Down

0 comments on commit f1ad86a

Please sign in to comment.