summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/gently-rm-cmdshims.js
blob: 304c6956bdfd60593bc30ccf9fab5d2ff743b523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
/* eslint-disable camelcase */
var path = require('path')
var fs = require('graceful-fs')
var test = require('tap').test
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var npm = require('../../lib/npm.js')

var work = path.join(__dirname, path.basename(__filename, '.js'))
var doremove = path.join(work, 'doremove')
var dontremove = path.join(work, 'dontremove')
var example_json = {
  name: 'example',
  version: '1.0.0',
  bin: {
    'example': 'example.js'
  }
}
var example_bin =
  '#!/usr/bin/env node\n' +
  'true\n'

// NOTE: if this were actually produced on windows it would be \ not / of
// course, buuut, path.resolve doesn't understand \ outside of windows =/
var do_example_cmd =
  '@IF EXIST "%~dp0\\node.exe" (\n' +
  '  "%~dp0\\node.exe"  "%~dp0\\../example/example.js" %*\n' +
  ') ELSE (\n' +
  '  @SETLOCAL\n' +
  '  @SET PATHEXT=%PATHEXT:;.JS;=;%\n' +
  '  node  "%~dp0\\../example/example.js" %*\n' +
  ')\n'

var do_example_cygwin =
  '#!/bin/sh\n' +
  'basedir=`dirname "$0"`\n' +
  '\n' +
  'case `uname` in\n' +
  '    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;\n' +
  'esac\n' +
  '\n' +
  'if [ -x "$basedir/node" ]; then\n' +
  '  "$basedir/node"  "$basedir/../example/example.js" "$@"\n' +
  '  ret=$?\n' +
  'else\n' +
  '  node  "$basedir/../example/example.js" "$@"\n' +
  '  ret=$?\n' +
  'fi\n' +
  'exit $ret\n'

var dont_example_cmd =
  '@IF EXIST "%~dp0\\node.exe" (\n' +
  '  "%~dp0\\node.exe"  "%~dp0\\../example-other/example.js" %*\n' +
  ') ELSE (\n' +
  '  @SETLOCAL\n' +
  '  @SET PATHEXT=%PATHEXT:;.JS;=;%\n' +
  '  node  "%~dp0\\../example-other/example.js" %*\n' +
  ')\n'

var dont_example_cygwin =
  '#!/bin/sh\n' +
  'basedir=`dirname "$0"`\n' +
  '\n' +
  'case `uname` in\n' +
  '    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;\n' +
  'esac\n' +
  '\n' +
  'if [ -x "$basedir/node" ]; then\n' +
  '  "$basedir/node"  "$basedir/../example-other/example.js" "$@"\n' +
  '  ret=$?\n' +
  'else\n' +
  '  node  "$basedir/../example-other/example.js" "$@"\n' +
  '  ret=$?\n' +
  'fi\n' +
  'exit $ret\n'

function cleanup () {
  rimraf.sync(work)
}

var doremove_module = path.join(doremove, 'node_modules', 'example')
var doremove_example_cmd = path.join(doremove, 'node_modules', '.bin', 'example.cmd')
var doremove_example_cygwin = path.join(doremove, 'node_modules', '.bin', 'example')
var dontremove_module = path.join(dontremove, 'node_modules', 'example')
var dontremove_example_cmd = path.join(dontremove, 'node_modules', '.bin', 'example.cmd')
var dontremove_example_cygwin = path.join(dontremove, 'node_modules', '.bin', 'example')

function setup () {
  mkdirp.sync(doremove_module)
  mkdirp.sync(path.join(doremove, 'node_modules', '.bin'))
  fs.writeFileSync(path.join(doremove, 'node_modules', 'example', 'package.json'), JSON.stringify(example_json))
  fs.writeFileSync(path.join(doremove, 'node_modules', 'example', 'example.js'), JSON.stringify(example_bin))
  fs.writeFileSync(doremove_example_cmd, do_example_cmd)
  fs.writeFileSync(doremove_example_cygwin, do_example_cygwin)

  mkdirp.sync(dontremove_module)
  mkdirp.sync(path.join(dontremove, 'node_modules', '.bin'))
  fs.writeFileSync(path.join(dontremove, 'node_modules', 'example', 'package.json'), JSON.stringify(example_json))
  fs.writeFileSync(path.join(dontremove, 'node_modules', 'example', 'example.js'), JSON.stringify(example_bin))
  fs.writeFileSync(dontremove_example_cmd, dont_example_cmd)
  fs.writeFileSync(dontremove_example_cygwin, dont_example_cygwin)
}

test('setup', function (t) {
  cleanup()
  setup()
  npm.load({}, function () {
    t.done()
  })
})

// Like slide.chain, but runs all commands even if they have errors, also
// throws away results.
function runAll (cmds, done) {
  runNext()
  function runNext () {
    if (cmds.length === 0) return done()
    var cmdline = cmds.shift()
    var cmd = cmdline.shift()
    cmdline.push(runNext)
    cmd.apply(null, cmdline)
  }
}

test('remove-cmd-shims', function (t) {
  t.plan(2)

  var gentlyRm = require('../../lib/utils/gently-rm.js')
  runAll([ [gentlyRm, doremove_example_cmd, true, doremove_module],
    [gentlyRm, doremove_example_cygwin, true, doremove_module] ],
  function () {
    fs.stat(doremove_example_cmd, function (er) {
      t.is(er && er.code, 'ENOENT', 'cmd-shim was removed')
    })
    fs.stat(doremove_example_cygwin, function (er) {
      t.is(er && er.code, 'ENOENT', 'cmd-shim cygwin script was removed')
    })
  })
})

test('dont-remove-cmd-shims', function (t) {
  t.plan(2)
  var gentlyRm = require('../../lib/utils/gently-rm.js')
  runAll([ [gentlyRm, dontremove_example_cmd, true, dontremove_module],
    [gentlyRm, dontremove_example_cygwin, true, dontremove_module] ],
  function () {
    fs.stat(dontremove_example_cmd, function (er) {
      t.is(er, null, 'cmd-shim was not removed')
    })
    fs.stat(dontremove_example_cygwin, function (er) {
      t.is(er, null, 'cmd-shim cygwin script was not removed')
    })
  })
})

test('cleanup', function (t) {
  cleanup()
  t.done()
})