|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { |
| 3 | + mkdtempSync, |
| 4 | + rmSync, |
| 5 | + existsSync, |
| 6 | + readFileSync, |
| 7 | + writeFileSync, |
| 8 | +} from 'node:fs'; |
| 9 | +import { tmpdir } from 'node:os'; |
| 10 | +import { join } from 'node:path'; |
| 11 | +import { setupNpmrc } from './npmrc'; |
| 12 | +import { RequestError } from '../../core/request-error'; |
| 13 | +import type { EngineServicesClient } from '../../core/client'; |
| 14 | + |
| 15 | +function fakeClient(getNpmCredentials: () => Promise<unknown>): EngineServicesClient { |
| 16 | + return { getNpmCredentials } as unknown as EngineServicesClient; |
| 17 | +} |
| 18 | + |
| 19 | +describe('setupNpmrc', () => { |
| 20 | + let dir: string; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + dir = mkdtempSync(join(tmpdir(), 'npmrc-test-')); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + rmSync(dir, { recursive: true, force: true }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('writes .npmrc and returns written on success', async () => { |
| 31 | + const npmrc = |
| 32 | + '@thatopen-platform:registry=https://registry.npmjs.org/\n' + |
| 33 | + '//registry.npmjs.org/:_authToken=npm_ro\n'; |
| 34 | + const client = fakeClient(async () => ({ |
| 35 | + registry: 'https://registry.npmjs.org/', |
| 36 | + scope: '@thatopen-platform', |
| 37 | + token: 'npm_ro', |
| 38 | + npmrc, |
| 39 | + })); |
| 40 | + |
| 41 | + const result = await setupNpmrc(client, dir); |
| 42 | + |
| 43 | + expect(result).toEqual({ status: 'written', scope: '@thatopen-platform' }); |
| 44 | + expect(readFileSync(join(dir, '.npmrc'), 'utf-8')).toBe(npmrc); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns forbidden and writes nothing on a 403', async () => { |
| 48 | + const client = fakeClient(async () => { |
| 49 | + throw new RequestError( |
| 50 | + 403, |
| 51 | + 'Forbidden', |
| 52 | + JSON.stringify({ message: 'Community membership required' }), |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + const result = await setupNpmrc(client, dir); |
| 57 | + |
| 58 | + expect(result).toEqual({ status: 'forbidden' }); |
| 59 | + expect(existsSync(join(dir, '.npmrc'))).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns error (and writes nothing) on any other failure', async () => { |
| 63 | + const client = fakeClient(async () => { |
| 64 | + throw new Error('network down'); |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await setupNpmrc(client, dir); |
| 68 | + |
| 69 | + expect(result.status).toBe('error'); |
| 70 | + expect(existsSync(join(dir, '.npmrc'))).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('.gitignore protection (Sergio review #19)', () => { |
| 74 | + const okClient = () => |
| 75 | + fakeClient(async () => ({ |
| 76 | + registry: 'https://registry.npmjs.org/', |
| 77 | + scope: '@thatopen-platform', |
| 78 | + token: 'npm_ro', |
| 79 | + npmrc: '//registry.npmjs.org/:_authToken=npm_ro\n', |
| 80 | + })); |
| 81 | + |
| 82 | + const gitignore = () => |
| 83 | + readFileSync(join(dir, '.gitignore'), 'utf-8'); |
| 84 | + |
| 85 | + it('creates .gitignore ignoring .npmrc when none exists', async () => { |
| 86 | + await setupNpmrc(okClient(), dir); |
| 87 | + expect(gitignore()).toBe('.npmrc\n'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('appends .npmrc to an existing .gitignore that lacks it', async () => { |
| 91 | + writeFileSync(join(dir, '.gitignore'), 'node_modules\ndist\n'); |
| 92 | + await setupNpmrc(okClient(), dir); |
| 93 | + expect(gitignore()).toBe('node_modules\ndist\n.npmrc\n'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('adds a newline before appending when the file has no trailing newline', async () => { |
| 97 | + writeFileSync(join(dir, '.gitignore'), 'node_modules'); |
| 98 | + await setupNpmrc(okClient(), dir); |
| 99 | + expect(gitignore()).toBe('node_modules\n.npmrc\n'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('does not duplicate .npmrc when already ignored', async () => { |
| 103 | + writeFileSync(join(dir, '.gitignore'), 'node_modules\n.npmrc\ndist\n'); |
| 104 | + await setupNpmrc(okClient(), dir); |
| 105 | + expect(gitignore()).toBe('node_modules\n.npmrc\ndist\n'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('does not write .gitignore when the account is forbidden', async () => { |
| 109 | + const client = fakeClient(async () => { |
| 110 | + throw new RequestError(403, 'Forbidden', '{}'); |
| 111 | + }); |
| 112 | + await setupNpmrc(client, dir); |
| 113 | + expect(existsSync(join(dir, '.gitignore'))).toBe(false); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments