Skip to content

Commit

Permalink
fix: Create a new console logger for each snapshot (#407)
Browse files Browse the repository at this point in the history
* commit dist

This will be reverted soon. Just so I can test on a fork without publishing
anything (or dealing with symlinks).

*  test: Create memory leak test for logger util

* test: Throw error when memory leak is detected

* Update test script names, create console transport function

* fix: file clean up in logger test

* test: capture output

* Code review feedback
  • Loading branch information
Robdel12 authored Oct 30, 2019
1 parent 8956e1e commit f63b832
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
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
33 changes: 33 additions & 0 deletions test/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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) => {
const filePath = `${process.cwd()}/${file}`
if (!existsSync(filePath)) { return }

unlinkSync(filePath)
})
}
})

it('does not leak memory', async () => {
const output = await captureStdErr(() => {
for (let index = 0; index < 600; index++) {
const fileName = `test-file-${index}`
createFileLogger(fileName)
filesToCleanUp.push(fileName)
}
})

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

0 comments on commit f63b832

Please sign in to comment.