Skip to content

Commit

Permalink
misc and testing fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotberry committed Apr 25, 2024
1 parent 6e613a8 commit 4ff593e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 79 deletions.
7 changes: 3 additions & 4 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import stylistic from '@stylistic/eslint-plugin'
import pluginImport from 'eslint-plugin-import'
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import prettierConfig from 'eslint-plugin-prettier/recommended'
import promise from 'eslint-plugin-promise'
import is from 'eslint-plugin-simple-import-sort'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'

export default [
prettierConfig,
eslintPluginUnicorn.configs['flat/recommended'],
perfectionistNatural,
stylistic.configs['recommended-flat'],

{
files: ['**/*.js'],
languageOptions: {
Expand All @@ -20,8 +20,7 @@ export default [
import: pluginImport,
pluginImport,
promise,
'simple-import-sort': is

'simple-import-sort': is,
},

rules: {
Expand Down
21 changes: 7 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import chalk from 'chalk'

import { baseLog } from './lib/closest-parent-info.js'
import getFilePathInfo from './lib/file-path-info.js'
import ignore from './lib/ignore.js'
Expand All @@ -11,33 +9,29 @@ import useFdir from './lib/useFdir.js'
const logIgnored = (arrayOfFilePaths, lengthBefore, options, files) => {
if (arrayOfFilePaths.length === 0) {
!global.silent &&
console.log(chalk.blue(`no files to rename, as all were ignored.`))
console.log(`no files to rename, as all were ignored.`)
throw new Error('no files to rename.')
}
!global.silent &&
console.log(`ignored ${lengthBefore - arrayOfFilePaths.length} files.`)
!global.silent &&
console.log(
chalk.blue(
`${arrayOfFilePaths.length}/${files.length} ${
options.dryrun ? `would` : `will`
} be renamed.`
)
`${arrayOfFilePaths.length}/${files.length} ${
options.dryrun ? `would` : `will`
} be renamed.`
)
}
const filesFoundInfo = (files, inputString) => {
const numberFilesFoundInGlob = files.length
if (numberFilesFoundInGlob === 1) {
!global.silent && console.log(chalk.green(`found a file: ${files[0]}.`))
!global.silent && console.log(`found a file: ${files[0]}.`)
} else {
!global.silent &&
console.log(
chalk.green(
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
)
`found ${numberFilesFoundInGlob} files in ${inputString.path}.`
)
if (numberFilesFoundInGlob === 0) {
!global.silent && console.log(chalk.red(`thus, exiting.`))
!global.silent && console.log(`thus, exiting.`)
throw new Error('no files found.')
}
}
Expand Down Expand Up @@ -105,7 +99,6 @@ const run = async (globPattern, userOptions) => {
await processOneFile(file, options.rename)
await baseLog(file)
}
// await logChanges(arrayOfFilePaths)
}
}
export default run
4 changes: 2 additions & 2 deletions lib/file-path-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const getFilePathInfo = async (

const newPath = path.join(currentPath.dir, newName + newExtension)
return {
oldParsed: currentPath,
new: newPath,
newParsed: path.parse(newPath),
old: path.join(currentPath.dir, currentPath.name + currentPath.ext),
new: newPath,
oldParsed: currentPath,
}
} catch (error) {
!global.silent &&
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dependencies": {
"chalk": "^5.3.0",
"change-case": "^5.4.4",
"elliotisms": "^0.0.14",
"fdir": "^6.1.1",
"picomatch": "^4.0.2",
"rc": "^1.2.8",
Expand All @@ -64,6 +65,7 @@
"eslint-plugin-perfectionist": "^2.9.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-security-node": "^1.1.4",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-unicorn": "^52.0.0",
"prettier": "^3.2.5",
Expand Down
30 changes: 29 additions & 1 deletion test/createDirectoryWithFiles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import fs from 'fs';
import path from 'path';
import { deleteDirectoryAndFiles, dirPath, names, coolText } from './test.js';
import { deleteDirectoryAndFiles, dirPath } from './test.js';
async function deleteDirectoryAndFiles() {
try {
await fs.promises.rm(dirPath, { recursive: true })

console.log('deleted test dir')
} catch {
// console.log(coolText('no folder to delete'))
}
}
export const names = [
'Persian',
'Siamese',
'Maine Coon',
'Ragdoll',
'Sphynx',
'Bengal',
'Abyssinian',
'British Shorthair',
'Scottish Fold',
'Burmese',
'Oriental',
'Siberian',
'Tonkinese',
'Russian Blue',
'Norwegian Forest',
'кошки',
'பூனைகள்',
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
]
export async function createDirectoryWithFiles() {
await deleteDirectoryAndFiles();
// Create directory
Expand Down
85 changes: 27 additions & 58 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,33 @@
import fs from 'fs'
import path from 'path'
import detawks from '../index.js'
import chalk from 'chalk'
import { slugify } from '../lib/slugify.js'
import { exec } from 'node:child_process'
import assert from 'assert'
import { fileURLToPath } from 'url'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import chalk from 'chalk'
import fs from 'node:fs'
import execa from 'elliotisms/lib/exec.js'
import test from 'node:test'
import path from 'path'

import __dirname from '../lib/__dirname.js'
import { slugify } from '../lib/slugify.js'
import { createDirectoryWithFiles } from './createDirectoryWithFiles.js'

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

console.log(coolText('deleted test dir'))
} catch (e) {
console.log('deleted test dir')
} catch {
// console.log(coolText('no folder to delete'))
}
}
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',
'Ragdoll',
'Sphynx',
'Bengal',
'Abyssinian',
'British Shorthair',
'Scottish Fold',
'Burmese',
'Oriental',
'Siberian',
'Tonkinese',
'Russian Blue',
'Norwegian Forest',
'кошки',
'பூனைகள்',
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
]

export const dirPath = path.resolve(path.join(__dirname, 'test-assets'))
console.log(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'

//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)
function noNonASCIIChars(string_) {
for (let index = 0; index < string_.length; index++) {
const charCode = string_.charCodeAt(index)
if (charCode > 127 || charCode === 95) {
return false
}
Expand All @@ -71,8 +40,8 @@ const main = async () => {
try {
let oneFilePath = await createDirectoryWithFiles()
await execa(`${app} "${oneFilePath}" --dryrun --rename --silent`)
} catch (e) {
console.error(e)
} catch (error) {
console.error(error)
failed = true
}
assert.strictEqual(failed, false)
Expand All @@ -93,8 +62,8 @@ const main = async () => {
let files = await fs.promises.readdir(dirPath)
//filter out files
console.log(files)
} catch (e) {
console.error(e)
} catch (error) {
console.error(error)
failed = true
}
assert.strictEqual(failed, false)
Expand All @@ -106,9 +75,9 @@ const main = async () => {
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 }
let options = { dryrun: false, rename: true, silent: true }
console.log(coolText('Options for the tests:'))
console.log(coolText(opts.toString()))
console.log(coolText(options.toString()))
console.log(
coolText(
`Running detawks against one file... (${oneFilePath}))`
Expand All @@ -126,8 +95,8 @@ const main = async () => {
console.log(coolText('Deleting directory and files...'))

console.log(coolText('Tests finished!'))
} catch (e) {
console.error(e)
} catch (error) {
console.error(error)
failed = true
}
assert.strictEqual(failed, false)
Expand All @@ -136,24 +105,24 @@ const main = async () => {
await test('test basic string replacement', async (t) => {
const testCases = [
{
input: 'Hello World',
expected: 'hello-world',
input: 'Hello World',
},
{
input: 'Hello World',
expected: 'hello-world',
input: 'Hello World',
},
{
input: 'Hello World',
expected: 'hello-world',
input: 'Hello World',
},
]

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

10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,11 @@ electron-to-chromium@^1.4.668:
resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.747.tgz#e37fa5b7b7e4c22607c5f59b5cf78f947266e77d"
integrity sha512-+FnSWZIAvFHbsNVmUxhEqWiaOiPMcfum1GQzlWCg/wLigVtshOsjXHyEFfmt6cFK6+HkS3QOJBv6/3OPumbBfw==

elliotisms@^0.0.14:
version "0.0.14"
resolved "https://registry.npmjs.org/elliotisms/-/elliotisms-0.0.14.tgz#b87f733a7b1014be0ca08fa357ee2b93d683eb19"
integrity sha512-NEheJmioYy/dxXW3MzfFLVYpa21sQXxaje8P4xrsbF/WT7cskIPF9gJKgnJ7r4aOpV+QrVB7m1ksJukRB4uH+w==

emoji-regex@^10.3.0:
version "10.3.0"
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.3.0.tgz#76998b9268409eb3dae3de989254d456e70cfe23"
Expand Down Expand Up @@ -1316,6 +1321,11 @@ eslint-plugin-promise@^6.1.1:
resolved "https://registry.npmjs.org/eslint-plugin-promise/-/eslint-plugin-promise-6.1.1.tgz#269a3e2772f62875661220631bd4dafcb4083816"
integrity sha512-tjqWDwVZQo7UIPMeDReOpUgHCmCiH+ePnVT+5zVapL0uuHnegBUs2smM13CzOs2Xb5+MHMRFTs9v24yjba4Oig==

eslint-plugin-security-node@^1.1.4:
version "1.1.4"
resolved "https://registry.npmjs.org/eslint-plugin-security-node/-/eslint-plugin-security-node-1.1.4.tgz#e78dbfa9d4852076d51ca512a28d6b4ead069792"
integrity sha512-8+agTMb2glNbP1zFhqo/Ixwtz16Hn0TvJW5KgpoHkAzGjDUhQf9iT+D6OgbhvZCMWRKMjc+5FbJ2Lh0UEUz7fQ==

eslint-plugin-simple-import-sort@^12.1.0:
version "12.1.0"
resolved "https://registry.npmjs.org/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-12.1.0.tgz#8186ad55474d2f5c986a2f1bf70625a981e30d05"
Expand Down

0 comments on commit 4ff593e

Please sign in to comment.