Skip to content

Commit aca5d02

Browse files
committed
improved console logger
1 parent 36d56a2 commit aca5d02

File tree

12 files changed

+91
-28
lines changed

12 files changed

+91
-28
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [0.7.0] -
5+
## [0.7.0] - 2016-05-23
66
- improved console logger
77

88
## [0.6.0] - 2016-04-01

lib/.tmp.js

+52-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
1-
/// coderoad-test-file
2-
3-
41

2+
'use strict';
3+
const util = require('util');
4+
5+
function getType(output) {
6+
switch (Object.prototype.toString.call(output)) {
7+
case '[object Array]':
8+
return 'array';
9+
case '[object Date]':
10+
return 'date';
11+
case '[object Null]':
12+
return 'null';
13+
case '[object Number]':
14+
// NaN !== NaN
15+
return (output !== output) ? 'NaN' : 'number';
16+
}
17+
return typeof output;
18+
}
19+
const inspectOptions = {
20+
depth: null
21+
};
22+
function assignTypes(args) {
23+
return JSON.stringify(
24+
args.map((output) => {
25+
const type = getType(output);
26+
switch (type) {
27+
case 'object':
28+
case 'array':
29+
output = JSON.stringify(output);
30+
output = util.inspect(output, inspectOptions);
31+
output = output.substring(1, output.length - 1);
32+
break;
33+
case 'undefined':
34+
case 'null':
35+
case 'NaN':
36+
return { type };
37+
}
38+
return { type, output };
39+
})
40+
);
41+
}
542

43+
if (console && console.log) {
44+
const originalLog = console.log;
45+
console.log = (...args) => {
46+
var stack = new Error().stack;
47+
debugger;
48+
setTimeout(originalLog.apply(this, [assignTypes(args)], null));
49+
};
50+
};
651
var expect = require('chai').expect;
752

853
global.something = {
@@ -19,6 +64,10 @@ function subtractOne(x) {
1964
return x - 1
2065
}
2166

67+
console.log({
68+
a: 1
69+
}, 2)
70+
2271

2372
describe('01 addOne', function() {
2473

lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var constants_1 = require('./constants');
33
var runner_process_1 = require('./runner-process');
44
var write_test_1 = require('./write-test');
5+
var process_console_log_1 = require('process-console-log');
56
function runner(testString, config, handleResult) {
67
write_test_1.default(testString);
78
var runner = runner_process_1.default(config);
@@ -13,10 +14,10 @@ function runner(testString, config, handleResult) {
1314
var match = signalMatch.exec(data);
1415
if (!match) {
1516
try {
16-
console.log(data);
17+
process_console_log_1.parseLog(data);
1718
}
1819
catch (e) {
19-
console.log(data);
20+
process_console_log_1.parseLog(data);
2021
}
2122
return;
2223
}

lib/logger.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var loggerOverride = fs_1.readFileSync('./override-logger');
4+
function loadLogger(test) {
5+
}
6+
Object.defineProperty(exports, "__esModule", { value: true });
7+
exports.default = loadLogger;

lib/runner-process.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ function runnerProcess(config) {
3232
if (options.env == null) {
3333
options.env = Object.create(process.env);
3434
}
35-
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
36-
options.env.DIR = config.dir;
37-
options.env.TUTORIAL_DIR = config.dir;
38-
options.env.TASK_POSITION = config.taskPosition;
35+
Object.assign(options.env, {
36+
ATOM_SHELL_INTERNAL_RUN_AS_NODE: 1,
37+
DIR: config.dir,
38+
TUTORIAL_DIR: config.dir,
39+
TASK_POSITION: config.taskPosition
40+
});
3941
return child_process_1.spawn(node, [
4042
mocha,
4143
'--bail',

lib/write-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22
var fs_1 = require('fs');
3+
var process_console_log_1 = require('process-console-log');
34
var constants_1 = require('./constants');
45
function writeTest(output) {
6+
output = process_console_log_1.logger + output;
57
fs_1.writeFileSync(constants_1.testPath, output, 'utf8');
68
}
79
Object.defineProperty(exports, "__esModule", { value: true });

src/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {signal, testPath} from './constants';
1+
import {signal} from './constants';
22
import runnerProcess from './runner-process';
33
import writeTest from './write-test';
4-
// import {parseLog} from 'process-console-log';
5-
// import loadLogger from './logger';
4+
import {parseLog} from 'process-console-log';
65

76
export default function runner(
8-
testString: string, config: CR.Config,
7+
testString: string,
8+
config: CR.Config,
99
handleResult: (result) => CR.TestResult
1010
) {
1111
// write tests to file
@@ -25,9 +25,11 @@ export default function runner(
2525

2626
if (!match) {
2727
try {
28-
console.log(data);
28+
// console.log(data);
29+
parseLog(data);
2930
} catch (e) {
30-
console.log(data);
31+
// console.log(data);
32+
parseLog(data);
3133
}
3234
return;
3335
}

src/logger.ts

-7
This file was deleted.

src/runner-process.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ export default function runnerProcess(config: CR.Config) {
3636
if (options.env == null) {
3737
options.env = Object.create(process.env);
3838
}
39-
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
40-
options.env.DIR = config.dir;
41-
options.env.TUTORIAL_DIR = config.dir;
42-
options.env.TASK_POSITION = config.taskPosition;
39+
40+
Object.assign(options.env, {
41+
ATOM_SHELL_INTERNAL_RUN_AS_NODE: 1,
42+
DIR: config.dir,
43+
TUTORIAL_DIR: config.dir,
44+
TASK_POSITION: config.taskPosition
45+
});
4346

4447
// spawn child process calling mocha test runner
4548
return spawn(node, [
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare module 'process-console-log' {
22
export function parseLog(logged: {type: string, output: any}[]): void;
33
export function initProcessLogger(): void;
4+
export const logger: string;
45
}

src/write-test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {writeFileSync} from 'fs';
2+
import {logger} from 'process-console-log';
23
import {testPath} from './constants';
34

45
export default function writeTest(output: string) {
6+
// append logger
7+
output = logger + output;
8+
// write test file
59
writeFileSync(testPath, output, 'utf8');
610
}

tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"files": [
1818
"src/constants.ts",
1919
"src/index.ts",
20-
"src/logger.ts",
2120
"src/reporter.ts",
2221
"src/runner-process.ts",
2322
"src/write-test.ts",

0 commit comments

Comments
 (0)