UNPKG

2.8 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const v8 = require('v8')
5const { test } = require('tap')
6const async = require('async')
7const rimraf = require('rimraf')
8const ClinicDoctor = require('../index.js')
9
10test('cmd - test visualization - data exists', function (t) {
11 const tool = new ClinicDoctor({ dest: './foo' })
12
13 function cleanup (err, dirname) {
14 t.ifError(err)
15
16 t.match(dirname, /^foo(\/|\\)[0-9]+\.clinic-doctor$/)
17
18 async.parallel([
19 (done) => rimraf(dirname, done),
20 (done) => fs.unlink(dirname + '.html', done)
21 ], function (err) {
22 t.ifError(err)
23 t.end()
24 })
25 }
26
27 tool.collect(
28 [process.execPath, '-e', 'setTimeout(() => {}, 400)'],
29 function (err, dirname) {
30 if (err) return cleanup(err, dirname)
31
32 tool.visualize(dirname, dirname + '.html', function (err) {
33 if (err) return cleanup(err, dirname)
34
35 fs.readFile(dirname + '.html', function (err, content) {
36 if (err) return cleanup(err, dirname)
37
38 t.ok(content.length > 1024)
39 cleanup(null, dirname)
40 })
41 })
42 }
43 )
44})
45
46test('cmd - test visualization - memory exhausted', function (t) {
47 const tmp = process.memoryUsage
48 const HEAP_MAX = v8.getHeapStatistics().heap_size_limit
49
50 // Mock the used function to pretend the memory is exhausted.
51 process.memoryUsage = () => {
52 return {
53 rss: 0,
54 heapTotal: HEAP_MAX,
55 heapUsed: 0,
56 external: 0
57 }
58 }
59
60 const tool = new ClinicDoctor()
61
62 function cleanup (err, dirname) {
63 process.memoryUsage = tmp
64 t.ifError(err)
65
66 t.match(dirname, /^[0-9]+\.clinic-doctor$/)
67
68 async.parallel([
69 (done) => rimraf(dirname, done),
70 (done) => fs.unlink(dirname + '.html', done)
71 ], function (err) {
72 t.ifError(err)
73 t.end()
74 })
75 }
76
77 tool.on('warning', function (warning) {
78 t.equal(warning, 'Truncating input data due to memory constraints')
79 })
80 tool.on('truncate', function (undef) {
81 t.equal(undef, undefined)
82 })
83
84 tool.collect(
85 [process.execPath, '-e', 'setTimeout(() => {}, 400)'],
86 function (err, dirname) {
87 if (err) return cleanup(err, dirname)
88
89 tool.visualize(dirname, dirname + '.html', function (err) {
90 if (err) return cleanup(err, dirname)
91
92 fs.readFile(dirname + '.html', function (err, content) {
93 if (err) return cleanup(err, dirname)
94
95 t.ok(content.length > 1024)
96 cleanup(null, dirname)
97 })
98 })
99 }
100 )
101})
102
103test('cmd - test visualization - missing data', function (t) {
104 const tool = new ClinicDoctor({ debug: true })
105
106 tool.visualize(
107 'missing.clinic-doctor',
108 'missing.clinic-doctor.html',
109 function (err) {
110 t.ok(err.message.includes('ENOENT: no such file or directory'))
111 t.end()
112 }
113 )
114})