Skip to content

Commit 9891d48

Browse files
committed
halfassed testing suite
1 parent 4929230 commit 9891d48

File tree

9 files changed

+354
-58
lines changed

9 files changed

+354
-58
lines changed

.github/workflows/nodejs-tests.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Node.js Test Workflow
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Check out repository
15+
uses: actions/checkout@v2
16+
17+
- name: Set up Node.js
18+
uses: actions/setup-node@v2
19+
with:
20+
node-version: '18' # or the version you need
21+
22+
- name: Install dependencies
23+
run: npm install
24+
25+
- name: Run tests
26+
run: npm test

lib/file-path-info.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import path from 'path';
22
import {slugify} from './slugify.js';
33
import chalk from 'chalk';
44

5-
6-
75
// Takes a file path and an optional boolean flag as input.
86
// Extracts information about the file, such as the directory, file name, and file extension.
97
// Creates a new file name that is a URL-friendly version of the original file name and returns an object that contains the old file path and the new file path if the new file name is different from the original file name. Otherwise, it returns null.

lib/ignore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from 'path'
1+
22
import config from './rc.js'
33
let ignoreFn
44

lib/log-changes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import os from 'os';
44
import chalk from 'chalk';
55

6-
var silent = global.silent
6+
77

88
const logDir = path.join(os.homedir(), '.config');
99

lib/path-validation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path'
22
import fs from 'fs'
3-
4-
3+
import chalk from 'chalk'
54
export const truncatePathIfPossible = (str, parentDir) => {
65
if (parentDir) {
76
let truncated = str.split(parentDir)[1]
@@ -84,9 +83,9 @@ export const validateAndFormatInput = async(inputStr) => {
8483
} else if (type === 'glob') {
8584
return { path: inputStr, type: 'glob' }
8685
} else {
87-
throw new Error(`invalid input: ${inputStr}`)
86+
throw new Error(chalk.red(`invalid input: ${inputStr}`))
8887
}
8988
} catch (e) {
90-
throw new Error(`invalid input: ${inputStr}`)
89+
throw new Error(chalk.red(`invalid input: ${inputStr}`))
9190
}
9291
}

lib/process-one-file.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import fs from 'fs'
22
import fsp from 'node:fs/promises'
33
import path from 'path'
4-
5-
const processOneFile = async (f, rename=true, fixTildes=false) => {
4+
import chalk from 'chalk'
5+
const processOneFile = async (fileObj, rename=true, fixTildes=false) => {
66
try {
77

8-
!global.silent && console.log(`${f.old} -> ${f.new}`)
8+
!global.silent && console.log(`${fileObj.old} -> ${fileObj.new}`)
99

10-
if (fs.existsSync(f.new)) {
10+
if (fs.existsSync(fileObj.new)) {
1111
if (rename === true) {
12-
//ambiguous !global.silent && console.log(`file will be renamed: ${f.new}`)
12+
//ambiguous !global.silent && console.log(`file will be renamed: ${fileObj.new}`)
1313
let i = 1
14-
let parsed = path.parse(f.new)
14+
let parsed = path.parse(fileObj.new)
1515
let newName = `${parsed.name}-${i}${parsed.ext}`
1616
while (fs.existsSync(path.join(parsed.dir, newName))) {
1717
i++
1818
newName = `${parsed.name}-${i}${parsed.ext}`
1919
}
20-
f.new = path.resolve(path.join(parsed.dir, newName))
21-
await fsp.rename(f.old, f.new)
20+
fileObj.new = path.resolve(path.join(parsed.dir, newName))
21+
await fsp.rename(fileObj.old, fileObj.new)
2222
} else {
23-
throw new Error(`file already exists, doing nothing: ${f.new}`)
23+
throw new Error(chalk.red(`file already exists, doing nothing: ${fileObj.new}`))
2424
}
2525
} else {
26-
await fsp.rename(f.old, f.new)
26+
await fsp.rename(fileObj.old, fileObj.new)
2727
}
2828
} catch (e) {
29-
console.log(`error processing ${f.old}: ${e}`)
29+
console.log(chalk.orange(`error processing ${fileObj.old}: ${e}`))
3030
}
3131
}
3232

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@
6969
"singleQuote": true
7070
},
7171
"devDependencies": {
72+
"chai": "^4.3.10",
7273
"eslint": "^8.50.0",
7374
"eslint-config-prettier": "^9.0.0",
75+
"mocha": "^10.2.0",
7476
"prettier": "^3.0.3",
7577
"release-it": "^16.2.1"
7678
},

test.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import detawks from './index.js'
44
import chalk from 'chalk'
55
import { slugify } from './lib/slugify.js'
66
import { exec } from 'child_process'
7-
7+
import assert from 'assert'
88
import { fileURLToPath } from 'url'
9-
9+
import { expect } from 'chai'
10+
import { describe, it } from 'mocha'
11+
import test from 'node:test'
1012
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1113
//this part is important for the tests to run
1214
var coolText = chalk.bgBlue.black
@@ -33,7 +35,7 @@ const names = [
3335
'✨🌀🌈🐱‍👤🐱‍🚀🐱‍🐉🐱‍💻👾🎃🕺💃🎉🎲🎸🚀🌠🌌🔮💎🎭🎨🖖🌀✨',
3436
]
3537

36-
const testSlugify = async () => {
38+
test('test strings', async(t) => {
3739
const testCases = [
3840
{
3941
input: 'Hello World',
@@ -48,16 +50,13 @@ const testSlugify = async () => {
4850
expected: 'hello-world',
4951
},
5052
]
53+
5154
for await (let testCase of testCases) {
5255
const { input, expected } = testCase
5356
const actual = slugify(input)
54-
if (actual !== expected) {
55-
console.log(`expected ${expected} but got ${actual}`)
56-
} else {
57-
console.log(`passed test: ${input} -> ${actual}`)
58-
}
57+
assert.strictEqual(expected, actual)
5958
}
60-
}
59+
});
6160

6261
async function deleteDirectoryAndFiles() {
6362
try {
@@ -121,7 +120,9 @@ const withAPI = async () => {
121120
console.log(coolText('Tests finished!'))
122121
}
123122

124-
const withCommandLine = async () => {
123+
test('test command line usage for extreme error', async(t) => {
124+
var failed = false
125+
try {
125126
console.log(coolText('Running tests with command line...'))
126127
console.log(coolText('Creating directory with files...'))
127128
let oneFilePath = await createDirectoryWithFiles()
@@ -179,11 +180,9 @@ const withCommandLine = async () => {
179180
console.log(coolText('Deleting directory and files...'))
180181

181182
console.log(coolText('Tests finished!'))
182-
}
183-
184-
const main = async () => {
185-
await testSlugify()
186-
await withAPI()
187-
await withCommandLine()
188-
}
189-
main()
183+
} catch (e) {
184+
console.error(e)
185+
failed = true
186+
}
187+
assert.strictEqual(failed, false)
188+
});

0 commit comments

Comments
 (0)