UNPKG

@diullei/codeguardian

Version:

Open-source developer tool to validate and enforce architectural rules, especially for AI-generated code

72 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssertCommandOutputRule = void 0; const core_1 = require("../core"); class AssertCommandOutputRule extends core_1.AssertionRule { target; pattern; condition; value; firstLines; lastLines; shouldMatch; constructor(id, target, pattern, condition, value, firstLines, lastLines, shouldMatch = true) { super(id); this.target = target; this.pattern = pattern; this.condition = condition; this.value = value; this.firstLines = firstLines; this.lastLines = lastLines; this.shouldMatch = shouldMatch; if (this.firstLines && this.lastLines) { throw new Error('Cannot use firstLines and lastLines simultaneously.'); } } async assert(item, _context) { const commandOutput = item; if (!commandOutput || typeof commandOutput.exitCode !== 'number') { return false; } switch (this.target) { case 'exitCode': return this.assertExitCode(commandOutput.exitCode); case 'stdout': case 'stderr': return this.assertOutputText(commandOutput[this.target]); default: return false; } } assertExitCode(exitCode) { if (this.condition === undefined || this.value === undefined) { throw new Error('Asserting against exitCode requires "condition" and "value"'); } switch (this.condition) { case '==': return exitCode === this.value; case '!=': return exitCode !== this.value; case '>': return exitCode > this.value; case '>=': return exitCode >= this.value; case '<': return exitCode < this.value; case '<=': return exitCode <= this.value; default: return false; } } assertOutputText(text) { if (!this.pattern) { throw new Error('Asserting against stdout/stderr requires a "pattern"'); } let targetText = text; const lines = text.split('\n'); if (this.firstLines) { targetText = lines.slice(0, this.firstLines).join('\n'); } else if (this.lastLines) { targetText = lines.slice(-this.lastLines).join('\n'); } const matches = this.pattern.test(targetText); return this.shouldMatch ? matches : !matches; } } exports.AssertCommandOutputRule = AssertCommandOutputRule; //# sourceMappingURL=AssertCommandOutputRule.js.map