Skip to content

Commit 4979486

Browse files
committed
logger refactor, comments and cleanup
1 parent b41ee77 commit 4979486

File tree

7 files changed

+61
-51
lines changed

7 files changed

+61
-51
lines changed

Diff for: lib/logger.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
var unexpectedOutput = /^(?!# TAP)(?!(not )?ok [0-9]+ -)(?!1..[0-9]+)(?!# E\s)(.*)$/gm;
3+
function logger(data) {
4+
var logs = data.match(unexpectedOutput);
5+
if (!logs && logs.length < 1) {
6+
return;
7+
}
8+
logs.forEach(function (line) {
9+
if (line.length > 0) {
10+
try {
11+
line = JSON.parse(JSON.stringify(line));
12+
console.dir(JSON.parse(JSON.stringify(line)));
13+
}
14+
catch (e) {
15+
console.log(line);
16+
}
17+
}
18+
});
19+
}
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
exports.default = logger;

Diff for: lib/runner.js

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
"use strict";
22
var create_runner_1 = require('./create-runner');
33
var parse_tap_1 = require('./parse-tap');
4-
function log(data) {
5-
var logs = data.match(/^(?!# TAP)(?!(not )?ok [0-9]+ -)(?!1..[0-9]+)(?!# E\s)(.*)$/gm);
6-
if (logs && logs.length > 0) {
7-
logs.forEach(function (line) {
8-
if (line.length > 0) {
9-
try {
10-
line = JSON.parse(JSON.stringify(line));
11-
if (typeof line === 'string') {
12-
console.log(line);
13-
}
14-
else {
15-
console.dir(JSON.parse(JSON.stringify(line)));
16-
}
17-
}
18-
catch (e) {
19-
console.log(line);
20-
}
21-
}
22-
});
23-
}
24-
}
4+
var logger_1 = require('./logger');
255
function runner(testFile, config, handleResult) {
266
var runner = create_runner_1.default(config, testFile);
277
var final = null;
@@ -31,7 +11,7 @@ function runner(testFile, config, handleResult) {
3111
if (!data || !data.length) {
3212
return;
3313
}
34-
log(data);
14+
logger_1.default(data);
3515
final = parse_tap_1.default(data);
3616
if (!final) {
3717
console.log('Error parsing test ouptut:', data);

Diff for: src/create-runner.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let python = 'python';
55
let localPath = '/usr/local/bin/python';
66
let globalPath = '/usr/bin/python';
77

8+
// use python path for mac/linux (usr/local/bin, usr/bin), or windows (python)
89
if (process.platform === 'darwin' && process.resourcesPath) {
910
if (exists(localPath)) {
1011
python = localPath;
@@ -16,11 +17,12 @@ if (process.platform === 'darwin' && process.resourcesPath) {
1617
}
1718

1819
export default function createRunner(config: CR.Config, testFile: string) {
20+
// see pytest options: https://pytest.org/latest/usage.html
1921
return exec([
2022
python,
2123
'-m pytest',
22-
'-s',
23-
'--tap-stream',
24+
'-s', // capture content
25+
'--tap-stream', // TAP formatted output
2426
'-x', // stop after first failure
2527
'--tb=no', // no traceback
2628
testFile

Diff for: src/logger.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const unexpectedOutput: RegExp = /^(?!# TAP)(?!(not )?ok [0-9]+ -)(?!1..[0-9]+)(?!# E\s)(.*)$/gm;
2+
3+
// capture any unexpected output to the log
4+
export default function logger(data: string): void {
5+
var logs: string[] = data.match(unexpectedOutput);
6+
if (!logs && logs.length < 1) {
7+
return;
8+
}
9+
logs.forEach((line: string) => {
10+
if (line.length > 0) {
11+
try {
12+
line = JSON.parse(JSON.stringify(line));
13+
console.dir(JSON.parse(JSON.stringify(line)));
14+
} catch (e) {
15+
console.log(line);
16+
}
17+
}
18+
});
19+
}

Diff for: src/parse-tap.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ function formatFeedback(message: string): string {
1111

1212
export default function parseTap(data: string): ParseFinal {
1313

14+
// Output as TAP?
1415
if (!data || !data.match(regex.isTap)) {
1516
console.log('No TAP output: ', data);
1617
return;
1718
}
1819

20+
// Final test number?
1921
if (!data.match(regex.finalTest)) {
2022
console.log('Could not parse final test number: ', data);
2123
return;
2224
}
23-
2425
let finalTest: number = parseInt(data.match(regex.finalTest)[1], 10);
25-
2626
let final: ParseFinal = null;
2727

28+
// Fail or Pass?
2829
if (data.match(regex.error)) {
2930

3031
// first FAILing test
3132

32-
let failingLineRegex = new RegExp(`^not ok ${finalTest} - (.+)$`, 'm');
33+
let failingLineRegex: RegExp = new RegExp(`^not ok ${finalTest} - (.+)$`, 'm');
3334
let line: string = data.match(failingLineRegex)[1];
3435
if (!line || typeof line !== 'string') {
3536
console.log('Error matching failing test line: ', data);
@@ -56,7 +57,7 @@ export default function parseTap(data: string): ParseFinal {
5657

5758
// all tests PASS
5859

59-
let finalPassRegex = new RegExp(`^ok ${finalTest} - (.+)$`, 'm');
60+
let finalPassRegex: RegExp = new RegExp(`^ok ${finalTest} - (.+)$`, 'm');
6061
let line: string = data.match(finalPassRegex)[1];
6162
let taskPosition: number = parseInt(line.match(/Test([0-9]+)/)[1], 10);
6263

Diff for: src/runner.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
11
import createRunner from './create-runner';
22
import parseTap from './parse-tap';
3+
import logger from './logger';
34

4-
function log(data: string): void {
5-
var logs = data.match(/^(?!# TAP)(?!(not )?ok [0-9]+ -)(?!1..[0-9]+)(?!# E\s)(.*)$/gm);
6-
if (logs && logs.length > 0) {
7-
logs.forEach((line: string) => {
8-
if (line.length > 0) {
9-
try {
10-
line = JSON.parse(JSON.stringify(line));
11-
if (typeof line === 'string') {
12-
console.log(line);
13-
} else {
14-
console.dir(JSON.parse(JSON.stringify(line)));
15-
}
16-
} catch (e) {
17-
console.log(line);
18-
}
19-
}
20-
});
21-
}
22-
}
235

246
export default function runner(testFile: string, config: CR.Config,
257
handleResult: (result) => CR.TestResult): Promise<CR.TestResult> {
@@ -32,25 +14,29 @@ export default function runner(testFile: string, config: CR.Config,
3214
runner.stdout.on('data', function(data): void {
3315

3416
data = data.toString();
17+
18+
// no output, end early
3519
if (!data || !data.length) {
3620
return;
3721
}
3822

39-
// capture any abnormal data as a log
40-
log(data);
23+
// log to Atom console
24+
logger(data);
4125

42-
// transform data;
26+
// parse data into JSON object
4327
final = parseTap(data);
28+
29+
// could not parse, log error
4430
if (!final) {
4531
console.log('Error parsing test ouptut:', data);
4632
}
4733

34+
// complete JSON object
4835
final.change = final.taskPosition - config.taskPosition;
4936
final.pass = final.change > 0;
5037

5138
// return result to atom-coderoad
5239
handleResult(final);
53-
5440
});
5541

5642
runner.stderr.on('data', function(data) {

Diff for: tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"files": [
1818
"src/create-runner.ts",
1919
"src/exists.ts",
20+
"src/logger.ts",
2021
"src/parse-tap.ts",
2122
"src/runner.ts",
2223
"src/typings/tsd.d.ts"

0 commit comments

Comments
 (0)