Files
pnpm/crypto/hash/test/index.ts
Zoltan Kochan 5d5818e44f style: enforce node: protocol for builtin imports (#10951)
Add n/prefer-node-protocol rule and autofix all bare builtin imports
to use the node: prefix. Simplify the simple-import-sort builtins
pattern to just ^node: since all imports now use the prefix.
2026-03-13 07:59:51 +01:00

36 lines
1.2 KiB
TypeScript

/// <reference path="../../../__typings__/index.d.ts"/>
import fs from 'node:fs'
import { pipeline } from 'node:stream/promises'
import { createHashFromFile, createShortHash, getTarballIntegrity } from '@pnpm/crypto.hash'
import { tempDir } from '@pnpm/prepare'
import tar from 'tar-stream'
test('createShortHash()', () => {
expect(createShortHash('AAA')).toBe('cb1ad2119d8fafb69566510ee712661f')
})
test('createHashFromFile normalizes line endings before calculating the hash', async () => {
tempDir()
fs.writeFileSync('win-eol.txt', 'a\r\nb\r\nc')
fs.writeFileSync('posix-eol.txt', 'a\nb\r\nc')
expect(await createHashFromFile('win-eol.txt')).toEqual(await createHashFromFile('posix-eol.txt'))
})
test('getTarballIntegrity creates integrity hash for tarball', async () => {
expect.hasAssertions()
tempDir()
const pack = tar.pack()
pack.entry({ name: 'package.json', mtime: new Date('1970-01-01T00:00:00.000Z') }, JSON.stringify({
name: 'local-tarball',
version: '1.0.0',
}))
pack.finalize()
await pipeline(pack, fs.createWriteStream('./local-tarball.tar'))
await expect(getTarballIntegrity('./local-tarball.tar'))
.resolves.toBe('sha512-nQP7gWOhNQ/5HoM/rJmzOgzZt6Wg6k56CyvO/0sMmiS3UkLSmzY5mW8mMrnbspgqpmOW8q/FHyb0YIr4n2A8VQ==')
})