Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornlf <quitlahok@gmail.com>2020-12-14 19:47:37 +0300
committernlf <quitlahok@gmail.com>2020-12-15 22:16:37 +0300
commit3a6dd511c944c5f2699825a99bba1dde333a45ef (patch)
treed3d3907486f181dd1c68a4e72650f6826050f2aa /test
parentd6817f062e62d987c729a12770e0346a9a9d561b (diff)
refactor edit, add tests
Diffstat (limited to 'test')
-rw-r--r--test/lib/edit.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/test/lib/edit.js b/test/lib/edit.js
new file mode 100644
index 000000000..0d3bbb4c5
--- /dev/null
+++ b/test/lib/edit.js
@@ -0,0 +1,123 @@
+const { test } = require('tap')
+const { resolve } = require('path')
+const requireInject = require('require-inject')
+const { EventEmitter } = require('events')
+
+let editorBin = null
+let editorArgs = null
+let editorOpts = null
+let EDITOR_CODE = 0
+const childProcess = {
+ spawn: (bin, args, opts) => {
+ // save for assertions
+ editorBin = bin
+ editorArgs = args
+ editorOpts = opts
+
+ const editorEvents = new EventEmitter()
+ process.nextTick(() => {
+ editorEvents.emit('exit', EDITOR_CODE)
+ })
+ return editorEvents
+ },
+}
+
+let rebuildArgs = null
+let EDITOR = 'vim'
+const npm = {
+ config: {
+ get: () => EDITOR,
+ },
+ dir: resolve(__dirname, '../../node_modules'),
+ commands: {
+ rebuild: (args, cb) => {
+ rebuildArgs = args
+ return cb()
+ },
+ },
+}
+
+const gracefulFs = require('graceful-fs')
+const edit = requireInject('../../lib/edit.js', {
+ '../../lib/npm.js': npm,
+ child_process: childProcess,
+ 'graceful-fs': gracefulFs,
+})
+
+test('npm edit', t => {
+ t.teardown(() => {
+ rebuildArgs = null
+ editorBin = null
+ editorArgs = null
+ editorOpts = null
+ })
+
+ return edit(['semver'], (err) => {
+ if (err)
+ throw err
+
+ const path = resolve(__dirname, '../../node_modules/semver')
+ t.strictSame(editorBin, EDITOR, 'used the correct editor')
+ t.strictSame(editorArgs, [path], 'edited the correct directory')
+ t.strictSame(editorOpts, { stdio: 'inherit' }, 'passed the correct opts')
+ t.strictSame(rebuildArgs, [path], 'passed the correct path to rebuild')
+ t.end()
+ })
+})
+
+test('npm edit editor has flags', t => {
+ EDITOR = 'code -w'
+ t.teardown(() => {
+ rebuildArgs = null
+ editorBin = null
+ editorArgs = null
+ editorOpts = null
+ EDITOR = 'vim'
+ })
+
+ return edit(['semver'], (err) => {
+ if (err)
+ throw err
+
+ const path = resolve(__dirname, '../../node_modules/semver')
+ t.strictSame(editorBin, 'code', 'used the correct editor')
+ t.strictSame(editorArgs, ['-w', path], 'edited the correct directory, keeping flags')
+ t.strictSame(editorOpts, { stdio: 'inherit' }, 'passed the correct opts')
+ t.strictSame(rebuildArgs, [path], 'passed the correct path to rebuild')
+ t.end()
+ })
+})
+
+test('npm edit no args', t => {
+ return edit([], (err) => {
+ t.match(err, /npm edit/, 'throws usage error')
+ t.end()
+ })
+})
+
+test('npm edit lstat error propagates', t => {
+ const _lstat = gracefulFs.lstat
+ gracefulFs.lstat = (dir, cb) => {
+ return cb(new Error('lstat failed'))
+ }
+ t.teardown(() => {
+ gracefulFs.lstat = _lstat
+ })
+
+ return edit(['semver'], (err) => {
+ t.match(err, /lstat failed/, 'user received correct error')
+ t.end()
+ })
+})
+
+test('npm edit editor exit code error propagates', t => {
+ EDITOR_CODE = 137
+ t.teardown(() => {
+ EDITOR_CODE = 0
+ })
+
+ return edit(['semver'], (err) => {
+ t.match(err, /exited with code: 137/, 'user received correct error')
+ t.end()
+ })
+})