-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Other (release-tools): Created a decorated version of utils exposes b…
…y the "pacote" package ("manifest()", "packument()"). It prevents from using any cache when checking the npm registry.
- Loading branch information
Showing
2 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/ckeditor5-dev-release-tools/lib/utils/pacotecacheless.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import os from 'os'; | ||
import { randomUUID } from 'crypto'; | ||
import upath from 'upath'; | ||
import fs from 'fs-extra'; | ||
import pacote from 'pacote'; | ||
|
||
export const manifest = cacheLessPacoteFactory( pacote.manifest ); | ||
export const packument = cacheLessPacoteFactory( pacote.packument ); | ||
|
||
function cacheLessPacoteFactory( callback ) { | ||
return async ( description, options = {} ) => { | ||
const uuid = randomUUID(); | ||
const cacheDir = upath.join( os.tmpdir(), `pacote--${ uuid }` ); | ||
|
||
await fs.ensureDir( cacheDir ); | ||
|
||
try { | ||
return await callback( description, { | ||
...options, | ||
cache: cacheDir, | ||
memoize: false, | ||
preferOnline: true | ||
} ); | ||
} finally { | ||
await fs.remove( cacheDir ); | ||
} | ||
}; | ||
} |
198 changes: 198 additions & 0 deletions
198
packages/ckeditor5-dev-release-tools/tests/utils/pacotecacheless.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import os from 'os'; | ||
import { randomUUID } from 'crypto'; | ||
import fs from 'fs-extra'; | ||
import pacote from 'pacote'; | ||
import { manifest, packument } from '../../lib/utils/pacotecacheless.js'; | ||
|
||
vi.mock( 'os' ); | ||
vi.mock( 'crypto' ); | ||
vi.mock( 'fs-extra' ); | ||
vi.mock( 'pacote' ); | ||
|
||
describe( 'pacote (no cache)', () => { | ||
beforeEach( () => { | ||
vi.mocked( os ).tmpdir.mockReturnValue( '/tmp' ); | ||
vi.mocked( randomUUID ).mockReturnValue( 'a-1-b-2' ); | ||
|
||
vi.mocked( fs ).ensureDir.mockResolvedValue(); | ||
vi.mocked( fs ).remove.mockResolvedValue(); | ||
} ); | ||
|
||
describe( 'manifest()', () => { | ||
it( 'should be a function', () => { | ||
expect( manifest ).toBeTypeOf( 'function' ); | ||
} ); | ||
|
||
it( 'should create a temporary cache directory', async () => { | ||
await manifest( 'foo' ); | ||
|
||
expect( vi.mocked( os ).tmpdir ).toHaveBeenCalledOnce(); | ||
expect( vi.mocked( randomUUID ) ).toHaveBeenCalledOnce(); | ||
expect( vi.mocked( fs ).ensureDir ).toHaveBeenCalledExactlyOnceWith( '/tmp/pacote--a-1-b-2' ); | ||
} ); | ||
|
||
it( 'must create a cache directory before executing `pacote.manifest()`', async () => { | ||
await manifest( 'foo' ); | ||
|
||
expect( vi.mocked( fs ).ensureDir ).toHaveBeenCalledBefore( vi.mocked( pacote ).manifest ); | ||
} ); | ||
|
||
it( 'should pass a temporary cache directory to `pacote.manifest()`', async () => { | ||
await manifest( 'foo' ); | ||
|
||
expect( vi.mocked( pacote ).manifest ).toHaveBeenCalledExactlyOnceWith( | ||
expect.any( String ), | ||
expect.objectContaining( { | ||
cache: '/tmp/pacote--a-1-b-2', | ||
memoize: false, | ||
preferOnline: true | ||
} ) ); | ||
} ); | ||
|
||
it( 'should pass arguments to `pacote.manifest()`', async () => { | ||
await manifest( 'foo', { foo: true, bar: false } ); | ||
|
||
expect( vi.mocked( pacote ).manifest ).toHaveBeenCalledExactlyOnceWith( | ||
'foo', | ||
expect.objectContaining( { | ||
foo: true, | ||
bar: false | ||
} ) ); | ||
} ); | ||
|
||
it( 'should not allow overriding the cache parameters when executing `pacote.manifest()`', async () => { | ||
await manifest( 'foo', { | ||
cache: null, | ||
memoize: 1, | ||
preferOnline: 'never' | ||
} ); | ||
|
||
expect( vi.mocked( pacote ).manifest ).toHaveBeenCalledExactlyOnceWith( | ||
expect.any( String ), | ||
expect.not.objectContaining( { | ||
cache: null, | ||
memoize: 1, | ||
preferOnline: 'never' | ||
} ) ); | ||
} ); | ||
|
||
it( 'should resolve with a value returned by `pacote.manifest()', async () => { | ||
const value = { | ||
status: 'success', | ||
data: { | ||
done: true | ||
} | ||
}; | ||
|
||
vi.mocked( pacote ).manifest.mockResolvedValue( value ); | ||
|
||
await expect( manifest( 'foo' ) ).resolves.toEqual( value ); | ||
} ); | ||
|
||
it( 'must remove a cache directory after executing `pacote.manifest()` (when resolved)', async () => { | ||
await manifest( 'foo' ); | ||
|
||
expect( vi.mocked( fs ).remove ).toHaveBeenCalledAfter( vi.mocked( pacote ).manifest ); | ||
} ); | ||
|
||
it( 'must remove a cache directory after executing `pacote.manifest()` (when rejected)', async () => { | ||
vi.mocked( pacote ).manifest.mockRejectedValue( 'null' ); | ||
|
||
await expect( manifest( 'foo' ) ).rejects.toThrow(); | ||
|
||
expect( vi.mocked( fs ).remove ).toHaveBeenCalledAfter( vi.mocked( pacote ).manifest ); | ||
} ); | ||
} ); | ||
|
||
describe( 'packument()', () => { | ||
it( 'should be a function', () => { | ||
expect( packument ).toBeTypeOf( 'function' ); | ||
} ); | ||
|
||
it( 'should create a temporary cache directory', async () => { | ||
await packument( 'foo' ); | ||
|
||
expect( vi.mocked( os ).tmpdir ).toHaveBeenCalledOnce(); | ||
expect( vi.mocked( randomUUID ) ).toHaveBeenCalledOnce(); | ||
expect( vi.mocked( fs ).ensureDir ).toHaveBeenCalledExactlyOnceWith( '/tmp/pacote--a-1-b-2' ); | ||
} ); | ||
|
||
it( 'must create a cache directory before executing `pacote.packument()`', async () => { | ||
await packument( 'foo' ); | ||
|
||
expect( vi.mocked( fs ).ensureDir ).toHaveBeenCalledBefore( vi.mocked( pacote ).packument ); | ||
} ); | ||
|
||
it( 'should pass a temporary cache directory to `pacote.packument()`', async () => { | ||
await packument( 'foo' ); | ||
|
||
expect( vi.mocked( pacote ).packument ).toHaveBeenCalledExactlyOnceWith( | ||
expect.any( String ), | ||
expect.objectContaining( { | ||
cache: '/tmp/pacote--a-1-b-2', | ||
memoize: false, | ||
preferOnline: true | ||
} ) ); | ||
} ); | ||
|
||
it( 'should pass arguments to `pacote.packument()`', async () => { | ||
await packument( 'foo', { foo: true, bar: false } ); | ||
|
||
expect( vi.mocked( pacote ).packument ).toHaveBeenCalledExactlyOnceWith( | ||
'foo', | ||
expect.objectContaining( { | ||
foo: true, | ||
bar: false | ||
} ) ); | ||
} ); | ||
|
||
it( 'should not allow overriding the cache parameters when executing `pacote.packument()`', async () => { | ||
await packument( 'foo', { | ||
cache: null, | ||
memoize: 1, | ||
preferOnline: 'never' | ||
} ); | ||
|
||
expect( vi.mocked( pacote ).packument ).toHaveBeenCalledExactlyOnceWith( | ||
expect.any( String ), | ||
expect.not.objectContaining( { | ||
cache: null, | ||
memoize: 1, | ||
preferOnline: 'never' | ||
} ) ); | ||
} ); | ||
|
||
it( 'should resolve with a value returned by `pacote.packument()', async () => { | ||
const value = { | ||
status: 'success', | ||
data: { | ||
done: true | ||
} | ||
}; | ||
|
||
vi.mocked( pacote ).packument.mockResolvedValue( value ); | ||
|
||
await expect( packument( 'foo' ) ).resolves.toEqual( value ); | ||
} ); | ||
|
||
it( 'must remove a cache directory after executing `pacote.packument()` (when resolved)', async () => { | ||
await packument( 'foo' ); | ||
|
||
expect( vi.mocked( fs ).remove ).toHaveBeenCalledAfter( vi.mocked( pacote ).packument ); | ||
} ); | ||
|
||
it( 'must remove a cache directory after executing `pacote.packument()` (when rejected)', async () => { | ||
vi.mocked( pacote ).packument.mockRejectedValue( 'null' ); | ||
|
||
await expect( packument( 'foo' ) ).rejects.toThrow(); | ||
|
||
expect( vi.mocked( fs ).remove ).toHaveBeenCalledAfter( vi.mocked( pacote ).packument ); | ||
} ); | ||
} ); | ||
} ); |