@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
86 lines • 3.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const test_utils_1 = require("@sprucelabs/test-utils");
const duration_utility_1 = __importDefault(require("../../utilities/duration.utility"));
require('dotenv').config();
function hasArg(regex) {
return !!process.argv?.find((arg) => arg.search(regex) > -1);
}
const testUtil = {
shouldClearCache() {
return hasArg(/clear.*?cache/gi);
},
isCacheEnabled() {
return !hasArg(/no.*?cache/gi);
},
resolveTestDir(cacheKey = '') {
return spruce_skill_utils_1.diskUtil.resolvePath(this.getTestRootDir(), 'spruce-cli', cacheKey);
},
shouldCleanupTestSkillDirs() {
return process.env.CLEANUP_TEST_SKILL_DIRS !== 'false';
},
shouldCleanupAfterEach() {
return (this.shouldCleanupTestSkillDirs() &&
process.env.CLEANUP_TEST_SKILL_DIRS_ON === 'afterEach');
},
shouldCleanupAfterAll() {
return (this.shouldCleanupTestSkillDirs() &&
process.env.CLEANUP_TEST_SKILL_DIRS_ON !== 'afterEach');
},
getTestRootDir() {
return process.env.TEST_CACHE_ROOT_DIR ?? path_1.default.join(os_1.default.tmpdir());
},
assertCountsByAction(files, options) {
const generated = (files ?? []).filter((f) => f.action === 'generated').length;
const updated = (files ?? []).filter((f) => f.action === 'updated').length;
const skipped = (files ?? []).filter((f) => f.action === 'skipped').length;
test_utils_1.assert.isEqual(generated, options.generated, `Generated the wrong number of files. Got ${generated} but expected ${options.generated}.`);
test_utils_1.assert.isEqual(updated, options.updated, `Updated the wrong number of files. Got ${updated} but expected ${options.updated}.`);
test_utils_1.assert.isEqual(skipped, options.skipped, `Skipped the wrong number of files. Got ${skipped} but expected ${options.skipped}.`);
},
assertFileByNameInGeneratedFiles(name, files) {
const file = (files ?? []).find((f) => f.name.search(name) > -1)?.path;
test_utils_1.assert.isTruthy(file, `file named '${name}' not found in generated files.\n\n${JSON.stringify(files ?? [], null, 2)}`);
return file;
},
assertFileByPathInGeneratedFiles(path, files) {
const file = (files ?? []).find((f) => f.path.search(path) > -1)?.path;
test_utils_1.assert.isTruthy(file, `file with path '${path}' not found in generated files.\n\n${JSON.stringify(files ?? [], null, 2)}`);
return file;
},
startTime: 0,
startLogTimer() {
this.startTime = new Date().getTime();
},
log(...args) {
//@ts-ignore
const prefix = global.activeTest
? //@ts-ignore
`${global.activeTest?.file}::${global.activeTest?.test} :: `
: '';
if (process.env.TEST_LOG_FILTER) {
const match = prefix.search(new RegExp(process.env.TEST_LOG_FILTER, 'igm'));
if (match < 0) {
return;
}
}
process.stderr.write(prefix + this.getTimeSpentFormatted() + ': ' + args.join(' '));
},
getTimeSpent() {
const now = new Date().getTime();
const delta = now - this.startTime;
return delta;
},
getTimeSpentFormatted() {
const spent = this.getTimeSpent();
return duration_utility_1.default.msToFriendly(spent);
},
};
exports.default = testUtil;
//# sourceMappingURL=test.utility.js.map