-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpublint.test.ts
More file actions
124 lines (105 loc) · 3.46 KB
/
publint.test.ts
File metadata and controls
124 lines (105 loc) · 3.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import {runPublint} from '../../analyze/publint.js';
import {TarballFileSystem} from '../../tarball-file-system.js';
import {LocalFileSystem} from '../../local-file-system.js';
import {createTempDir, cleanupTempDir, createTestPackage} from '../utils.js';
import {pack as packAsTarball} from '@publint/pack';
import fs from 'node:fs/promises';
import path from 'node:path';
describe('runPublint', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await createTempDir();
});
afterEach(async () => {
await cleanupTempDir(tempDir);
});
async function createTarballFromPackage(
packageData: any,
files: Record<string, string> = {}
): Promise<ArrayBuffer> {
await createTestPackage(tempDir, packageData);
for (const [fileName, content] of Object.entries(files)) {
await fs.writeFile(path.join(tempDir, fileName), content);
}
const tarballPath = await packAsTarball(tempDir, {
packageManager: 'npm',
ignoreScripts: true,
destination: tempDir
});
const buffer = await fs.readFile(tarballPath);
return buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
) as ArrayBuffer;
}
it('should return empty result for non-TarballFileSystem', async () => {
const localFileSystem = new LocalFileSystem(tempDir);
const result = await runPublint(localFileSystem);
expect(result).toEqual({
messages: []
});
});
it('should process package with publint errors', async () => {
const tarball = await createTarballFromPackage(
{
name: 'test-package',
version: '1.0.0',
type: 'module',
main: './non-existent-file.js'
},
{
'index.js': 'export const test = "hello";'
}
);
const fileSystem = new TarballFileSystem(tarball);
const result = await runPublint(fileSystem);
expect(result.messages.length).toBeGreaterThan(0);
for (const message of result.messages) {
expect(['error', 'warning', 'suggestion']).toContain(message.severity);
expect(message.score).toBe(0);
expect(typeof message.message).toBe('string');
expect(message.message.length).toBeGreaterThan(0);
}
});
it('should handle package with multiple publint issues', async () => {
const tarball = await createTarballFromPackage(
{
name: 'test-package',
version: '1.0.0',
type: 'module',
main: './missing-main.js',
exports: {
'.': './missing-export.js'
},
module: './missing-module.js'
},
{
'index.js': 'export const test = "hello";'
}
);
const fileSystem = new TarballFileSystem(tarball);
const result = await runPublint(fileSystem);
expect(result.messages.length).toBeGreaterThan(0);
expect(result.messages.some((m) => m.severity === 'error')).toBe(true);
});
it('should handle package without issues', async () => {
const tarball = await createTarballFromPackage(
{
name: 'test-package',
version: '1.0.0',
type: 'module',
exports: {
'.': './index.js'
}
},
{
'index.js': 'export const test = "hello";'
}
);
const fileSystem = new TarballFileSystem(tarball);
const result = await runPublint(fileSystem);
expect(result).toHaveProperty('messages');
expect(Array.isArray(result.messages)).toBe(true);
});
});