|
| 1 | +const test = require('tap') |
| 2 | +const requireInject = require('require-inject') |
| 3 | +const setScriptDefault = require('../../lib/set-script.js') |
| 4 | +const parseJSON = require('json-parse-even-better-errors') |
| 5 | + |
| 6 | +test.type(setScriptDefault, 'function', 'command is function') |
| 7 | +test.equal(setScriptDefault.completion, require('../../lib/utils/completion/none.js'), 'empty completion') |
| 8 | +test.equal(setScriptDefault.usage, 'npm set-script [<script>] [<command>]', 'usage matches') |
| 9 | +test.test('fails on invalid arguments', (t) => { |
| 10 | + const setScript = requireInject('../../lib/set-script.js', { |
| 11 | + fs: {}, |
| 12 | + npmlog: {}, |
| 13 | + }) |
| 14 | + t.plan(3) |
| 15 | + setScript(['arg1'], (fail) => t.match(fail, /Expected 2 arguments: got 1/)) |
| 16 | + setScript(['arg1', 'arg2', 'arg3'], (fail) => t.match(fail, /Expected 2 arguments: got 3/)) |
| 17 | + setScript(['arg1', 'arg2', 'arg3', 'arg4'], (fail) => t.match(fail, /Expected 2 arguments: got 4/)) |
| 18 | +}) |
| 19 | +test.test('fails if run in postinstall script', (t) => { |
| 20 | + var originalVar = process.env.npm_lifecycle_event |
| 21 | + process.env.npm_lifecycle_event = 'postinstall' |
| 22 | + const setScript = requireInject('../../lib/set-script.js', { |
| 23 | + fs: {}, |
| 24 | + npmlog: {}, |
| 25 | + }) |
| 26 | + t.plan(1) |
| 27 | + setScript(['arg1', 'arg2'], (fail) => t.equal(fail.toString(), 'Error: Scripts can’t set from the postinstall script')) |
| 28 | + process.env.npm_lifecycle_event = originalVar |
| 29 | +}) |
| 30 | +test.test('fails when package.json not found', (t) => { |
| 31 | + const setScript = requireInject('../../lib/set-script.js', { |
| 32 | + '../../lib/npm.js': { |
| 33 | + localPrefix: 'IDONTEXIST', |
| 34 | + }, |
| 35 | + }) |
| 36 | + t.plan(1) |
| 37 | + setScript(['arg1', 'arg2'], (fail) => t.match(fail, /package.json not found/)) |
| 38 | +}) |
| 39 | +test.test('fails on invalid JSON', (t) => { |
| 40 | + const setScript = requireInject('../../lib/set-script.js', { |
| 41 | + fs: { |
| 42 | + readFileSync: (name, charcode) => { |
| 43 | + return 'iamnotjson' |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | + t.plan(1) |
| 48 | + setScript(['arg1', 'arg2'], (fail) => t.match(fail, /Invalid package.json: JSONParseError/)) |
| 49 | +}) |
| 50 | +test.test('creates scripts object', (t) => { |
| 51 | + var mockFile = '' |
| 52 | + const setScript = requireInject('../../lib/set-script.js', { |
| 53 | + fs: { |
| 54 | + readFileSync: (name, charcode) => { |
| 55 | + return '{}' |
| 56 | + }, |
| 57 | + writeFileSync: (location, inner) => { |
| 58 | + mockFile = inner |
| 59 | + }, |
| 60 | + }, |
| 61 | + 'read-package-json-fast': async function (filename) { |
| 62 | + return { |
| 63 | + [Symbol.for('indent')]: ' ', |
| 64 | + [Symbol.for('newline')]: '\n', |
| 65 | + } |
| 66 | + }, |
| 67 | + }) |
| 68 | + t.plan(2) |
| 69 | + setScript(['arg1', 'arg2'], (error) => { |
| 70 | + t.equal(error, undefined) |
| 71 | + t.assert(parseJSON(mockFile), {scripts: {arg1: 'arg2'}}) |
| 72 | + }) |
| 73 | +}) |
| 74 | +test.test('warns before overwriting', (t) => { |
| 75 | + var warningListened = '' |
| 76 | + const setScript = requireInject('../../lib/set-script.js', { |
| 77 | + fs: { |
| 78 | + readFileSync: (name, charcode) => { |
| 79 | + return JSON.stringify({ |
| 80 | + scripts: { |
| 81 | + arg1: 'blah', |
| 82 | + }, |
| 83 | + }) |
| 84 | + }, |
| 85 | + writeFileSync: (name, content) => {}, |
| 86 | + }, |
| 87 | + 'read-package-json-fast': async function (filename) { |
| 88 | + return { |
| 89 | + [Symbol.for('indent')]: ' ', |
| 90 | + [Symbol.for('newline')]: '\n', |
| 91 | + } |
| 92 | + }, |
| 93 | + npmlog: { |
| 94 | + warn: (prefix, message) => { |
| 95 | + warningListened = message |
| 96 | + }, |
| 97 | + }, |
| 98 | + }) |
| 99 | + t.plan(2) |
| 100 | + setScript(['arg1', 'arg2'], (error) => { |
| 101 | + t.equal(error, undefined, 'no error') |
| 102 | + t.equal(warningListened, 'Script "arg1" was overwritten') |
| 103 | + }) |
| 104 | +}) |
| 105 | +test.test('provided indentation and eol is used', (t) => { |
| 106 | + var mockFile = '' |
| 107 | + const setScript = requireInject('../../lib/set-script.js', { |
| 108 | + fs: { |
| 109 | + readFileSync: (name, charcode) => { |
| 110 | + return '{}' |
| 111 | + }, |
| 112 | + writeFileSync: (name, content) => { |
| 113 | + mockFile = content |
| 114 | + }, |
| 115 | + }, |
| 116 | + 'read-package-json-fast': async function (filename) { |
| 117 | + return { |
| 118 | + [Symbol.for('indent')]: ' '.repeat(6), |
| 119 | + [Symbol.for('newline')]: '\r\n', |
| 120 | + } |
| 121 | + }, |
| 122 | + }) |
| 123 | + t.plan(3) |
| 124 | + setScript(['arg1', 'arg2'], (error) => { |
| 125 | + t.equal(error, undefined) |
| 126 | + t.equal(mockFile.split('\r\n').length > 1, true) |
| 127 | + t.equal(mockFile.split('\r\n').every((value) => !value.startsWith(' ') || value.startsWith(' '.repeat(6))), true) |
| 128 | + }) |
| 129 | +}) |
| 130 | +test.test('goes to default when undefined indent and eol provided', (t) => { |
| 131 | + var mockFile = '' |
| 132 | + const setScript = requireInject('../../lib/set-script.js', { |
| 133 | + fs: { |
| 134 | + readFileSync: (name, charcode) => { |
| 135 | + return '{}' |
| 136 | + }, |
| 137 | + writeFileSync: (name, content) => { |
| 138 | + mockFile = content |
| 139 | + }, |
| 140 | + }, |
| 141 | + 'read-package-json-fast': async function (filename) { |
| 142 | + return { |
| 143 | + [Symbol.for('indent')]: undefined, |
| 144 | + [Symbol.for('newline')]: undefined, |
| 145 | + } |
| 146 | + }, |
| 147 | + }) |
| 148 | + t.plan(3) |
| 149 | + setScript(['arg1', 'arg2'], (error) => { |
| 150 | + t.equal(error, undefined) |
| 151 | + t.equal(mockFile.split('\n').length > 1, true) |
| 152 | + t.equal(mockFile.split('\n').every((value) => !value.startsWith(' ') || value.startsWith(' ')), true) |
| 153 | + }) |
| 154 | +}) |
0 commit comments