Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotberry committed Nov 30, 2023
1 parent 2d59a63 commit 95b5293
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 140 deletions.
2 changes: 0 additions & 2 deletions lib/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var slugifyFunction
//
const pipeFunctions = async (str, ...fns) => {
for await (const fn of fns) {

str = await fn.fn(str, fn.args)
}
return str
Expand All @@ -26,7 +25,6 @@ const init = () => {

)
} else {
console.log(`adding ${item.name} to slugify function`)
finalFunctions.push({
fn: stringModificationFunctions[item.name].fn,
args: item.args || {},
Expand Down
36 changes: 36 additions & 0 deletions test/createDirectoryWithFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs';
import path from 'path';
import { deleteDirectoryAndFiles, dirPath, names, coolText } from './test.js';

export async function createDirectoryWithFiles() {
await deleteDirectoryAndFiles();
// Create directory
await fs.promises.mkdir(dirPath);

const getRandomNames = (num = 1) => {
let nameArr = [];
for (let i = 0; i < num; i++) {
nameArr.push(names[Math.floor(Math.random() * names.length)]);
}
return nameArr.join(' ');
};
let arr = Array.from({ length: 20 }, (_, i) => i);
let testFilesMade = 0;
let oneFilePath = '';
for await (let i of arr) {
const num = Math.floor(Math.random() * 5) + 1;
let ext = '.txt';
if (i === 0) {
ext = '.md';
}

let filePath = path.join(dirPath, `${getRandomNames(num)}${ext}`);
await fs.promises.writeFile(filePath, 'Test content');
testFilesMade++;
if (i === 19) {
oneFilePath = filePath;
}
}
console.log(coolText(`created ${testFilesMade} test files`));
return oneFilePath;
}
253 changes: 115 additions & 138 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import { expect } from 'chai'
import { describe, it } from 'mocha'
import test from 'node:test'
import __dirname from '../lib/__dirname.js'
import { createDirectoryWithFiles } from './createDirectoryWithFiles.js'
export async function deleteDirectoryAndFiles() {
try {
await fs.promises.rm(dirPath, { recursive: true })

const execa = async cmd => new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
throw new Error(error || stderr)
console.log(coolText('deleted test dir'))
} catch (e) {
// console.log(coolText('no folder to delete'))
}
resolve(stdout)
})
})
const coolText = chalk.bgBlue.black; //this part is important for the tests to run
const app = 'node ./cli.js';
const dirPath = path.resolve(path.join(__dirname, 'test-assets'))

const names = [
}
export const coolText = chalk.bgBlue.black //this part is important for the tests to run
export const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
export const names = [
'Persian',
'Siamese',
'Maine Coon',
Expand All @@ -44,139 +43,117 @@ const names = [
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
]

const containsThingsIDontWant = (p) => {
let base = path.basename(p)

}

async function deleteDirectoryAndFiles() {
try {
await fs.promises.rm(dirPath, { recursive: true })

console.log(coolText('deleted test dir'))
} catch (e) {
// console.log(coolText('no folder to delete'))
}
}

async function createDirectoryWithFiles() {
await deleteDirectoryAndFiles()
// Create directory
await fs.promises.mkdir(dirPath)
const main = async () => {
const execa = async (cmd) =>
new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
throw new Error(error || stderr)
}
resolve(stdout)
})
})
const app = 'node ./cli.js'

const getRandomNames = (num = 1) => {
let nameArr = []
for (let i = 0; i < num; i++) {
nameArr.push(names[Math.floor(Math.random() * names.length)])
//a function that returns true if the string contains no non-ascii characters and npo underscores
function noNonASCIIChars(str) {
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i)
if (charCode > 127 || charCode === 95) {
return false
}
}
return nameArr.join(' ')
return true
}
let arr = Array.from({ length: 20 }, (_, i) => i)
let testFilesMade = 0
let oneFilePath = ''
for await (let i of arr) {
const num = Math.floor(Math.random() * 5) + 1
let ext = '.txt'
if (i === 0) {
ext = '.md'

await test('test command line usage', async (t) => {
let failed = false
try {
let oneFilePath = await createDirectoryWithFiles()
await execa(`${app} "${oneFilePath}" --dryrun --rename --silent`)
} catch (e) {
console.error(e)
failed = true
}
assert.strictEqual(failed, false)
})

let filePath = path.join(dirPath, `${getRandomNames(num)}${ext}`)
await fs.promises.writeFile(filePath, 'Test content')
testFilesMade++
if (i === 19) {
oneFilePath = filePath
await test('create a folder and test renaming it', async (t) => {
let failed = false
try {
await deleteDirectoryAndFiles()
// Create directory
await fs.promises.mkdir(dirPath)
let theFolderInQuestion = path.join(dirPath, 'test-பூனைகள் fff')
let oneFilePath = await fs.promises.mkdir(theFolderInQuestion)
await execa(
`${app} "${theFolderInQuestion}" --dirs --rename --silent`
)
//get folders in the directory
let files = await fs.promises.readdir(dirPath)
//filter out files
console.log(files)
} catch (e) {
console.error(e)
failed = true
}
}
console.log(coolText(`created ${testFilesMade} test files`))
return oneFilePath
}
assert.strictEqual(failed, false)
})

test('test command line usage', async (t) => {
let failed = false;
try {
let oneFilePath = await createDirectoryWithFiles()
await execa(`${app} "${oneFilePath}" --dryrun --rename --silent`)
} catch (e) {
console.error(e)
failed = true
}
assert.strictEqual(failed, false)
})
test('test command line usage for extreme error', async (t) => {
let failed = false;
try {
console.log(coolText('Running tests with command line...'))
console.log(coolText('Creating directory with files...'))
let oneFilePath = await createDirectoryWithFiles()
let opts = { dryrun: false, rename: true, silent: true }
console.log(coolText('Options for the tests:'))
console.log(coolText(opts.toString()))
console.log(
coolText(`Running detawks against one file... (${oneFilePath}))`)
)
let testGlob = `${dirPath}/*.md`
console.log(coolText(`Running detawks with a glob... (${testGlob})`))
exec(
`${app} ${testGlob} --dryrun --rename --silent`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(stdout)
}
)
console.log(
coolText('Running detawks against the whole test directory...')
)
exec(
`${app} ${dirPath} --dryrun --rename --silent`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(stdout)
}
)
console.log(coolText('Deleting directory and files...'))
await test('test command line usage for extreme error', async (t) => {
let failed = false
try {
console.log(coolText('Running tests with command line...'))
console.log(coolText('Creating directory with files...'))
let oneFilePath = await createDirectoryWithFiles()
let opts = { dryrun: false, rename: true, silent: true }
console.log(coolText('Options for the tests:'))
console.log(coolText(opts.toString()))
console.log(
coolText(
`Running detawks against one file... (${oneFilePath}))`
)
)
let testGlob = `${dirPath}/*.md`
console.log(
coolText(`Running detawks with a glob... (${testGlob})`)
)
await execa(`${app} ${testGlob} --dryrun --rename --silent`)
console.log(
coolText('Running detawks against the whole test directory...')
)
await execa(`${app} ${dirPath} --dryrun --rename --silent`)
console.log(coolText('Deleting directory and files...'))

console.log(coolText('Tests finished!'))
} catch (e) {
console.error(e)
failed = true
}
assert.strictEqual(failed, false)
})
console.log(coolText('Tests finished!'))
} catch (e) {
console.error(e)
failed = true
}
assert.strictEqual(failed, false)
})

test('test basic string replacement', async (t) => {
const testCases = [
{
input: 'Hello World',
expected: 'hello-world',
},
{
input: 'Hello World',
expected: 'hello-world',
},
{
input: 'Hello World',
expected: 'hello-world',
},
]
await test('test basic string replacement', async (t) => {
const testCases = [
{
input: 'Hello World',
expected: 'hello-world',
},
{
input: 'Hello World',
expected: 'hello-world',
},
{
input: 'Hello World',
expected: 'hello-world',
},
]

for await (let testCase of testCases) {
const { input, expected } = testCase
const actual = slugify(input)
assert.strictEqual(expected, actual)
}
})
for await (let testCase of testCases) {
const { input, expected } = testCase
const actual = await slugify(input)
assert.strictEqual(expected, actual)
}
})
}
main()

0 comments on commit 95b5293

Please sign in to comment.