@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
304 lines • 12.3 kB
JavaScript
"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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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 });
const test_utils_1 = __importStar(require("@sprucelabs/test-utils"));
const LintService_1 = __importDefault(require("../../../../services/LintService"));
const StaticTestFinder_1 = __importDefault(require("../../../../tests/staticToInstanceMigration/StaticTestFinder"));
const StaticToInstanceMigrator_1 = __importDefault(require("../../../../tests/staticToInstanceMigration/StaticToInstanceMigrator"));
const StaticToInstanceTestFileMigrator_1 = __importDefault(require("../../../../tests/staticToInstanceMigration/StaticToInstanceTestFileMigrator"));
let StaticToInstanceMigratorTest = class StaticToInstanceMigratorTest extends test_utils_1.default {
testFinder;
testFileMigrator;
migrator;
readFiles = [];
fakedFileContents = {};
lastWrittenFile;
spyCommandService;
constructor() {
super();
StaticTestFinder_1.default.Class = FakeStaticTestFinder;
StaticToInstanceTestFileMigrator_1.default.Class = FakeFileMigrator;
StaticToInstanceMigrator_1.default.diskUtil.readFile = (source) => {
this.readFiles.push(source);
return this.fakedFileContents[source] ?? (0, test_utils_1.generateId)();
};
StaticToInstanceMigrator_1.default.diskUtil.writeFile = (destination, contents) => {
this.lastWrittenFile = {
destination,
contents,
};
};
this.testFinder = StaticTestFinder_1.default.Finder();
this.testFileMigrator =
StaticToInstanceTestFileMigrator_1.default.Migrator();
this.spyCommandService = new SpyCommandService();
const lintService = new LintService_1.default(this.cwd, () => this.spyCommandService);
this.migrator = StaticToInstanceMigrator_1.default.Migrator({
testFinder: this.testFinder,
testFileMigrator: this.testFileMigrator,
lintService,
});
}
throwsWithMissing() {
const err = test_utils_1.assert.doesThrow(() =>
//@ts-ignore
StaticToInstanceMigrator_1.default.Migrator());
test_utils_1.errorAssert.assertError(err, 'MISSING_PARAMETERS', {
parameters: ['testFinder', 'testFileMigrator', 'lintService'],
});
}
async runThrowsWithMissing() {
//@ts-ignore
const err = await test_utils_1.assert.doesThrowAsync(() => this.migrator.run());
test_utils_1.errorAssert.assertError(err, 'MISSING_PARAMETERS', {
parameters: ['lookupDir'],
});
}
async passesLookupDirToTestFinder() {
const path = (0, test_utils_1.generateId)();
await this.run(path);
test_utils_1.assert.isEqual(this.testFinder.lastLookupDir, path);
}
async returnsStatsBasedOnTestsFound(results) {
this.setFakedFinderResults(results);
const stats = await this.run();
test_utils_1.assert.isEqualDeep(stats, {
totalTestsUpdated: this.testFinder.fakedResults.length,
totalTestsSkipped: 0,
});
}
async passesOneFilesContentsToMigrator(filename) {
this.setFakedFinderResults([filename]);
await this.run();
test_utils_1.assert.isEqualDeep(this.readFiles, [filename]);
}
async passesResultsOfFileToMigrator() {
this.setFakedFinderResults(['a']);
this.setFakedFileContents({
a: (0, test_utils_1.generateId)(),
});
await this.run();
this.assertContentsPassedToFileMigratorEquals([
this.fakedFileContents['a'],
]);
}
async passesResultsOfMultipleFilesToMigrator() {
const contents1 = (0, test_utils_1.generateId)();
const contents2 = (0, test_utils_1.generateId)();
this.setFakedFinderResults(['what', 'the']);
this.setFakedFileContents({
what: contents1,
the: contents2,
});
await this.run();
this.assertContentsPassedToFileMigratorEquals([contents1, contents2]);
}
async writesResponseOfMigrate() {
const filename = (0, test_utils_1.generateId)();
this.setFakedFinderResults([filename]);
this.setFakedFileContents({
[filename]: (0, test_utils_1.generateId)(),
});
await this.run();
test_utils_1.assert.isEqualDeep(this.lastWrittenFile, {
destination: filename,
contents: this.testFileMigrator.fakedMigrateResponse,
});
}
async returnsSkippedIfMigratedIsTheSame() {
this.setFakedFinderResults(['a']);
const contents = (0, test_utils_1.generateId)();
this.setFakedFileContents({
a: contents,
});
this.setMigrateFileResponse(contents);
const stats = await this.run();
test_utils_1.assert.isEqualDeep(stats, {
totalTestsUpdated: 0,
totalTestsSkipped: 1,
});
}
async countsSkippedAndMatched() {
const contents1 = (0, test_utils_1.generateId)();
const contents2 = (0, test_utils_1.generateId)();
this.setFakedFinderResults(['what', 'the', 'heck']);
this.setFakedFileContents({
what: contents1,
the: contents2,
heck: (0, test_utils_1.generateId)(),
});
this.testFileMigrator.fakedMigrateResponsesByContents = {
[contents1]: contents1,
[contents2]: contents2,
};
const stats = await this.run();
test_utils_1.assert.isEqualDeep(stats, {
totalTestsUpdated: 1,
totalTestsSkipped: 2,
});
}
async doesNotWriteFileThatDidNotChange() {
this.setFakedFinderResults(['a']);
const contents = (0, test_utils_1.generateId)();
this.setFakedFileContents({
a: contents,
});
this.setMigrateFileResponse(contents);
await this.run();
test_utils_1.assert.isUndefined(this.lastWrittenFile);
}
async lintsMigratedFiles() {
await this.run();
test_utils_1.assert.isTrue(this.spyCommandService.didExecute);
test_utils_1.assert.doesInclude(this.spyCommandService.lastCommand, `await cli.lintFiles(['**/*.ts'])`);
}
async shouldLintAfterMigrating() {
this.setFakedFinderResults(['match']);
this.setFakedFileContents({
match: (0, test_utils_1.generateId)(),
});
StaticToInstanceMigrator_1.default.diskUtil.writeFile = () => {
test_utils_1.assert.isFalse(this.spyCommandService.didExecute, 'Should not lint yet');
};
await this.run();
}
setMigrateFileResponse(contents) {
this.testFileMigrator.fakedMigrateResponse = contents;
}
assertContentsPassedToFileMigratorEquals(expected) {
test_utils_1.assert.isEqualDeep(this.testFileMigrator.contentsPassedToMigrate, expected);
}
setFakedFileContents(contents) {
this.fakedFileContents = contents;
}
setFakedFinderResults(results) {
this.testFinder.fakedResults = results;
}
async run(path) {
return await this.migrator.run(path ?? (0, test_utils_1.generateId)());
}
};
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "throwsWithMissing", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "runThrowsWithMissing", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "passesLookupDirToTestFinder", null);
__decorate([
(0, test_utils_1.test)('returns stats based on 3 tests found', ['a', 'b', 'c']),
(0, test_utils_1.test)('returns stats based on 0 tests found', [])
], StaticToInstanceMigratorTest.prototype, "returnsStatsBasedOnTestsFound", null);
__decorate([
(0, test_utils_1.test)('passes one files contents to migrator', 'a'),
(0, test_utils_1.test)('passes another files contents to migrator', 'b')
], StaticToInstanceMigratorTest.prototype, "passesOneFilesContentsToMigrator", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "passesResultsOfFileToMigrator", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "passesResultsOfMultipleFilesToMigrator", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "writesResponseOfMigrate", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "returnsSkippedIfMigratedIsTheSame", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "countsSkippedAndMatched", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "doesNotWriteFileThatDidNotChange", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "lintsMigratedFiles", null);
__decorate([
(0, test_utils_1.test)()
], StaticToInstanceMigratorTest.prototype, "shouldLintAfterMigrating", null);
StaticToInstanceMigratorTest = __decorate([
(0, test_utils_1.suite)()
], StaticToInstanceMigratorTest);
exports.default = StaticToInstanceMigratorTest;
class FakeStaticTestFinder {
lastLookupDir;
fakedResults = [];
async find(lookupDir) {
this.lastLookupDir = lookupDir;
return this.fakedResults;
}
}
class FakeFileMigrator {
contentsPassedToMigrate = [];
fakedMigrateResponse = (0, test_utils_1.generateId)();
fakedMigrateResponsesByContents = {};
migrate(contents) {
this.contentsPassedToMigrate.push(contents);
return (this.fakedMigrateResponsesByContents[contents] ??
this.fakedMigrateResponse);
}
}
class SpyCommandService {
didExecute = false;
lastCommand;
async execute(cmd, options) {
this.lastCommand = options?.args?.[1] ?? cmd;
this.didExecute = true;
return {
stdout: (0, test_utils_1.generateId)(),
};
}
getCwd() {
return '';
}
setCwd(_cwd) { }
kill() { }
pid() {
return 0;
}
}
//# sourceMappingURL=StaticToInstanceMigrator.test.js.map