declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
100 lines (96 loc) • 4.09 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const yaml_1 = __importDefault(require("yaml"));
const readFileAsync_1 = require("./readFileAsync");
const readYmlFile_1 = require("./readYmlFile");
jest.mock('./readFileAsync');
const readFileAsyncMock = readFileAsync_1.readFileAsync;
readFileAsyncMock.mockResolvedValue(`
- type: change
path: 'init/service_user.sql'
id: 'init_20190619_1'
reapplyOnUpdate: false # we'll do it manually for now
- type: change
id: 'data_20190619_1'
path: 'data/data_sources.sql'
reapplyOnUpdate: false # only run each insert once
- type: change
id: 'procedures_20190619_1'
path: 'procedures/awesome_sproc.sql'
reapplyOnUpdate: true
`);
describe('readDefinitionsFileRecursive', () => {
beforeEach(() => jest.clearAllMocks());
it('should throw an error if the filepath defined does not point to a yml file', () => __awaiter(void 0, void 0, void 0, function* () {
try {
yield (0, readYmlFile_1.readYmlFile)({ filePath: 'some.json' });
throw new Error('should not reach here');
}
catch (error) {
expect(error.message).toMatch('file path point to a .yml file.');
}
}));
it('should attempt to read the file', () => __awaiter(void 0, void 0, void 0, function* () {
yield (0, readYmlFile_1.readYmlFile)({ filePath: 'some.yml' });
expect(readFileAsyncMock.mock.calls.length).toEqual(1);
expect(readFileAsyncMock.mock.calls[0][0]).toEqual({
filePath: 'some.yml',
});
}));
it('should parse the contents into yml with the YAML library', () => __awaiter(void 0, void 0, void 0, function* () {
const spy = jest.spyOn(yaml_1.default, 'parse');
expect(spy.mock.calls.length).toEqual(0);
yield (0, readYmlFile_1.readYmlFile)({ filePath: 'some.yml' });
expect(spy.mock.calls.length).toEqual(1);
}));
it('should return the parsed yml content', () => __awaiter(void 0, void 0, void 0, function* () {
readFileAsyncMock.mockResolvedValueOnce(`
- type: change
path: 'init/service_user.sql'
id: 'init_20190619_1'
reapplyOnUpdate: false # we'll do it manually for now
- type: change
id: 'data_20190619_1'
path: 'data/data_sources.sql'
reapplyOnUpdate: false # only run each insert once
- type: change
id: 'procedures_20190619_1'
path: 'procedures/awesome_sproc.sql'
reapplyOnUpdate: true
`);
const result = yield (0, readYmlFile_1.readYmlFile)({ filePath: 'some.yml' });
expect(result).toMatchObject([
{
type: 'change',
path: 'init/service_user.sql',
id: 'init_20190619_1',
reapplyOnUpdate: false,
},
{
type: 'change',
id: 'data_20190619_1',
path: 'data/data_sources.sql',
reapplyOnUpdate: false,
},
{
type: 'change',
id: 'procedures_20190619_1',
path: 'procedures/awesome_sproc.sql',
reapplyOnUpdate: true,
},
]);
}));
});
//# sourceMappingURL=readYmlFile.test.js.map