1// Flags: --expose-internals 2'use strict'; 3 4const common = require('../common'); 5const { getDirents, getDirent } = require('internal/fs/utils'); 6const assert = require('assert'); 7const { internalBinding } = require('internal/test/binding'); 8const { UV_DIRENT_UNKNOWN } = internalBinding('constants').fs; 9const fs = require('fs'); 10const path = require('path'); 11 12const tmpdir = require('../common/tmpdir'); 13const filename = 'foo'; 14 15{ 16 // setup 17 tmpdir.refresh(); 18 fs.writeFileSync(path.join(tmpdir.path, filename), ''); 19} 20// getDirents 21{ 22 // string + string 23 getDirents( 24 tmpdir.path, 25 [[filename], [UV_DIRENT_UNKNOWN]], 26 common.mustCall((err, names) => { 27 assert.strictEqual(err, null); 28 assert.strictEqual(names.length, 1); 29 }, 30 )); 31} 32{ 33 // string + Buffer 34 getDirents( 35 tmpdir.path, 36 [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], 37 common.mustCall((err, names) => { 38 assert.strictEqual(err, null); 39 assert.strictEqual(names.length, 1); 40 }, 41 )); 42} 43{ 44 // Buffer + Buffer 45 getDirents( 46 Buffer.from(tmpdir.path), 47 [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], 48 common.mustCall((err, names) => { 49 assert.strictEqual(err, null); 50 assert.strictEqual(names.length, 1); 51 }, 52 )); 53} 54{ 55 // wrong combination 56 getDirents( 57 42, 58 [[Buffer.from(filename)], [UV_DIRENT_UNKNOWN]], 59 common.mustCall((err) => { 60 assert.strictEqual( 61 err.message, 62 [ 63 'The "path" argument must be of type string or an ' + 64 'instance of Buffer. Received type number (42)', 65 ].join('')); 66 }, 67 )); 68} 69// getDirent 70{ 71 // string + string 72 getDirent( 73 tmpdir.path, 74 filename, 75 UV_DIRENT_UNKNOWN, 76 common.mustCall((err, dirent) => { 77 assert.strictEqual(err, null); 78 assert.strictEqual(dirent.name, filename); 79 }, 80 )); 81} 82{ 83 // string + Buffer 84 const filenameBuffer = Buffer.from(filename); 85 getDirent( 86 tmpdir.path, 87 filenameBuffer, 88 UV_DIRENT_UNKNOWN, 89 common.mustCall((err, dirent) => { 90 assert.strictEqual(err, null); 91 assert.strictEqual(dirent.name, filenameBuffer); 92 }, 93 )); 94} 95{ 96 // Buffer + Buffer 97 const filenameBuffer = Buffer.from(filename); 98 getDirent( 99 Buffer.from(tmpdir.path), 100 filenameBuffer, 101 UV_DIRENT_UNKNOWN, 102 common.mustCall((err, dirent) => { 103 assert.strictEqual(err, null); 104 assert.strictEqual(dirent.name, filenameBuffer); 105 }, 106 )); 107} 108{ 109 // wrong combination 110 getDirent( 111 42, 112 Buffer.from(filename), 113 UV_DIRENT_UNKNOWN, 114 common.mustCall((err) => { 115 assert.strictEqual( 116 err.message, 117 [ 118 'The "path" argument must be of type string or an ' + 119 'instance of Buffer. Received type number (42)', 120 ].join('')); 121 }, 122 )); 123} 124