Skip to content

Commit 3ca257f

Browse files
committed
#44: adds test cases
1 parent 25a89b4 commit 3ca257f

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

bin/rm.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ remove(){
389389
read answer
390390
if [[ ${answer:0:1} =~ [yY] ]]; then
391391
[[ $(ls -A "$file") ]] && {
392+
debug "$LINENO: $file: Directory not empty: $(ls -A "$file")"
393+
392394
echo "$COMMAND: $file: Directory not empty"
393395

394396
return 1

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"safe-rm": "./bin/rm.sh"
77
},
88
"scripts": {
9-
"test": "ava --timeout=10s --verbose",
9+
"test:rm": "ava --timeout=10s --verbose test/safe-rm.test.js",
10+
"test:as": "ava --timeout=10s --verbose test/safe-rm-as.test.js",
11+
"test": "npm run test:rm && npm run test:as",
1012
"test:dev": "NODE_DEBUG=safe-rm npm run test",
1113
"test:debug": "SAFE_RM_DEBUG=1 npm run test:dev",
1214
"test:mock-linux": "SAFE_RM_DEBUG_LINUX=1 npm run test",

test/cases.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,46 @@ module.exports = (
272272
t.true(result.stderr.includes('protected'), 'stderr should include "protected"')
273273
t.true(await pathExists(filepath), 'file should not be removed')
274274
})
275+
276+
!is_as(type) && test(`#44: recursively and interactively`, async t => {
277+
const {
278+
createDir,
279+
createFile,
280+
runRm,
281+
pathExists
282+
} = t.context
283+
284+
const name = 'foo'
285+
286+
const dir = await createDir({
287+
name
288+
})
289+
290+
const fileA = await createFile({
291+
name: 'a',
292+
under: dir
293+
})
294+
295+
const fileB = await createFile({
296+
name: 'b',
297+
under: dir
298+
})
299+
300+
const result = await runRm(['-ri', dir], {
301+
input: ['y', 'y', 'y', 'y']
302+
})
303+
304+
t.is(result.stderr, '', 'stderr should be empty')
305+
t.is(result.code, 0, 'exit code should be 0')
306+
t.is(
307+
result.stdout,
308+
`examine files in directory ${dir}? y
309+
remove ${fileA}? y
310+
remove ${fileB}? y
311+
remove ${dir}? y
312+
`,
313+
'stdout does not match'
314+
)
315+
// t.false(await pathExists(dir), 'directory should be removed')
316+
})
275317
}

test/helper.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ const generateContextMethods = (
5858

5959
// Helper function to run rm commands
6060
function runRm (args, {
61-
input = '',
61+
input = [],
6262
command = rm_command,
6363
env: arg_env = {}
6464
} = {}) {
65-
return new Promise(resolve => {
65+
return new Promise((resolve, reject) => {
6666
const env = {
67-
// ...process.env,
67+
...process.env,
6868
...{
6969
SAFE_RM_TRASH: t.context.trash_path
7070
},
@@ -80,21 +80,33 @@ const generateContextMethods = (
8080

8181
child.stdout.on('data', data => {
8282
stdout += data.toString()
83+
84+
if (/\?\s*$/.test(stdout)) {
85+
if (!child.stdin) {
86+
reject(new Error('Child process does not support stdin'))
87+
return
88+
}
89+
90+
const this_input = input.shift()
91+
92+
if (!this_input) {
93+
reject(new Error('Need more input'))
94+
return
95+
}
96+
97+
child.stdin.write(`${this_input}\n`)
98+
stdout += `${this_input}\n`
99+
100+
if (input.length === 0) {
101+
child.stdin.end()
102+
}
103+
}
83104
})
84105

85106
child.stderr.on('data', data => {
86107
stderr += data.toString()
87108
})
88109

89-
if (input) {
90-
if (!child.stdin) {
91-
throw new Error('Child process does not support stdin')
92-
}
93-
94-
child.stdin.write(input)
95-
child.stdin.end()
96-
}
97-
98110
child.on('close', code => {
99111
const resolved = {
100112
code,
@@ -105,6 +117,7 @@ const generateContextMethods = (
105117
log(command, 'result:', resolved)
106118

107119
resolve(resolved)
120+
child.kill()
108121
})
109122
})
110123
}

0 commit comments

Comments
 (0)