1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const fs = require('fs');
6const path = require('path');
7
8const tmpdir = require('../common/tmpdir');
9tmpdir.refresh();
10
11function handler(err, folder) {
12  assert.ifError(err);
13  assert(fs.existsSync(folder));
14  assert.strictEqual(this, undefined);
15}
16
17// Test with plain string
18{
19  const tmpFolder = fs.mkdtempSync(path.join(tmpdir.path, 'foo.'));
20
21  assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
22  assert(fs.existsSync(tmpFolder));
23
24  const utf8 = fs.mkdtempSync(path.join(tmpdir.path, '\u0222abc.'));
25  assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
26                     Buffer.byteLength('\u0222abc.XXXXXX'));
27  assert(fs.existsSync(utf8));
28
29  fs.mkdtemp(path.join(tmpdir.path, 'bar.'), common.mustCall(handler));
30
31  // Same test as above, but making sure that passing an options object doesn't
32  // affect the way the callback function is handled.
33  fs.mkdtemp(path.join(tmpdir.path, 'bar.'), {}, common.mustCall(handler));
34
35  const warningMsg = 'mkdtemp() templates ending with X are not portable. ' +
36                     'For details see: https://nodejs.org/api/fs.html';
37  common.expectWarning('Warning', warningMsg);
38  fs.mkdtemp(path.join(tmpdir.path, 'bar.X'), common.mustCall(handler));
39}
40
41// Test with URL object
42{
43  const tmpFolder = fs.mkdtempSync(tmpdir.fileURL('foo.'));
44
45  assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
46  assert(fs.existsSync(tmpFolder));
47
48  const utf8 = fs.mkdtempSync(tmpdir.fileURL('\u0222abc.'));
49  assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
50                     Buffer.byteLength('\u0222abc.XXXXXX'));
51  assert(fs.existsSync(utf8));
52
53  fs.mkdtemp(tmpdir.fileURL('bar.'), common.mustCall(handler));
54
55  // Same test as above, but making sure that passing an options object doesn't
56  // affect the way the callback function is handled.
57  fs.mkdtemp(tmpdir.fileURL('bar.'), {}, common.mustCall(handler));
58
59  // Warning fires only once
60  fs.mkdtemp(tmpdir.fileURL('bar.X'), common.mustCall(handler));
61}
62
63// Test with Buffer
64{
65  const tmpFolder = fs.mkdtempSync(Buffer.from(path.join(tmpdir.path, 'foo.')));
66
67  assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
68  assert(fs.existsSync(tmpFolder));
69
70  const utf8 = fs.mkdtempSync(Buffer.from(path.join(tmpdir.path, '\u0222abc.')));
71  assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
72                     Buffer.byteLength('\u0222abc.XXXXXX'));
73  assert(fs.existsSync(utf8));
74
75  fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.')), common.mustCall(handler));
76
77  // Same test as above, but making sure that passing an options object doesn't
78  // affect the way the callback function is handled.
79  fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.')), {}, common.mustCall(handler));
80
81  // Warning fires only once
82  fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.X')), common.mustCall(handler));
83}
84
85// Test with Uint8Array
86{
87  const encoder = new TextEncoder();
88
89  const tmpFolder = fs.mkdtempSync(encoder.encode(path.join(tmpdir.path, 'foo.')));
90
91  assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length);
92  assert(fs.existsSync(tmpFolder));
93
94  const utf8 = fs.mkdtempSync(encoder.encode(path.join(tmpdir.path, '\u0222abc.')));
95  assert.strictEqual(Buffer.byteLength(path.basename(utf8)),
96                     Buffer.byteLength('\u0222abc.XXXXXX'));
97  assert(fs.existsSync(utf8));
98
99  fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.')), common.mustCall(handler));
100
101  // Same test as above, but making sure that passing an options object doesn't
102  // affect the way the callback function is handled.
103  fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.')), {}, common.mustCall(handler));
104
105  // Warning fires only once
106  fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.X')), common.mustCall(handler));
107}
108