Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cleanup-log-files.js « utils « lib « test « npm « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e97cf36b55dec041c0cbf91a7abc1c2d8aaf6fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const t = require('tap')

const glob = require('glob')
const rimraf = require('rimraf')
const mocks = { glob, rimraf }
const cleanup = t.mock('../../../lib/utils/cleanup-log-files.js', {
  glob: (...args) => mocks.glob(...args),
  rimraf: (...args) => mocks.rimraf(...args),
})
const { basename } = require('path')

const fs = require('fs')

t.test('clean up those files', t => {
  const cache = t.testdir({
    _logs: {
      '1-debug.log': 'hello',
      '2-debug.log': 'hello',
      '3-debug.log': 'hello',
      '4-debug.log': 'hello',
      '5-debug.log': 'hello',
    },
  })
  const warn = (...warning) => t.fail('failed cleanup', { warning })
  return cleanup(cache, 3, warn).then(() => {
    t.strictSame(fs.readdirSync(cache + '/_logs').sort(), [
      '3-debug.log',
      '4-debug.log',
      '5-debug.log',
    ])
  })
})

t.test('nothing to clean up', t => {
  const cache = t.testdir({
    _logs: {
      '4-debug.log': 'hello',
      '5-debug.log': 'hello',
    },
  })
  const warn = (...warning) => t.fail('failed cleanup', { warning })
  return cleanup(cache, 3, warn).then(() => {
    t.strictSame(fs.readdirSync(cache + '/_logs').sort(), [
      '4-debug.log',
      '5-debug.log',
    ])
  })
})

t.test('glob fail', t => {
  mocks.glob = (pattern, cb) => cb(new Error('no globbity'))
  t.teardown(() => mocks.glob = glob)
  const cache = t.testdir({})
  const warn = (...warning) => t.fail('failed cleanup', { warning })
  return cleanup(cache, 3, warn)
})

t.test('rimraf fail', t => {
  mocks.rimraf = (file, cb) => cb(new Error('youll never rimraf me!'))
  t.teardown(() => mocks.rimraf = rimraf)

  const cache = t.testdir({
    _logs: {
      '1-debug.log': 'hello',
      '2-debug.log': 'hello',
      '3-debug.log': 'hello',
      '4-debug.log': 'hello',
      '5-debug.log': 'hello',
    },
  })
  const warnings = []
  const warn = (...warning) => warnings.push(basename(warning[2]))
  return cleanup(cache, 3, warn).then(() => {
    t.strictSame(warnings.sort((a, b) => a.localeCompare(b, 'en')), [
      '1-debug.log',
      '2-debug.log',
    ])
  })
})