11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst fs = require('fs');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst readdirDir = tmpdir.path;
101cb0ef41Sopenharmony_ciconst files = ['empty', 'files', 'for', 'just', 'testing'];
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Make sure tmp directory is clean
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Create the necessary files
161cb0ef41Sopenharmony_cifiles.forEach(function(currentFile) {
171cb0ef41Sopenharmony_ci  fs.closeSync(fs.openSync(`${readdirDir}/${currentFile}`, 'w'));
181cb0ef41Sopenharmony_ci});
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Check the readdir Sync version
211cb0ef41Sopenharmony_ciassert.deepStrictEqual(files, fs.readdirSync(readdirDir).sort());
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Check the readdir async version
241cb0ef41Sopenharmony_cifs.readdir(readdirDir, common.mustSucceed((f) => {
251cb0ef41Sopenharmony_ci  assert.deepStrictEqual(files, f.sort());
261cb0ef41Sopenharmony_ci}));
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// readdir() on file should throw ENOTDIR
291cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/1869
301cb0ef41Sopenharmony_ciassert.throws(function() {
311cb0ef41Sopenharmony_ci  fs.readdirSync(__filename);
321cb0ef41Sopenharmony_ci}, /Error: ENOTDIR: not a directory/);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cifs.readdir(__filename, common.mustCall(function(e) {
351cb0ef41Sopenharmony_ci  assert.strictEqual(e.code, 'ENOTDIR');
361cb0ef41Sopenharmony_ci}));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci[false, 1, [], {}, null, undefined].forEach((i) => {
391cb0ef41Sopenharmony_ci  assert.throws(
401cb0ef41Sopenharmony_ci    () => fs.readdir(i, common.mustNotCall()),
411cb0ef41Sopenharmony_ci    {
421cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
431cb0ef41Sopenharmony_ci      name: 'TypeError'
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  );
461cb0ef41Sopenharmony_ci  assert.throws(
471cb0ef41Sopenharmony_ci    () => fs.readdirSync(i),
481cb0ef41Sopenharmony_ci    {
491cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
501cb0ef41Sopenharmony_ci      name: 'TypeError'
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci  );
531cb0ef41Sopenharmony_ci});
54