UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

93 lines (92 loc) 4.91 kB
"use strict"; 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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeContract = void 0; const filenamify_1 = __importDefault(require("filenamify")); const slug_1 = __importDefault(require("slug")); const mkdirp_1 = require("mkdirp"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const entities_1 = require("../../entities"); const checkCurrentRunField = (context, configName) => { const maybeCaseContractConfig = context; const fieldName = `case:currentRun:context:${configName}`; if (fieldName in maybeCaseContractConfig) { const value = maybeCaseContractConfig[fieldName]; if (typeof value === 'string' && value !== '') { context.logger.maintainerDebug(`Validated config field '${fieldName}', accepted '${value}'`); return true; } context.logger.maintainerDebug(`Failed validation for config field '${fieldName}', failed '${value}'`); } else { context.logger.maintainerDebug(`Failed validation for config field '${fieldName}', not present in context`, context); } context.logger.error(`Missing configuration field '${configName}'. Please ensure it is set to a non-empty string.`); return false; }; const isCaseContractConfig = (context) => ['testRunId', 'contractDir'] .map((field) => checkCurrentRunField(context, field)) .reduce((acc, curr) => acc && curr, true); const EXTENSION = '.case.json'; const escapeFileName = (pathString) => (0, filenamify_1.default)(pathString); const makeFilename = (description, config) => escapeFileName(`${(0, slug_1.default)(`${description.consumerName}-${description.providerName}`)}-${config['case:currentRun:context:testRunId']}${EXTENSION}`); const makePath = (description, config) => path.join(config['case:currentRun:context:contractDir'], makeFilename(description, config)); const writeContract = (contract, context) => { if (!isCaseContractConfig(context)) { throw new entities_1.CaseConfigurationError('Unable to write contract without required configuration options set. See the error logs for more information.'); } if (context['case:currentRun:context:contractFilename'] !== undefined && context['case:currentRun:context:contractDir'] !== context['case:currentRun:context:defaultConfig']['contractDir']) { context.logger.warn('Both contractFilename and contractDir have been specified, but you should only set one of these when writing a contract.'); context.logger.warn(`Ignoring contractDir setting, and writing to file at: ${context['case:currentRun:context:contractFilename']}`); } const pathToFile = context['case:currentRun:context:contractFilename'] ? context['case:currentRun:context:contractFilename'] : makePath(contract.description, context); context.logger.maintainerDebug(`About to write contract to '${pathToFile}'`); const dirName = path.dirname(pathToFile); if (!fs.existsSync(dirName)) { context.logger.maintainerDebug(`Creating directory ${dirName}`); try { mkdirp_1.mkdirp.sync(dirName); } catch (e) { throw new entities_1.CaseConfigurationError(`Failed trying to create contract directory '${dirName}': ${e.message}`); } } if (fs.existsSync(pathToFile) && !context['case:currentRun:context:overwriteFile']) { context.logger.error(`Can't write to '${pathToFile}, as it already exists'`); throw new entities_1.CaseConfigurationError(`The file ${pathToFile} already exists`); } fs.writeFileSync(pathToFile, JSON.stringify(contract, undefined, 2)); return pathToFile; }; exports.writeContract = writeContract;