Skip to content

Commit

Permalink
#22: test: optimize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelzhang committed Nov 23, 2024
1 parent 1f3abf2 commit 335ba29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ If you have NPM (node) installed (RECOMMENDED):
npm i -g safe-rm
```

Or normally with `make`:
Or normally with `make` (not recommended, may be unstable):

```sh
make && sudo make install
Expand Down
15 changes: 7 additions & 8 deletions test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {v4: uuid} = require('uuid')
const delay = require('delay')

const {
generateContextMethods
generateContextMethods,
assertEmptySuccess
} = require('./helper')


Expand Down Expand Up @@ -60,17 +61,17 @@ module.exports = (

const filepath1 = await createFile(filename, '1')
const result1 = await runRm([filepath1])
t.is(result1.code, 0, 'exit code 1 should be 0')
assertEmptySuccess(t, result1)
t.false(await pathExists(filepath1), 'file 1 should be removed')

const filepath2 = await createFile(filename, '2')
const result2 = await runRm([filepath2])
t.is(result2.code, 0, 'exit code 2 should be 0')
assertEmptySuccess(t, result2)
t.false(await pathExists(filepath2), 'file 2 should be removed')

const filepath3 = await createFile(filename, '3')
const result3 = await runRm([filepath3])
t.is(result3.code, 0, 'exit code 3 should be 0')
assertEmptySuccess(t, result3)
t.false(await pathExists(filepath3), 'file 3 should be removed')

if (!test_safe_rm) {
Expand Down Expand Up @@ -110,7 +111,7 @@ module.exports = (
}
})

t.is(result.code, 0, 'exit code should be 0')
assertEmptySuccess(t, result)
t.false(await pathExists(filepath), 'file should be removed')

if (!test_safe_rm) {
Expand All @@ -134,9 +135,7 @@ module.exports = (
const filepath = path.join(source_path, uuid())
const result = await runRm(['-f', filepath])

t.is(result.code, 0, 'if rm -f a non-existing file, exit code should be 0')
t.is(result.stdout, '', 'stdout should be empty')
t.is(result.stderr, '', 'stderr should be empty')
assertEmptySuccess(t, result, ', if rm -f a non-existing file')

const result_no_f = await runRm([filepath])

Expand Down
9 changes: 8 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ const generateContextMethods = (rm_command = SAFE_RM_PATH) => async t => {
})
}

const assertEmptySuccess = (t, result, a = '', b = '', c = '') => {
t.is(result.code, 0, 'exit code should be 0' + a)
t.is(result.stdout, '', 'stdout should be empty' + b)
t.is(result.stderr, '', 'stderr should be empty' + c)
}

module.exports = {
generateContextMethods
generateContextMethods,
assertEmptySuccess
}

0 comments on commit 335ba29

Please sign in to comment.