1'use strict';
2
3const common = require('../common');
4
5if (!common.isMainThread)
6  common.skip('process.chdir is not available in Workers');
7
8const { writeHeapSnapshot, getHeapSnapshot } = require('v8');
9const assert = require('assert');
10const fs = require('fs');
11const tmpdir = require('../common/tmpdir');
12
13tmpdir.refresh();
14process.chdir(tmpdir.path);
15
16{
17  writeHeapSnapshot('my.heapdump');
18  fs.accessSync('my.heapdump');
19}
20
21{
22  const heapdump = writeHeapSnapshot();
23  assert.strictEqual(typeof heapdump, 'string');
24  fs.accessSync(heapdump);
25}
26
27{
28  const directory = 'directory';
29  fs.mkdirSync(directory);
30  assert.throws(() => {
31    writeHeapSnapshot(directory);
32  }, (e) => {
33    assert.ok(e, 'writeHeapSnapshot should error');
34    assert.strictEqual(e.code, 'EISDIR');
35    assert.strictEqual(e.syscall, 'open');
36    return true;
37  });
38}
39
40[1, true, {}, [], null, Infinity, NaN].forEach((i) => {
41  assert.throws(() => writeHeapSnapshot(i), {
42    code: 'ERR_INVALID_ARG_TYPE',
43    name: 'TypeError',
44    message: 'The "path" argument must be of type string or an instance of ' +
45             'Buffer or URL.' +
46             common.invalidArgTypeHelper(i)
47  });
48});
49
50{
51  let data = '';
52  const snapshot = getHeapSnapshot();
53  snapshot.setEncoding('utf-8');
54  snapshot.on('data', common.mustCallAtLeast((chunk) => {
55    data += chunk.toString();
56  }));
57  snapshot.on('end', common.mustCall(() => {
58    JSON.parse(data);
59  }));
60}
61