@case-contract-testing/case
Version:
Next-generation contract testing suite
109 lines (108 loc) • 5.08 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeContractStore = exports.readContract = void 0;
const path = __importStar(require("node:path"));
const fs = __importStar(require("node:fs"));
const entities_1 = require("../../entities");
const readContract = (pathToContract) => {
let content = '';
try {
content = fs.readFileSync(pathToContract, 'utf-8');
}
catch (e) {
throw new entities_1.CaseConfigurationError(`Unable to load contract file from disk at '${pathToContract}': ${e.message}`);
}
let contract;
try {
contract = JSON.parse(content);
}
catch (e) {
throw new entities_1.CaseConfigurationError(`Unable to parse contract file from disk at '${pathToContract}': ${e.message}`);
}
return contract;
};
exports.readContract = readContract;
const readContractsFromDir = (pathToDir, context) => {
if (!fs.existsSync(pathToDir)) {
throw new entities_1.CaseConfigurationError(`The directory '${pathToDir}' does not seem to exist, so can't read contracts from it`);
}
if (!fs.statSync(pathToDir).isDirectory()) {
throw new entities_1.CaseConfigurationError(`'${pathToDir}' exists but is a not a directory, so can't read contracts from it`);
}
const files = fs.readdirSync(pathToDir);
const buffers = files
.map((file) => {
const filePath = path.join(pathToDir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return null;
}
let contents;
try {
contents = fs.readFileSync(filePath);
}
catch (e) {
throw new entities_1.CaseConfigurationError(`Unable to load contract file from disk at '${filePath}': ${e.message}\n\n.This is almost certainly a race condition where the files were deleted during the directory read.`);
}
return { contents, filePath };
})
.filter((f) => f !== null);
const jsonContracts = buffers
.filter(Boolean)
.map((l) => ({ contents: l.contents.toString(), filePath: l.filePath }))
.map((s) => {
try {
return { contents: JSON.parse(s.contents), filePath: s.filePath };
}
catch (e) {
context.logger.warn(`Failed (${e.message}) while parsing '${s.filePath}' as json.\n\nThis may be a configuration error. The best practice is to:\n\n * Keep the contract directory clear of non-contract files\n * Remove all files in this directory before downloading contracts`);
return { contents: null, filePath: s.filePath };
}
})
.filter((u) => u.contents !== null);
jsonContracts
.filter((item) => item.contents.contractType !== 'case::contract')
.forEach((item) => {
context.logger.debug(`Skipping ${item.filePath} because it is not a case contract (incorrect contractType value of '${item.contents.contractType}')`);
});
const caseContracts = jsonContracts.filter((item) => item.contents.contractType === 'case::contract');
caseContracts
.filter((item) => typeof item.contents.description?.providerName !== 'string' ||
typeof item.contents.description?.consumerName !== 'string')
.forEach((item) => {
context.logger.warn(`Skipping ${item.filePath} because it must have a description.consumerName and description.providerName (was '${item.contents.description?.consumerName}' and '${item.contents.description?.providerName}' respectively)`);
});
return caseContracts.filter((item) => typeof item.contents.description?.providerName === 'string' &&
typeof item.contents.description?.consumerName === 'string');
};
const makeContractStore = (context) => ({
readContract: (pathToContract) => ({
contents: (0, exports.readContract)(pathToContract),
filePath: pathToContract,
}),
readContractsFromDir: (pathToDir) => readContractsFromDir(pathToDir, context),
});
exports.makeContractStore = makeContractStore;