safe-rm 3.1.1 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/rm.sh CHANGED
@@ -43,7 +43,7 @@ debug(){
43
43
 
44
44
 
45
45
  error(){
46
- echo $@ >&2
46
+ echo "$@" >&2
47
47
  }
48
48
 
49
49
 
@@ -389,6 +389,8 @@ remove(){
389
389
  read answer
390
390
  if [[ ${answer:0:1} =~ [yY] ]]; then
391
391
  [[ $(ls -A "$file") ]] && {
392
+ debug "$LINENO: $file: Directory not empty: $(ls -A "$file")"
393
+
392
394
  echo "$COMMAND: $file: Directory not empty"
393
395
 
394
396
  return 1
@@ -434,7 +436,9 @@ recursive_remove(){
434
436
  # never use `find $1`, for the searching order is not what we want
435
437
  local list=$(ls -A "$1")
436
438
 
437
- [[ -n $list ]] && for path in "$list"; do
439
+ [[ -n $list ]] && for path in $list; do
440
+ debug "$LINENO: recursively remove: $1/$path"
441
+
438
442
  remove "$1/$path"
439
443
  done
440
444
  }
@@ -630,7 +634,7 @@ check_linux_trash_base(){
630
634
  local num=
631
635
 
632
636
  while IFS= read -r file; do
633
- if [[ $file =~ ${filename}\.([0-9]+)$ ]]; then
637
+ if [[ $file =~ ${base}\.([0-9]+)$ ]]; then
634
638
  # Remove leading zeros and make sure the number is in base 10
635
639
  num=$((10#${BASH_REMATCH[1]}))
636
640
  if ((num > max_n)); then
@@ -688,7 +692,7 @@ list_files(){
688
692
  local list=$(ls -A "$1")
689
693
  local f
690
694
 
691
- [[ -n $list ]] && for f in "$list"; do
695
+ [[ -n $list ]] && for f in $list; do
692
696
  list_files "$1/$f"
693
697
  done
694
698
  fi
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "safe-rm",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "description": "A much safer replacement of bash rm with nearly full functionalities and options of the rm command!",
5
5
  "bin": {
6
6
  "safe-rm": "./bin/rm.sh"
7
7
  },
8
8
  "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",
10
12
  "test:dev": "NODE_DEBUG=safe-rm npm run test",
11
13
  "test:debug": "SAFE_RM_DEBUG=1 npm run test:dev",
12
14
  "test:mock-linux": "SAFE_RM_DEBUG_LINUX=1 npm run test",
package/test/cases.js CHANGED
@@ -272,4 +272,47 @@ module.exports = (
272
272
  t.true(result.stderr.includes('protected'), 'stderr should include "protected"')
273
273
  t.true(await pathExists(filepath), 'file should not be removed')
274
274
  })
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
+
316
+ t.false(await pathExists(dir), 'directory should be removed')
317
+ })
275
318
  }
package/test/helper.js CHANGED
@@ -58,13 +58,13 @@ const generateContextMethods = (
58
58
 
59
59
  // Helper function to run rm commands
60
60
  function runRm (args, {
61
- input = '',
61
+ input = [],
62
62
  command = rm_command,
63
63
  env: arg_env = {}
64
64
  } = {}) {
65
- return new Promise(resolve => {
65
+ return new Promise((resolve, reject) => {
66
66
  const env = {
67
- // ...process.env,
67
+ ...process.env,
68
68
  ...{
69
69
  SAFE_RM_TRASH: t.context.trash_path
70
70
  },
@@ -80,21 +80,33 @@ const generateContextMethods = (
80
80
 
81
81
  child.stdout.on('data', data => {
82
82
  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
+ }
83
104
  })
84
105
 
85
106
  child.stderr.on('data', data => {
86
107
  stderr += data.toString()
87
108
  })
88
109
 
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
-
98
110
  child.on('close', code => {
99
111
  const resolved = {
100
112
  code,
@@ -105,6 +117,7 @@ const generateContextMethods = (
105
117
  log(command, 'result:', resolved)
106
118
 
107
119
  resolve(resolved)
120
+ child.kill()
108
121
  })
109
122
  })
110
123
  }
package/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: bash
2
-
3
- script:
4
- - make test