Skip to content

Commit 641b6bb

Browse files
authored
Merge pull request #132 from mcode/consolidate-runinstancelogger
Consolidating log functions from app to RunInstanceLogger
2 parents f2dd7c4 + 1d8617d commit 641b6bb

File tree

6 files changed

+75
-89
lines changed

6 files changed

+75
-89
lines changed

src/application/app.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,8 @@ function checkInputAndConfig(config, fromDate, toDate) {
3838
}
3939
}
4040

41-
function checkLogFile(pathToLogs) {
42-
// If no custom log file was specified and no default log file exists, create one
43-
if (path.resolve(pathToLogs) === path.resolve(path.join('logs', 'run-logs.json')) && !fs.existsSync(pathToLogs)) {
44-
logger.info(`No log file found. Creating default log file at ${pathToLogs}`);
45-
if (!fs.existsSync('logs')) fs.mkdirSync('logs');
46-
fs.appendFileSync(pathToLogs, '[]');
47-
}
48-
// Check that the given log file exists
49-
try {
50-
const logFileContent = JSON.parse(fs.readFileSync(pathToLogs));
51-
if (!Array.isArray(logFileContent)) throw new Error('Log file needs to be an array.');
52-
} catch (err) {
53-
logger.error(`The provided filepath to a LogFile, ${pathToLogs}, did not point to a valid JSON file. Create a json file with an empty array at this location.`);
54-
throw new Error(err.message);
55-
}
56-
}
57-
58-
// Use previous runs to infer a valid fromDate if none was provided
59-
function getEffectiveFromDate(fromDate, runLogger) {
60-
if (fromDate) return fromDate;
61-
62-
// Use the most recent ToDate
63-
logger.info('No fromDate was provided, inferring an effectiveFromDate');
64-
const effectiveFromDate = runLogger.getMostRecentToDate();
65-
logger.info(`effectiveFromDate: ${effectiveFromDate}`);
66-
if (!effectiveFromDate) {
67-
throw new Error('no valid fromDate was supplied, and there are no log records from which we could pull a fromDate');
68-
}
69-
70-
return effectiveFromDate;
71-
}
72-
7341
async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, debug, allEntries) {
7442
if (debug) logger.level = 'debug';
75-
// Don't require a run-logs file if we are extracting all-entries. Only required when using --entries-filter.
76-
if (!allEntries) checkLogFile(pathToRunLogs);
7743
const config = getConfig(pathToConfig);
7844
checkInputAndConfig(config, fromDate, toDate);
7945

@@ -86,7 +52,7 @@ async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, d
8652

8753
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
8854
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);
89-
const effectiveFromDate = allEntries ? null : getEffectiveFromDate(fromDate, runLogger);
55+
const effectiveFromDate = allEntries ? null : runLogger.getEffectiveFromDate(fromDate);
9056
const effectiveToDate = allEntries ? null : toDate;
9157

9258
// Extract the data

src/application/tools/RunInstanceLogger.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,36 @@ function createLogObject(fromDate, toDate) {
2828
class RunInstanceLogger {
2929
constructor(pathToLogFile) {
3030
this.logPath = pathToLogFile;
31+
if (path.resolve(this.logPath) === path.resolve(path.join('logs', 'run-logs.json')) && !fs.existsSync(this.logPath)) {
32+
logger.info(`No log file found. Creating default log file at ${this.logPath}`);
33+
if (!fs.existsSync('logs')) fs.mkdirSync('logs');
34+
fs.appendFileSync(this.logPath, '[]');
35+
}
36+
// Check that the given log file exists
3137
try {
32-
this.logs = JSON.parse(fs.readFileSync(path.resolve(this.logPath)));
33-
// Sort logs on load
34-
this.logs.sort(logSorter);
38+
this.logs = JSON.parse(fs.readFileSync(this.logPath));
39+
if (!Array.isArray(this.logs)) throw new Error('Log file needs to be an array.');
3540
} catch (err) {
36-
logger.error(`FATAL-Could not parse the logPath provided: ${this.logPath}`);
37-
process.exit(1);
41+
logger.error(`The provided filepath to a LogFile, ${this.logPath}, did not point to a valid JSON file. Create a json file with an empty array at this location.`);
42+
throw new Error(err.message);
3843
}
44+
// Sort logs on load
45+
this.logs.sort(logSorter);
46+
}
47+
48+
// Use previous runs to infer a valid fromDate if none was provided
49+
getEffectiveFromDate(fromDate) {
50+
if (fromDate) return fromDate;
51+
52+
// Use the most recent ToDate
53+
logger.info('No fromDate was provided, inferring an effectiveFromDate');
54+
const effectiveFromDate = this.getMostRecentToDate();
55+
logger.info(`effectiveFromDate: ${effectiveFromDate}`);
56+
if (!effectiveFromDate) {
57+
throw new Error('no valid fromDate was supplied, and there are no log records from which we could pull a fromDate');
58+
}
59+
60+
return effectiveFromDate;
3961
}
4062

4163
// Get the most recent run performed and logged

test/application/app.test.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
const fs = require('fs');
21
const rewire = require('rewire');
32
const testConfig = require('./fixtures/test-config.json');
43

54
const app = rewire('../../src/application/app.js');
65
const getConfig = app.__get__('getConfig');
76
const checkInputAndConfig = app.__get__('checkInputAndConfig');
8-
const checkLogFile = app.__get__('checkLogFile');
9-
const getEffectiveFromDate = app.__get__('getEffectiveFromDate');
107

118
describe('App Tests', () => {
129
describe('getConfig', () => {
@@ -36,50 +33,4 @@ describe('App Tests', () => {
3633
expect(() => checkInputAndConfig(testConfig, '2020-06-01', '2020-06-30')).not.toThrowError();
3734
});
3835
});
39-
40-
describe('checkLogFile', () => {
41-
const fsSpy = jest.spyOn(fs, 'readFileSync');
42-
it('should throw error when not provided a path', () => {
43-
expect(() => checkLogFile()).toThrowError();
44-
});
45-
46-
it('should throw error when path does not point to valid JSON', () => {
47-
expect(() => checkLogFile('./bad-path')).toThrowError();
48-
});
49-
50-
it('should throw error when log file is not an array', () => {
51-
fsSpy.mockReturnValueOnce(Buffer.from('{}'));
52-
expect(() => checkLogFile('path')).toThrowError('Log file needs to be an array.');
53-
expect(fsSpy).toHaveBeenCalled();
54-
});
55-
56-
it('should not throw error when log file is an array', () => {
57-
fsSpy.mockReturnValueOnce(Buffer.from('[]'));
58-
expect(() => checkLogFile('path')).not.toThrowError();
59-
expect(fsSpy).toHaveBeenCalled();
60-
});
61-
});
62-
63-
describe('getEffectiveFromDate', () => {
64-
const testDate = '2020-06-16';
65-
const mockRunLogger = {
66-
getMostRecentToDate: jest.fn(),
67-
};
68-
69-
it('should return fromDate when valid', () => {
70-
expect(getEffectiveFromDate(testDate)).toEqual(testDate);
71-
});
72-
73-
it('should return most recent date from runLogger', () => {
74-
mockRunLogger.getMostRecentToDate.mockReset();
75-
mockRunLogger.getMostRecentToDate.mockReturnValue(testDate);
76-
expect(getEffectiveFromDate(null, mockRunLogger)).toEqual(testDate);
77-
});
78-
79-
it('should throw error when no recent date from runlogger', () => {
80-
mockRunLogger.getMostRecentToDate.mockReset();
81-
expect(() => getEffectiveFromDate(null, mockRunLogger)).toThrowError();
82-
expect(mockRunLogger.getMostRecentToDate).toHaveBeenCalled();
83-
});
84-
});
8536
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"dateRun":"2021-06-17T15:10:49.601Z","toDate":"2020-06-16","fromDate":"2019-01-01"}]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const fs = require('fs');
2+
const { RunInstanceLogger } = require('../../src/application/tools/RunInstanceLogger.js');
3+
4+
describe('RunInstanceLogger', () => {
5+
describe('constructor', () => {
6+
const fsSpy = jest.spyOn(fs, 'readFileSync');
7+
it('should throw error when not provided a path', () => {
8+
expect(() => new RunInstanceLogger()).toThrowError();
9+
});
10+
11+
it('should throw error when path does not point to valid JSON', () => {
12+
expect(() => new RunInstanceLogger('./bad-path')).toThrowError();
13+
});
14+
15+
it('should throw error when log file is not an array', () => {
16+
fsSpy.mockReturnValueOnce(Buffer.from('{}'));
17+
expect(() => new RunInstanceLogger('path')).toThrowError('Log file needs to be an array.');
18+
expect(fsSpy).toHaveBeenCalled();
19+
});
20+
21+
it('should not throw error when log file is an array', () => {
22+
expect(() => new RunInstanceLogger('./test/application/fixtures/run-logs.json')).not.toThrowError();
23+
expect(fsSpy).toHaveBeenCalled();
24+
});
25+
});
26+
27+
describe('getEffectiveFromDate', () => {
28+
const testDate = '2020-06-16';
29+
30+
it('should return fromDate when valid', () => {
31+
const runLogger = new RunInstanceLogger('./test/application/fixtures/run-logs.json');
32+
expect(runLogger.getEffectiveFromDate(testDate)).toEqual(testDate);
33+
});
34+
35+
it('should return most recent date from runLogger', () => {
36+
const runLogger = new RunInstanceLogger('./test/application/fixtures/run-logs.json');
37+
expect(runLogger.getEffectiveFromDate(null)).toEqual(testDate);
38+
});
39+
40+
it('should throw error when no recent date from runlogger', () => {
41+
const runLogger = new RunInstanceLogger('./test/application/fixtures/empty-run-logs.json');
42+
expect(() => runLogger.getEffectiveFromDate(null)).toThrowError();
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)