Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Create a new console logger for each snapshot #407

Merged
merged 7 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ commands:
name: Unit tests
command: |
mkdir -p reports
$NYC yarn test --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=-
$NYC yarn test:unit --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=-
- run:
name: Client tests
command: $NYC yarn test-client --singleRun
command: $NYC yarn test:client --singleRun
- run:
name: Integration tests
command: $NYC yarn test-integration
command: $NYC yarn test:integration
cli_integration:
steps:
- run:
name: Snapshot command test
command: yarn test-snapshot-command
command: yarn test:command:snapshot
- run:
name: Upload command test
command: yarn test-upload-command
command: yarn test:command:upload
cli_commands:
steps:
- run:
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@
"posttest": "yarn lint",
"prepublishOnly": "yarn build && oclif-dev manifest && oclif-dev readme",
"preversion": "yarn clean",
"test": "yarn build-client && PERCY_TOKEN=abc mocha --forbid-only \"test/**/*.test.ts\" --exclude \"test/percy-agent-client/**/*.test.ts\" --exclude \"test/integration/**/*\"",
"test-client": "karma start ./test/percy-agent-client/karma.conf.js",
"test-integration": "yarn build-client && node ./bin/run exec -h *.localtest.me -c .ci.percy.yml -- mocha test/integration/**/*.test.ts",
"test-snapshot-command": "./bin/run snapshot test/integration/test-static-site -b /dummy-base-url/ -i **/red-keep.** -s **/*.{html}",
"test-upload-command": "./bin/run upload test/integration/test-static-images",
"test": "yarn test:unit && yarn test:client && yarn test:integration && yarn test:command:snapshot && yarn test:command:upload",
"test:unit": "PERCY_TOKEN=abc mocha \"test/**/*.test.ts\" --exclude \"test/percy-agent-client/**/*.test.ts\" --exclude \"test/integration/**/*\"",
"test:client": "karma start ./test/percy-agent-client/karma.conf.js",
"test:integration": "yarn build-client && node ./bin/run exec -h *.localtest.me -c .ci.percy.yml -- mocha test/integration/**/*.test.ts",
"test:command:snapshot": "./bin/run snapshot test/integration/test-static-site -b /dummy-base-url/ -i **/red-keep.** -s **/*.{html}",
"test:command:upload": "./bin/run upload test/integration/test-static-images",
"version": "oclif-dev readme && git add README.md",
"watch": "npm-watch"
},
Expand Down
22 changes: 12 additions & 10 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import * as winston from 'winston'

const LOG_LEVEL = process.env.LOG_LEVEL || 'info'

const consoleTransport = new winston.transports.Console({
level: LOG_LEVEL,
stderrLevels: ['error'],
format: winston.format.combine(
winston.format.label({ label: colors.magenta('percy') }),
winston.format.printf(({ label, message }) => `[${label}] ${message}`),
),
})
function createConsoleTransport() {
return new winston.transports.Console({
level: LOG_LEVEL,
stderrLevels: ['error'],
format: winston.format.combine(
winston.format.label({ label: colors.magenta('percy') }),
winston.format.printf(({ label, message }) => `[${label}] ${message}`),
),
})
}

const logger = winston.createLogger({
transports: [consoleTransport],
transports: [createConsoleTransport()],
})

export function profile(id: string, meta?: any): winston.Logger | undefined {
Expand All @@ -29,7 +31,7 @@ export function logError(error: any) {

export function createFileLogger(filename: string) {
const fileTransport = new winston.transports.File({ filename, level: 'debug' })
return winston.createLogger({ transports: [consoleTransport, fileTransport] })
return winston.createLogger({ transports: [createConsoleTransport(), fileTransport] })
}

export default logger
32 changes: 32 additions & 0 deletions test/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect } from 'chai'
import { existsSync, unlinkSync } from 'fs'
import { createFileLogger } from '../../src/utils/logger'
import { captureStdErr } from '../helpers/stdout'

describe('logger utils', () => {
describe('createFileLogger', () => {
const filesToCleanUp = [] as any

afterEach(() => {
if (filesToCleanUp) {
filesToCleanUp.forEach((file: string) => existsSync(`${process.cwd()}/${file}`) && unlinkSync(`${process.cwd()}/${file}`))
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved
}
})

it('does not leak memory', async () => {
const output = await captureStdErr(async () => {
await new Promise((resolve) => {
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved
new Array(600).fill(0).forEach((item, index) => {
Robdel12 marked this conversation as resolved.
Show resolved Hide resolved
const fileName = `test-file-${index}`
createFileLogger(fileName)
filesToCleanUp.push(fileName)
})

resolve()
})
})

expect(output).to.equal('')
})
})
})