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

edit.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: acf03fa438c3cf4c97542fc67c3aaf627c15cc70 (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
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 rebuildFail = null
let EDITOR = 'vim'
const npm = {
  config: {
    get: () => EDITOR,
  },
  dir: resolve(__dirname, '../../node_modules'),
  commands: {
    rebuild: (args, cb) => {
      rebuildArgs = args
      return cb(rebuildFail)
    },
  },
}

const gracefulFs = require('graceful-fs')
const Edit = requireInject('../../lib/edit.js', {
  child_process: childProcess,
  'graceful-fs': gracefulFs,
})
const edit = new Edit(npm)

test('npm edit', t => {
  t.teardown(() => {
    rebuildArgs = null
    editorBin = null
    editorArgs = null
    editorOpts = null
  })

  return edit.exec(['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('rebuild fails', t => {
  t.teardown(() => {
    rebuildFail = null
    rebuildArgs = null
    editorBin = null
    editorArgs = null
    editorOpts = null
  })

  rebuildFail = new Error('test error')
  return edit.exec(['semver'], (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.match(err, { message: 'test error' })
    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.exec(['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.exec([], (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.exec(['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.exec(['semver'], (err) => {
    t.match(err, /exited with code: 137/, 'user received correct error')
    t.end()
  })
})