• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2/* eslint-disable camelcase */
3var path = require('path')
4var fs = require('graceful-fs')
5var test = require('tap').test
6var mkdirp = require('mkdirp')
7var rimraf = require('rimraf')
8var npm = require('../../lib/npm.js')
9
10const common = require('../common-tap.js')
11var work = common.pkg
12var doremove = path.join(work, 'doremove')
13var dontremove = path.join(work, 'dontremove')
14var example_json = {
15  name: 'example',
16  version: '1.0.0',
17  bin: {
18    'example': 'example.js'
19  }
20}
21var example_bin =
22  '#!/usr/bin/env node\n' +
23  'true\n'
24
25// NOTE: if this were actually produced on windows it would be \ not / of
26// course, buuut, path.resolve doesn't understand \ outside of windows =/
27var do_example_cmd =
28  '@IF EXIST "%~dp0\\node.exe" (\n' +
29  '  "%~dp0\\node.exe"  "%~dp0\\../example/example.js" %*\n' +
30  ') ELSE (\n' +
31  '  @SETLOCAL\n' +
32  '  @SET PATHEXT=%PATHEXT:;.JS;=;%\n' +
33  '  node  "%~dp0\\../example/example.js" %*\n' +
34  ')\n'
35
36var do_example_cygwin =
37  '#!/bin/sh\n' +
38  'basedir=`dirname "$0"`\n' +
39  '\n' +
40  'case `uname` in\n' +
41  '    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;\n' +
42  'esac\n' +
43  '\n' +
44  'if [ -x "$basedir/node" ]; then\n' +
45  '  "$basedir/node"  "$basedir/../example/example.js" "$@"\n' +
46  '  ret=$?\n' +
47  'else\n' +
48  '  node  "$basedir/../example/example.js" "$@"\n' +
49  '  ret=$?\n' +
50  'fi\n' +
51  'exit $ret\n'
52
53var dont_example_cmd =
54  '@IF EXIST "%~dp0\\node.exe" (\n' +
55  '  "%~dp0\\node.exe"  "%~dp0\\../example-other/example.js" %*\n' +
56  ') ELSE (\n' +
57  '  @SETLOCAL\n' +
58  '  @SET PATHEXT=%PATHEXT:;.JS;=;%\n' +
59  '  node  "%~dp0\\../example-other/example.js" %*\n' +
60  ')\n'
61
62var dont_example_cygwin =
63  '#!/bin/sh\n' +
64  'basedir=`dirname "$0"`\n' +
65  '\n' +
66  'case `uname` in\n' +
67  '    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;\n' +
68  'esac\n' +
69  '\n' +
70  'if [ -x "$basedir/node" ]; then\n' +
71  '  "$basedir/node"  "$basedir/../example-other/example.js" "$@"\n' +
72  '  ret=$?\n' +
73  'else\n' +
74  '  node  "$basedir/../example-other/example.js" "$@"\n' +
75  '  ret=$?\n' +
76  'fi\n' +
77  'exit $ret\n'
78
79function cleanup () {
80  rimraf.sync(work)
81}
82
83var doremove_module = path.join(doremove, 'node_modules', 'example')
84var doremove_example_cmd = path.join(doremove, 'node_modules', '.bin', 'example.cmd')
85var doremove_example_cygwin = path.join(doremove, 'node_modules', '.bin', 'example')
86var dontremove_module = path.join(dontremove, 'node_modules', 'example')
87var dontremove_example_cmd = path.join(dontremove, 'node_modules', '.bin', 'example.cmd')
88var dontremove_example_cygwin = path.join(dontremove, 'node_modules', '.bin', 'example')
89
90function setup () {
91  mkdirp.sync(doremove_module)
92  mkdirp.sync(path.join(doremove, 'node_modules', '.bin'))
93  fs.writeFileSync(path.join(doremove, 'node_modules', 'example', 'package.json'), JSON.stringify(example_json))
94  fs.writeFileSync(path.join(doremove, 'node_modules', 'example', 'example.js'), JSON.stringify(example_bin))
95  fs.writeFileSync(doremove_example_cmd, do_example_cmd)
96  fs.writeFileSync(doremove_example_cygwin, do_example_cygwin)
97
98  mkdirp.sync(dontremove_module)
99  mkdirp.sync(path.join(dontremove, 'node_modules', '.bin'))
100  fs.writeFileSync(path.join(dontremove, 'node_modules', 'example', 'package.json'), JSON.stringify(example_json))
101  fs.writeFileSync(path.join(dontremove, 'node_modules', 'example', 'example.js'), JSON.stringify(example_bin))
102  fs.writeFileSync(dontremove_example_cmd, dont_example_cmd)
103  fs.writeFileSync(dontremove_example_cygwin, dont_example_cygwin)
104}
105
106test('setup', function (t) {
107  cleanup()
108  setup()
109  npm.load({}, function () {
110    t.done()
111  })
112})
113
114// Like slide.chain, but runs all commands even if they have errors, also
115// throws away results.
116function runAll (cmds, done) {
117  runNext()
118  function runNext () {
119    if (cmds.length === 0) return done()
120    var cmdline = cmds.shift()
121    var cmd = cmdline.shift()
122    cmdline.push(runNext)
123    cmd.apply(null, cmdline)
124  }
125}
126
127test('remove-cmd-shims', function (t) {
128  t.plan(2)
129
130  var gentlyRm = require('../../lib/utils/gently-rm.js')
131  runAll([ [gentlyRm, doremove_example_cmd, true, doremove_module],
132    [gentlyRm, doremove_example_cygwin, true, doremove_module] ],
133  function () {
134    fs.stat(doremove_example_cmd, function (er) {
135      t.is(er && er.code, 'ENOENT', 'cmd-shim was removed')
136    })
137    fs.stat(doremove_example_cygwin, function (er) {
138      t.is(er && er.code, 'ENOENT', 'cmd-shim cygwin script was removed')
139    })
140  })
141})
142
143test('dont-remove-cmd-shims', function (t) {
144  t.plan(2)
145  var gentlyRm = require('../../lib/utils/gently-rm.js')
146  runAll([ [gentlyRm, dontremove_example_cmd, true, dontremove_module],
147    [gentlyRm, dontremove_example_cygwin, true, dontremove_module] ],
148  function () {
149    fs.stat(dontremove_example_cmd, function (er) {
150      t.is(er, null, 'cmd-shim was not removed')
151    })
152    fs.stat(dontremove_example_cygwin, function (er) {
153      t.is(er, null, 'cmd-shim cygwin script was not removed')
154    })
155  })
156})
157
158test('cleanup', function (t) {
159  cleanup()
160  t.done()
161})
162