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

edit.js « commands « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2a10be135ad90e39ae4b1cddaa41d4f72ffede8 (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
161
162
const t = require('tap')
const fs = require('fs')
const path = require('path')
const tspawk = require('../../fixtures/tspawk')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')

const spawk = tspawk(t)

// TODO this ... smells.  npm "script-shell" config mentions defaults but those
// are handled by run-script, not npm.  So for now we have to tie tests to some
// pretty specific internals of runScript
const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js')

const npmConfig = {
  config: {
    'ignore-scripts': false,
    editor: 'testeditor',
  },
  prefixDir: {
    node_modules: {
      semver: {
        'package.json': JSON.stringify({
          scripts: {
            install: 'testinstall',
          },
        }),
        node_modules: {
          abbrev: {},
        },
      },
      '@npmcli': {
        'scoped-package': {},
      },
    },
  },
}

t.test('npm edit', async t => {
  const { npm, joinedOutput } = await loadMockNpm(t, npmConfig)

  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
  const [scriptShell] = makeSpawnArgs({
    event: 'install',
    path: npm.prefix,
    cmd: 'testinstall',
  })
  spawk.spawn('testeditor', [semverPath])
  spawk.spawn(
    scriptShell,
    args => {
      const lastArg = args[args.length - 1]
      const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
      const rightFilename = path.basename(lastArg).startsWith('install')
      const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
        .trim().endsWith('testinstall')
      return rightExtension && rightFilename && rightContents
    },
    { cwd: semverPath }
  )
  await npm.exec('edit', ['semver'])
  t.match(joinedOutput(), 'rebuilt dependencies successfully')
})

t.test('rebuild failure', async t => {
  const { npm } = await loadMockNpm(t, npmConfig)
  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
  const [scriptShell] = makeSpawnArgs({
    event: 'install',
    path: npm.prefix,
    cmd: 'testinstall',
  })
  spawk.spawn('testeditor', [semverPath])
  spawk.spawn(
    scriptShell,
    args => {
      const lastArg = args[args.length - 1]
      const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
      const rightFilename = path.basename(lastArg).startsWith('install')
      const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
        .trim().endsWith('testinstall')
      return rightExtension && rightFilename && rightContents
    },
    { cwd: semverPath }
  ).exit(1).stdout('test error')
  await t.rejects(
    npm.exec('edit', ['semver']),
    { message: 'command failed' }
  )
})

t.test('editor failure', async t => {
  const { npm } = await loadMockNpm(t, npmConfig)
  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
  spawk.spawn('testeditor', [semverPath]).exit(1).stdout('test editor failure')
  await t.rejects(
    npm.exec('edit', ['semver']),
    { message: 'editor process exited with code: 1' }
  )
})

t.test('npm edit editor has flags', async t => {
  const { npm } = await loadMockNpm(t, {
    ...npmConfig,
    config: {
      ...npmConfig.config,
      editor: 'testeditor --flag',
    },
  })

  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
  const [scriptShell] = makeSpawnArgs({
    event: 'install',
    path: npm.prefix,
    cmd: 'testinstall',
  })
  spawk.spawn('testeditor', ['--flag', semverPath])
  spawk.spawn(
    scriptShell,
    args => {
      const lastArg = args[args.length - 1]
      const rightExtension = lastArg.endsWith('.cmd') || lastArg.endsWith('.sh')
      const rightFilename = path.basename(lastArg).startsWith('install')
      const rightContents = fs.readFileSync(lastArg, { encoding: 'utf8' })
        .trim().endsWith('testinstall')
      return rightExtension && rightFilename && rightContents
    },
    { cwd: semverPath }
  )
  await npm.exec('edit', ['semver'])
})

t.test('npm edit no args', async t => {
  const { npm } = await loadMockNpm(t)
  await t.rejects(
    npm.exec('edit', []),
    { code: 'EUSAGE' },
    'throws usage error'
  )
})

t.test('npm edit nonexistent package', async t => {
  const { npm } = await loadMockNpm(t, npmConfig)

  await t.rejects(
    npm.exec('edit', ['abbrev']),
    /lstat/
  )
})

t.test('scoped package', async t => {
  const { npm } = await loadMockNpm(t, npmConfig)
  const scopedPath = path.resolve(npm.prefix, 'node_modules', '@npmcli', 'scoped-package')
  spawk.spawn('testeditor', [scopedPath])
  await npm.exec('edit', ['@npmcli/scoped-package'])
})

t.test('subdependency', async t => {
  const { npm } = await loadMockNpm(t, npmConfig)
  const subdepPath = path.resolve(npm.prefix, 'node_modules', 'semver', 'node_modules', 'abbrev')
  spawk.spawn('testeditor', [subdepPath])
  await npm.exec('edit', ['semver/abbrev'])
})