-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtarball-file-system.test.ts
More file actions
84 lines (72 loc) · 2.63 KB
/
tarball-file-system.test.ts
File metadata and controls
84 lines (72 loc) · 2.63 KB
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
74
75
76
77
78
79
80
81
82
83
84
import {describe, it, expect, beforeEach, vi} from 'vitest';
import * as path from 'node:path';
import {TarballFileSystem} from '../tarball-file-system.js';
const enc = (s: string) => new TextEncoder().encode(s);
let mockFiles: Array<{name: string; data: Uint8Array}>;
const mockRootDir = 'package';
vi.mock('@publint/pack', () => {
return {
unpack: vi.fn(async () => ({
rootDir: mockRootDir,
files: mockFiles
}))
};
});
describe('TarballFileSystem', () => {
beforeEach(() => {
mockFiles = [
{
name: path.posix.join(mockRootDir, 'package.json'),
data: enc(JSON.stringify({name: 'pkg', version: '1.0.0'}))
},
{
name: path.posix.join(mockRootDir, 'tsconfig.json'),
data: enc('{}')
},
{
name: path.posix.join(mockRootDir, 'node_modules/a/package.json'),
data: enc(JSON.stringify({name: 'a', version: '1.0.0'}))
},
{
name: path.posix.join(
mockRootDir,
'node_modules/a/node_modules/b/package.json'
),
data: enc(JSON.stringify({name: 'b', version: '1.0.0'}))
},
{
name: path.posix.join(mockRootDir, 'node_modules/a/readme.txt'),
data: enc('abc')
}
];
});
it('should report true for an existing file and false for a missing file', async () => {
const tfs = new TarballFileSystem(new ArrayBuffer(0));
expect(await tfs.fileExists('/tsconfig.json')).toBe(true);
expect(await tfs.fileExists('/does-not-exist.json')).toBe(false);
});
it('should read /package.json and throw on a non-existent path', async () => {
const tfs = new TarballFileSystem(new ArrayBuffer(0));
const text = await tfs.readFile('/package.json');
expect(JSON.parse(text).name).toBe('pkg');
await expect(tfs.readFile('/nope.json')).rejects.toBeTruthy();
});
it('should list package.json files, including nested ones', async () => {
const tfs = new TarballFileSystem(new ArrayBuffer(0));
const root = await tfs.getRootDir();
const files = await tfs.listPackageFiles();
expect(files).toContain(path.posix.join(root, 'package.json'));
expect(files).toContain(
path.posix.join(root, 'node_modules/a/package.json')
);
expect(files).toContain(
path.posix.join(root, 'node_modules/a/node_modules/b/package.json')
);
});
it('should compute install size as the sum of file bytes from the unpacked tarball', async () => {
const tfs = new TarballFileSystem(new ArrayBuffer(0));
const expected = mockFiles.reduce((n, f) => n + f.data.byteLength, 0);
const size = await tfs.getInstallSize();
expect(size).toBe(expected);
});
});