aft-reporting-filesystem
Version:
Automated Functional Testing (AFT) reporting plugin package supporting logging to files
135 lines • 7.79 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const aft_core_1 = require("aft-core");
const filesystem_reporting_plugin_1 = require("../src/filesystem-reporting-plugin");
describe('FilesystemReportingPlugin', () => {
beforeEach(() => {
const logDir = path.join(process.cwd(), 'logs');
if (fs.existsSync(logDir)) {
fs.rmSync(logDir, { recursive: true, force: true });
}
});
it('can create a file on the filesystem and write logs to it', () => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const aftCfg = new aft_core_1.AftConfig();
const plugin = new filesystem_reporting_plugin_1.FilesystemReportingPlugin(aftCfg);
const name = 'can create a file on the filesystem and write logs to it';
yield plugin.log({ name, level: 'trace', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'info', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'error', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.submitResult({
testName: name,
resultId: aft_core_1.rand.guid,
created: Date.now(),
status: 'passed'
});
yield plugin.submitResult({
testName: name,
resultId: aft_core_1.rand.guid,
created: Date.now(),
status: 'skipped'
});
yield plugin.submitResult({
testName: name,
resultId: aft_core_1.rand.guid,
created: Date.now(),
status: 'failed'
});
yield plugin.submitResult({
testName: name,
resultId: aft_core_1.rand.guid,
created: Date.now(),
status: 'untested'
});
const filePath = path.join(process.cwd(), 'logs', `${aft_core_1.convert.toSafeString(name)}.log`);
expect(fs.existsSync(filePath)).toBeTrue();
const lines = ((_a = fs.readFileSync(filePath, { encoding: 'utf-8' })) === null || _a === void 0 ? void 0 : _a.split('\n')) || [];
expect(lines.length).toBe(8); // 7 logged lines plus one empty due to newline at end of each line
expect(lines[0]).toContain('TRACE');
expect(lines[6]).toContain('INFO');
expect(lines[7]).toEqual('');
}));
it('will not write to file if level below specified value', () => __awaiter(void 0, void 0, void 0, function* () {
var _b;
const aftCfg = new aft_core_1.AftConfig({
FilesystemReportingPluginConfig: {
logLevel: 'error'
}
});
const plugin = new filesystem_reporting_plugin_1.FilesystemReportingPlugin(aftCfg);
const name = 'will not write to file if level below specified value';
yield plugin.log({ name, level: 'trace', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'debug', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'info', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'step', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'pass', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'fail', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'warn', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
yield plugin.log({ name, level: 'error', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
const filePath = path.join(process.cwd(), 'logs', `${aft_core_1.convert.toSafeString(name)}.log`);
expect(fs.existsSync(filePath)).toBeTrue();
const lines = ((_b = fs.readFileSync(filePath, { encoding: 'utf-8' })) === null || _b === void 0 ? void 0 : _b.split('\n')) || [];
expect(lines.length).toBe(2);
expect(lines[0]).toContain('ERROR');
expect(lines[1]).toEqual('');
}));
it('can change the date formatting', () => __awaiter(void 0, void 0, void 0, function* () {
var _c;
const name = 'can change the date formatting';
const aftCfg = new aft_core_1.AftConfig();
const config = aftCfg.getSection(filesystem_reporting_plugin_1.FilesystemReportingPluginConfig);
config.logLevel = 'info';
config.dateFormat = 'SSS';
const plugin = new filesystem_reporting_plugin_1.FilesystemReportingPlugin(aftCfg);
yield plugin.log({ name, level: 'warn', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
const filePath = path.join(process.cwd(), 'logs', `${aft_core_1.convert.toSafeString(name)}.log`);
expect(fs.existsSync(filePath)).toBeTrue();
const lines = ((_c = fs.readFileSync(filePath, { encoding: 'utf-8' })) === null || _c === void 0 ? void 0 : _c.split('\n')) || [];
expect(lines.length).toBe(2);
expect(lines[0]).toMatch(/^\[[0-9]{3}\] - WARN - .+$/);
expect(lines[1]).toEqual('');
}));
it('can disable output of TestResult objects', () => __awaiter(void 0, void 0, void 0, function* () {
const logName = 'can disable output of TestResult objects';
const aftCfg = new aft_core_1.AftConfig({
FilesystemReportingPluginConfig: {
logLevel: 'trace',
includeResults: false
}
});
const plugin = new filesystem_reporting_plugin_1.FilesystemReportingPlugin(aftCfg);
yield plugin.submitResult({
testName: logName,
resultId: aft_core_1.rand.guid,
created: Date.now(),
status: 'passed'
});
const filePath = path.join(process.cwd(), 'logs', `${aft_core_1.convert.toSafeString(logName)}.log`);
expect(fs.existsSync(filePath)).toBeFalse();
}));
it('truncates exceptionally long filenames based on config', () => __awaiter(void 0, void 0, void 0, function* () {
const maxLength = 10;
const aftCfg = new aft_core_1.AftConfig({
FilesystemReportingPluginConfig: {
maxFilenameLength: maxLength
}
});
const plugin = new filesystem_reporting_plugin_1.FilesystemReportingPlugin(aftCfg);
const name = 'the quick brown fox jumped over the lazy dogs';
yield plugin.log({ name, level: 'error', message: aft_core_1.rand.getString(aft_core_1.rand.getInt(100, 200)) });
const filePath = path.join(process.cwd(), 'logs', `${(0, aft_core_1.ellide)(aft_core_1.convert.toSafeString(name), maxLength, 'middle')}.log`);
expect(fs.existsSync(filePath)).toBeTrue();
}));
});
//# sourceMappingURL=filesystem-reporting-plugin-spec.js.map