Skip to content

Commit

Permalink
test: Introduce new test cases
Browse files Browse the repository at this point in the history
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
mitsuki31 committed Apr 19, 2024
1 parent 14daa9a commit 6c3a254
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
36 changes: 29 additions & 7 deletions test/lsfnd.spec.cjs
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
);
38 changes: 29 additions & 9 deletions test/lsfnd.spec.mjs
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
);

0 comments on commit 6c3a254

Please sign in to comment.