|
| 1 | +const t = require('tap') |
| 2 | +const PackageJson = require('../') |
| 3 | +const normalize = require('../lib/normalize.js') |
| 4 | + |
| 5 | +const pkg = (data = {}) => { |
| 6 | + return JSON.stringify({ |
| 7 | + name: '@npmcli/test-package', |
| 8 | + version: '1.0.0', |
| 9 | + ...data, |
| 10 | + }) |
| 11 | +} |
| 12 | + |
| 13 | +// Helper to test the fixName step |
| 14 | +const testFixNameStep = async (t, testdir, expectation) => { |
| 15 | + const p = t.testdir(testdir) |
| 16 | + |
| 17 | + // Test using normalize directly with fixName step |
| 18 | + const instance = await PackageJson.load(p) |
| 19 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 20 | + |
| 21 | + // Apply expectation function if provided |
| 22 | + if (expectation) { |
| 23 | + expectation(t, instance.content) |
| 24 | + } |
| 25 | + |
| 26 | + return instance |
| 27 | +} |
| 28 | + |
| 29 | +t.test('fixName step', async t => { |
| 30 | + t.test('valid package name passes validation', async t => { |
| 31 | + const testdir = { |
| 32 | + 'package.json': pkg({ name: '@npmcli/test-package' }), |
| 33 | + } |
| 34 | + |
| 35 | + await testFixNameStep(t, testdir, (t, content) => { |
| 36 | + t.strictSame(content.name, '@npmcli/test-package', 'name should remain unchanged') |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + t.test('missing name field throws error', async t => { |
| 41 | + const testdir = { |
| 42 | + 'package.json': pkg({ name: undefined }), |
| 43 | + } |
| 44 | + |
| 45 | + await t.rejects( |
| 46 | + async () => { |
| 47 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 48 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 49 | + }, |
| 50 | + { message: /name field must be a string/ }, |
| 51 | + 'should reject with appropriate error message' |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + t.test('non-string name field throws error', async t => { |
| 56 | + const testdir = { |
| 57 | + 'package.json': pkg({ name: ['@npmcli/invalid-test-package'] }), |
| 58 | + } |
| 59 | + |
| 60 | + await t.rejects( |
| 61 | + async () => { |
| 62 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 63 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 64 | + }, |
| 65 | + { message: /name field must be a string/ }, |
| 66 | + 'should reject with appropriate error message' |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + t.test('invalid package name formats throw error', async t => { |
| 71 | + t.test('leading dot', async t => { |
| 72 | + const testdir = { |
| 73 | + 'package.json': pkg({ name: '.npmcli-test-package' }), |
| 74 | + } |
| 75 | + |
| 76 | + await t.rejects( |
| 77 | + async () => { |
| 78 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 79 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 80 | + }, |
| 81 | + { message: /Invalid name/ }, |
| 82 | + 'should reject names starting with a dot' |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + t.test('invalid scoped package name format', async t => { |
| 87 | + const testdir = { |
| 88 | + 'package.json': pkg({ name: '@npmcli/test/package/extra' }), |
| 89 | + } |
| 90 | + |
| 91 | + await t.rejects( |
| 92 | + async () => { |
| 93 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 94 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 95 | + }, |
| 96 | + { message: /Invalid name/ }, |
| 97 | + 'should reject invalid scoped package names' |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + t.test('node_modules reserved name', async t => { |
| 102 | + const testdir = { |
| 103 | + 'package.json': pkg({ name: 'node_modules' }), |
| 104 | + } |
| 105 | + |
| 106 | + await t.rejects( |
| 107 | + async () => { |
| 108 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 109 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 110 | + }, |
| 111 | + { message: /Invalid name/ }, |
| 112 | + 'should reject reserved name node_modules' |
| 113 | + ) |
| 114 | + }) |
| 115 | + |
| 116 | + t.test('favicon.ico reserved name', async t => { |
| 117 | + const testdir = { |
| 118 | + 'package.json': pkg({ name: 'favicon.ico' }), |
| 119 | + } |
| 120 | + |
| 121 | + await t.rejects( |
| 122 | + async () => { |
| 123 | + const instance = await PackageJson.load(t.testdir(testdir)) |
| 124 | + await normalize(instance, { steps: ['fixName'], strict: true, allowLegacyCase: true }) |
| 125 | + }, |
| 126 | + { message: /Invalid name/ }, |
| 127 | + 'should reject reserved name favicon.ico' |
| 128 | + ) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + t.test('builtin module name conflict', async t => { |
| 133 | + const builtinModules = require('node:module').builtinModules |
| 134 | + const builtinName = builtinModules[0] // Use the first builtin module name |
| 135 | + |
| 136 | + const testdir = { |
| 137 | + 'package.json': pkg({ name: builtinName }), |
| 138 | + } |
| 139 | + |
| 140 | + // For builtin modules, we don't throw but we log a warning |
| 141 | + const instance = await testFixNameStep(t, testdir, (t, content) => { |
| 142 | + t.strictSame(content.name, builtinName, 'should allow builtin module names') |
| 143 | + }) |
| 144 | + |
| 145 | + // We can't directly test the warning since it's logged, but we can confirm the name remains unchanged and the operation completes without error |
| 146 | + t.ok(instance, 'operation should complete without error') |
| 147 | + }) |
| 148 | + |
| 149 | + t.test('package with uppercase name', async t => { |
| 150 | + const testdir = { |
| 151 | + 'package.json': pkg({ name: '@NPMCLI/Test-Package' }), |
| 152 | + } |
| 153 | + |
| 154 | + // Case is allowed |
| 155 | + await testFixNameStep(t, testdir, (t, content) => { |
| 156 | + t.strictSame(content.name, '@NPMCLI/Test-Package', 'should allow uppercase in package name for publishing') |
| 157 | + }) |
| 158 | + }) |
| 159 | +}) |
0 commit comments