-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new test cases to test the APIs whether it correctly accepts file URL path and a `URL` object with 'file:' protocol, and correctly throws an `URIError` if the provided URL path using unsupported protocols.
- Loading branch information
Showing
2 changed files
with
58 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,51 @@ | ||
const { join, basename } = require('node:path'); | ||
/** | ||
* A test module for `lsfnd` package designed for CommonJS module (CJS). | ||
* @author Ryuu Mitsuki (https://github.com/mitsuki31) | ||
*/ | ||
|
||
const path = require('node:path'); | ||
const { pathToFileURL } = require('node:url'); | ||
const { ls, lsFiles, lsDirs } = require('..'); | ||
const { it, rejects, deepEq } = require('./lib/simpletest'); | ||
const { it, rejects, doesNotReject, deepEq } = require('./lib/simpletest'); | ||
|
||
const rootDir = path.resolve('..'); | ||
const rootDirPosix = path.posix.resolve('..'); | ||
|
||
console.log(`\n\x1b[1m${basename(__filename)}:\x1b[0m`); | ||
console.log(`\n\x1b[1m${path.basename(__filename)}:\x1b[0m`); | ||
|
||
it('test `ls` function by listing this file directory', async () => { | ||
const results = await ls(__dirname, {}, 0); | ||
const expected = [ 'lib', 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] | ||
.map((e) => join(__dirname, e)); | ||
.map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('test `lsFiles` function by listing this file directory', async () => { | ||
const results = await lsFiles(__dirname); | ||
const expected = [ 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] | ||
.map((e) => join(__dirname, e)); | ||
.map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('list root directory using URL object', async () => { | ||
await doesNotReject(ls(pathToFileURL(rootDirPosix)), URIError); | ||
}, false); | ||
|
||
it('list root directory using file URL path', async () => { | ||
await doesNotReject(ls('file:'.concat(rootDirPosix)), URIError); | ||
}, false); | ||
|
||
it('test `lsDirs` function by listing this file directory', async () => { | ||
const results = await lsDirs(__dirname); | ||
const expected = [ 'lib' ].map((e) => join(__dirname, e)); | ||
const expected = [ 'lib' ].map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('throws an error if given directory path not exist', async () => { | ||
it('throws an error if the given directory path not exist', async () => { | ||
await rejects(ls('./this/is/not/exist/directory/path'), Error); | ||
}, false); | ||
|
||
it('throws an URIError if the given file URL path using unsupported protocol', | ||
async () => await rejects(ls('http:'.concat(rootDirPosix)), URIError), | ||
false | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,55 @@ | ||
import { join, dirname, basename } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
/** | ||
* A test module for `lsfnd` package designed for ECMAScript module (ESM). | ||
* @author Ryuu Mitsuki (https://github.com/mitsuki31) | ||
*/ | ||
|
||
import * as path from 'node:path'; | ||
import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
import { ls, lsFiles, lsDirs } from '../dist/index.js'; | ||
import test from './lib/simpletest.js'; | ||
const { it, rejects, deepEq } = test; | ||
const { it, rejects, doesNotReject, deepEq } = test; // Resolve import from CommonJS module | ||
|
||
// Create the '__dirname' and '__filename' variable, because in ESM these are not defined | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const __dirname = path.dirname(__filename); | ||
const rootDir = path.resolve('..'); | ||
const rootDirPosix = path.posix.resolve('..'); | ||
|
||
console.log(`\n\x1b[1m${basename(__filename)}:\x1b[0m`); | ||
console.log(`\n\x1b[1m${path.basename(__filename)}:\x1b[0m`); | ||
|
||
it('test `ls` function by listing this file directory', async () => { | ||
const results = await ls(__dirname, {}, 0); | ||
const expected = [ 'lib', 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] | ||
.map((e) => join(__dirname, e)); | ||
.map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('test `lsFiles` function by listing this file directory', async () => { | ||
const results = await lsFiles(__dirname); | ||
const expected = [ 'lsfnd.spec.cjs', 'lsfnd.spec.mjs' ] | ||
.map((e) => join(__dirname, e)); | ||
.map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('test `lsDirs` function by listing this file directory', async () => { | ||
const results = await lsDirs(__dirname); | ||
const expected = [ 'lib' ].map((e) => join(__dirname, e)); | ||
const expected = [ 'lib' ].map((e) => path.join(__dirname, e)); | ||
deepEq(results, expected); | ||
}, false); | ||
|
||
it('throws an error if given directory path not exist', async () => { | ||
it('list root directory using URL object', async () => { | ||
await doesNotReject(ls(pathToFileURL(rootDirPosix)), URIError); | ||
}, false); | ||
|
||
it('list root directory using file URL path', async () => { | ||
await doesNotReject(ls('file:'.concat(rootDirPosix)), URIError); | ||
}, false); | ||
|
||
it('throws an error if the given directory path not exist', async () => { | ||
await rejects(ls('./this/is/not/exist/directory/path'), Error); | ||
}, false); | ||
|
||
it('throws an URIError if the given file URL path using unsupported protocol', | ||
async () => await rejects(ls('http:'.concat(rootDirPosix)), URIError), | ||
false | ||
); |