/
index.ts
73 lines (66 loc) · 2.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createReadStream, promises as fs } from 'fs'
import { DependencyManifest } from '@pnpm/types'
import pDefer from 'p-defer'
import path from 'path'
import tempy from 'tempy'
import createCafs, {
checkFilesIntegrity,
getFilePathInCafs,
} from '../src'
describe('cafs', () => {
it('unpack', async () => {
const dest = tempy.directory()
const cafs = createCafs(dest)
const filesIndex = await cafs.addFilesFromTarball(
createReadStream(path.join(__dirname, '../__fixtures__/node-gyp-6.1.0.tgz'))
)
expect(Object.keys(filesIndex)).toHaveLength(121)
const pkgFile = filesIndex['package.json']
expect(pkgFile.size).toBe(1121)
expect(pkgFile.mode).toBe(420)
const { checkedAt, integrity } = await pkgFile.writeResult
expect(typeof checkedAt).toBe('number')
expect(integrity.toString()).toBe('sha512-8xCvrlC7W3TlwXxetv5CZTi53szYhmT7tmpXF/ttNthtTR9TC7Y7WJFPmJToHaSQ4uObuZyOARdOJYNYuTSbXA==')
})
it('replaces an already existing file, if the integrity of it was broken', async () => {
const storeDir = tempy.directory()
const srcDir = path.join(__dirname, 'fixtures/one-file')
const manifest = pDefer<DependencyManifest>()
const addFiles = async () => createCafs(storeDir).addFilesFromDir(srcDir, manifest)
let filesIndex = await addFiles()
const { integrity } = await filesIndex['foo.txt'].writeResult
// Modifying the file in the store
const filePath = getFilePathInCafs(storeDir, integrity, 'nonexec')
await fs.appendFile(filePath, 'bar')
filesIndex = await addFiles()
await filesIndex['foo.txt'].writeResult
expect(await fs.readFile(filePath, 'utf8')).toBe('foo\n')
expect(await manifest.promise).toEqual(undefined)
})
})
describe('checkFilesIntegrity()', () => {
it("doesn't fail if file was removed from the store", async () => {
const storeDir = tempy.directory()
expect(await checkFilesIntegrity(storeDir, {
foo: {
integrity: 'sha512-8xCvrlC7W3TlwXxetv5CZTi53szYhmT7tmpXF/ttNthtTR9TC7Y7WJFPmJToHaSQ4uObuZyOARdOJYNYuTSbXA==',
mode: 420,
size: 10,
},
})).toBeFalsy()
})
})
test('file names are normalized when unpacking a tarball', async () => {
const dest = tempy.directory()
const cafs = createCafs(dest)
const filesIndex = await cafs.addFilesFromTarball(
createReadStream(path.join(__dirname, 'fixtures/colorize-semver-diff.tgz'))
)
expect(Object.keys(filesIndex).sort()).toStrictEqual([
'LICENSE',
'README.md',
'lib/index.d.ts',
'lib/index.js',
'package.json',
])
})