|
| 1 | +import fs from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { stripVTControlCharacters as stripAnsi } from 'node:util' |
| 5 | + |
1 | 6 | import { expect, test } from '@jest/globals' |
2 | 7 | import type { Config, ConfigContext } from '@pnpm/config.reader' |
3 | 8 | import { view } from '@pnpm/deps.inspection.commands' |
@@ -37,9 +42,18 @@ test('view: rcOptionsTypes should return object', () => { |
37 | 42 | }) |
38 | 43 |
|
39 | 44 | test('view: missing package name throws error', async () => { |
40 | | - await expect( |
41 | | - view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
42 | | - ).rejects.toMatchObject({ code: 'ERR_PNPM_MISSING_PACKAGE_NAME' }) |
| 45 | + const cwd = process.cwd() |
| 46 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 47 | + |
| 48 | + try { |
| 49 | + process.chdir(tmpDir) |
| 50 | + await expect( |
| 51 | + view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 52 | + ).rejects.toMatchObject({ code: 'ERR_PNPM_MISSING_PACKAGE_NAME' }) |
| 53 | + } finally { |
| 54 | + process.chdir(cwd) |
| 55 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 56 | + } |
43 | 57 | }) |
44 | 58 |
|
45 | 59 | test('view: non-registry spec throws error', async () => { |
@@ -142,8 +156,9 @@ test('view: text output includes dist section', async () => { |
142 | 156 |
|
143 | 157 | test('view: text output includes dist-tags', async () => { |
144 | 158 | const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, ['is-negative']) as string |
145 | | - expect(result).toContain('dist-tags:') |
146 | | - expect(result).toContain('latest:') |
| 159 | + const plainTextResult = stripAnsi(result) |
| 160 | + expect(plainTextResult).toContain('dist-tags:') |
| 161 | + expect(plainTextResult).toContain('latest:') |
147 | 162 | }) |
148 | 163 |
|
149 | 164 | test('view: text output for package with dependencies shows deps count', async () => { |
@@ -233,5 +248,140 @@ test('view: published info includes timestamp', async () => { |
233 | 248 | test('view: published info includes publisher when maintainer data is available', async () => { |
234 | 249 | // Note: is-negative package has maintainer data in the mock registry |
235 | 250 | const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, ['is-negative@1.0.0']) as string |
236 | | - expect(result).toMatch(/published .* ago by /) |
| 251 | + expect(stripAnsi(result)).toMatch(/published .* ago by /) |
| 252 | +}) |
| 253 | + |
| 254 | +test('view: uses package manifest name when no package name provided', async () => { |
| 255 | + const cwd = process.cwd() |
| 256 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 257 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 258 | + |
| 259 | + try { |
| 260 | + fs.writeFileSync(pkgJsonPath, JSON.stringify({ name: 'is-negative' })) |
| 261 | + process.chdir(tmpDir) |
| 262 | + |
| 263 | + const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 264 | + expect(typeof result).toBe('string') |
| 265 | + expect(result).toContain('is-negative') |
| 266 | + } finally { |
| 267 | + process.chdir(cwd) |
| 268 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 269 | + } |
| 270 | +}) |
| 271 | + |
| 272 | +test('view: searches upward for package manifest in nested directory', async () => { |
| 273 | + const cwd = process.cwd() |
| 274 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 275 | + const nestedDir = path.join(tmpDir, 'a', 'b') |
| 276 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 277 | + |
| 278 | + try { |
| 279 | + fs.mkdirSync(nestedDir, { recursive: true }) |
| 280 | + fs.writeFileSync(pkgJsonPath, JSON.stringify({ name: 'is-negative' })) |
| 281 | + process.chdir(nestedDir) |
| 282 | + |
| 283 | + const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 284 | + expect(typeof result).toBe('string') |
| 285 | + expect(result).toContain('is-negative') |
| 286 | + } finally { |
| 287 | + process.chdir(cwd) |
| 288 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 289 | + } |
| 290 | +}) |
| 291 | + |
| 292 | +test('view: package.json without name field throws error', async () => { |
| 293 | + const cwd = process.cwd() |
| 294 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 295 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 296 | + |
| 297 | + try { |
| 298 | + fs.writeFileSync(pkgJsonPath, JSON.stringify({ version: '1.0.0' })) |
| 299 | + process.chdir(tmpDir) |
| 300 | + |
| 301 | + await expect( |
| 302 | + view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 303 | + ).rejects.toMatchObject({ code: 'ERR_PNPM_INVALID_PACKAGE_JSON' }) |
| 304 | + } finally { |
| 305 | + process.chdir(cwd) |
| 306 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 307 | + } |
| 308 | +}) |
| 309 | + |
| 310 | +test('view: uses package.yaml name when no package name provided', async () => { |
| 311 | + const cwd = process.cwd() |
| 312 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 313 | + const pkgYamlPath = path.join(tmpDir, 'package.yaml') |
| 314 | + |
| 315 | + try { |
| 316 | + fs.writeFileSync(pkgYamlPath, 'name: is-negative\n') |
| 317 | + process.chdir(tmpDir) |
| 318 | + |
| 319 | + const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 320 | + expect(typeof result).toBe('string') |
| 321 | + expect(result).toContain('is-negative') |
| 322 | + } finally { |
| 323 | + process.chdir(cwd) |
| 324 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 325 | + } |
| 326 | +}) |
| 327 | + |
| 328 | +test('view: package.json with non-object JSON throws error', async () => { |
| 329 | + const cwd = process.cwd() |
| 330 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 331 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 332 | + |
| 333 | + try { |
| 334 | + fs.writeFileSync(pkgJsonPath, 'null') |
| 335 | + process.chdir(tmpDir) |
| 336 | + |
| 337 | + await expect( |
| 338 | + view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 339 | + ).rejects.toMatchObject({ code: 'ERR_PNPM_INVALID_PACKAGE_JSON' }) |
| 340 | + } finally { |
| 341 | + process.chdir(cwd) |
| 342 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 343 | + } |
| 344 | +}) |
| 345 | + |
| 346 | +test('view: resolves package.json from opts.dir when cwd differs', async () => { |
| 347 | + const cwd = process.cwd() |
| 348 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 349 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 350 | + const otherDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-other-')) |
| 351 | + |
| 352 | + try { |
| 353 | + fs.writeFileSync(pkgJsonPath, JSON.stringify({ name: 'is-negative' })) |
| 354 | + process.chdir(otherDir) |
| 355 | + |
| 356 | + const result = await view.handler({ ...VIEW_OPTIONS, dir: tmpDir } as unknown as Config & ConfigContext, []) |
| 357 | + expect(typeof result).toBe('string') |
| 358 | + expect(result).toContain('is-negative') |
| 359 | + } finally { |
| 360 | + process.chdir(cwd) |
| 361 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 362 | + fs.rmSync(otherDir, { recursive: true, force: true }) |
| 363 | + } |
| 364 | +}) |
| 365 | + |
| 366 | +test('view: derives package name even when engines.pnpm is incompatible', async () => { |
| 367 | + const cwd = process.cwd() |
| 368 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'view-test-')) |
| 369 | + const pkgJsonPath = path.join(tmpDir, 'package.json') |
| 370 | + |
| 371 | + try { |
| 372 | + fs.writeFileSync(pkgJsonPath, JSON.stringify({ |
| 373 | + name: 'is-negative', |
| 374 | + engines: { |
| 375 | + pnpm: '999.0.0', |
| 376 | + }, |
| 377 | + })) |
| 378 | + process.chdir(tmpDir) |
| 379 | + |
| 380 | + const result = await view.handler(VIEW_OPTIONS as unknown as Config & ConfigContext, []) |
| 381 | + expect(typeof result).toBe('string') |
| 382 | + expect(result).toContain('is-negative') |
| 383 | + } finally { |
| 384 | + process.chdir(cwd) |
| 385 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 386 | + } |
237 | 387 | }) |
0 commit comments