summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/set-script.js
blob: ab25ba968a92c381866475b163f45ef5c85a3292 (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
const test = require('tap')
const requireInject = require('require-inject')
const setScriptDefault = require('../../lib/set-script.js')
const parseJSON = require('json-parse-even-better-errors')

test.type(setScriptDefault, 'function', 'command is function')
test.equal(setScriptDefault.completion, require('../../lib/utils/completion/none.js'), 'empty completion')
test.equal(setScriptDefault.usage, 'npm set-script [<script>] [<command>]', 'usage matches')
test.test('fails on invalid arguments', (t) => {
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {},
    npmlog: {},
  })
  t.plan(3)
  setScript(['arg1'], (fail) => t.match(fail, /Expected 2 arguments: got 1/))
  setScript(['arg1', 'arg2', 'arg3'], (fail) => t.match(fail, /Expected 2 arguments: got 3/))
  setScript(['arg1', 'arg2', 'arg3', 'arg4'], (fail) => t.match(fail, /Expected 2 arguments: got 4/))
})
test.test('fails if run in postinstall script', (t) => {
  var originalVar = process.env.npm_lifecycle_event
  process.env.npm_lifecycle_event = 'postinstall'
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {},
    npmlog: {},
  })
  t.plan(1)
  setScript(['arg1', 'arg2'], (fail) => t.equal(fail.toString(), 'Error: Scripts can’t set from the postinstall script'))
  process.env.npm_lifecycle_event = originalVar
})
test.test('fails when package.json not found', (t) => {
  const setScript = requireInject('../../lib/set-script.js', {
    '../../lib/npm.js': {
      localPrefix: 'IDONTEXIST',
    },
  })
  t.plan(1)
  setScript(['arg1', 'arg2'], (fail) => t.match(fail, /package.json not found/))
})
test.test('fails on invalid JSON', (t) => {
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {
      readFileSync: (name, charcode) => {
        return 'iamnotjson'
      },
    },
  })
  t.plan(1)
  setScript(['arg1', 'arg2'], (fail) => t.match(fail, /Invalid package.json: JSONParseError/))
})
test.test('creates scripts object', (t) => {
  var mockFile = ''
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {
      readFileSync: (name, charcode) => {
        return '{}'
      },
      writeFileSync: (location, inner) => {
        mockFile = inner
      },
    },
    'read-package-json-fast': async function (filename) {
      return {
        [Symbol.for('indent')]: '  ',
        [Symbol.for('newline')]: '\n',
      }
    },
  })
  t.plan(2)
  setScript(['arg1', 'arg2'], (error) => {
    t.equal(error, undefined)
    t.assert(parseJSON(mockFile), {scripts: {arg1: 'arg2'}})
  })
})
test.test('warns before overwriting', (t) => {
  var warningListened = ''
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {
      readFileSync: (name, charcode) => {
        return JSON.stringify({
          scripts: {
            arg1: 'blah',
          },
        })
      },
      writeFileSync: (name, content) => {},
    },
    'read-package-json-fast': async function (filename) {
      return {
        [Symbol.for('indent')]: '  ',
        [Symbol.for('newline')]: '\n',
      }
    },
    npmlog: {
      warn: (prefix, message) => {
        warningListened = message
      },
    },
  })
  t.plan(2)
  setScript(['arg1', 'arg2'], (error) => {
    t.equal(error, undefined, 'no error')
    t.equal(warningListened, 'Script "arg1" was overwritten')
  })
})
test.test('provided indentation and eol is used', (t) => {
  var mockFile = ''
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {
      readFileSync: (name, charcode) => {
        return '{}'
      },
      writeFileSync: (name, content) => {
        mockFile = content
      },
    },
    'read-package-json-fast': async function (filename) {
      return {
        [Symbol.for('indent')]: ' '.repeat(6),
        [Symbol.for('newline')]: '\r\n',
      }
    },
  })
  t.plan(3)
  setScript(['arg1', 'arg2'], (error) => {
    t.equal(error, undefined)
    t.equal(mockFile.split('\r\n').length > 1, true)
    t.equal(mockFile.split('\r\n').every((value) => !value.startsWith(' ') || value.startsWith(' '.repeat(6))), true)
  })
})
test.test('goes to default when undefined indent and eol provided', (t) => {
  var mockFile = ''
  const setScript = requireInject('../../lib/set-script.js', {
    fs: {
      readFileSync: (name, charcode) => {
        return '{}'
      },
      writeFileSync: (name, content) => {
        mockFile = content
      },
    },
    'read-package-json-fast': async function (filename) {
      return {
        [Symbol.for('indent')]: undefined,
        [Symbol.for('newline')]: undefined,
      }
    },
  })
  t.plan(3)
  setScript(['arg1', 'arg2'], (error) => {
    t.equal(error, undefined)
    t.equal(mockFile.split('\n').length > 1, true)
    t.equal(mockFile.split('\n').every((value) => !value.startsWith(' ') || value.startsWith('  ')), true)
  })
})