-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathregistry.test.ts
More file actions
314 lines (275 loc) Β· 9.39 KB
/
registry.test.ts
File metadata and controls
314 lines (275 loc) Β· 9.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { mkdtempSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import {
getPackageManifest,
RegistryPackageType,
InitialManifestData,
} from '../registry';
describe('getPackageManifest', () => {
let tempDir: string;
beforeEach(() => {
tempDir = mkdtempSync(path.join(tmpdir(), 'craft-registry-test-'));
});
afterEach(() => {
rmSync(tempDir, { recursive: true, force: true });
});
describe('when package already exists', () => {
it('reads the existing latest.json manifest', async () => {
const packageDir = path.join(
tempDir,
'packages',
'npm',
'@sentry',
'browser',
);
fs.mkdirSync(packageDir, { recursive: true });
const existingManifest = {
canonical: 'npm:@sentry/browser',
name: 'Sentry Browser SDK',
version: '1.0.0',
repo_url: 'https://github.com/getsentry/sentry-javascript',
};
fs.writeFileSync(
path.join(packageDir, 'latest.json'),
JSON.stringify(existingManifest),
);
const result = await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/browser',
'1.1.0',
);
expect(result.packageManifest).toEqual(existingManifest);
expect(result.versionFilePath).toBe(path.join(packageDir, '1.1.0.json'));
});
it('throws an error if version file already exists', async () => {
const packageDir = path.join(
tempDir,
'packages',
'npm',
'@sentry',
'browser',
);
fs.mkdirSync(packageDir, { recursive: true });
fs.writeFileSync(
path.join(packageDir, 'latest.json'),
JSON.stringify({ canonical: 'npm:@sentry/browser' }),
);
fs.writeFileSync(path.join(packageDir, '1.0.0.json'), '{}');
await expect(
getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/browser',
'1.0.0',
),
).rejects.toThrow('Version file for "1.0.0" already exists');
});
});
describe('when package does not exist (new package)', () => {
it('creates directory structure and returns initial manifest', async () => {
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/wasm',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
name: 'Sentry WASM',
packageUrl: 'https://www.npmjs.com/package/@sentry/wasm',
mainDocsUrl: 'https://docs.sentry.io/platforms/javascript/',
};
const result = await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/wasm',
'0.1.0',
initialData,
);
// Check directory was created
const packageDir = path.join(
tempDir,
'packages',
'npm',
'@sentry',
'wasm',
);
expect(fs.existsSync(packageDir)).toBe(true);
// Check manifest has correct fields
expect(result.packageManifest).toEqual({
canonical: 'npm:@sentry/wasm',
repo_url: 'https://github.com/getsentry/sentry-javascript',
name: 'Sentry WASM',
package_url: 'https://www.npmjs.com/package/@sentry/wasm',
main_docs_url: 'https://docs.sentry.io/platforms/javascript/',
});
// Check version file path
expect(result.versionFilePath).toBe(path.join(packageDir, '0.1.0.json'));
});
it('creates initial manifest with only required fields when optional are not provided', async () => {
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/minimal',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
};
const result = await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/minimal',
'1.0.0',
initialData,
);
expect(result.packageManifest).toEqual({
canonical: 'npm:@sentry/minimal',
repo_url: 'https://github.com/getsentry/sentry-javascript',
});
});
it('throws an error when package does not exist and no initial data provided', async () => {
await expect(
getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/new-package',
'1.0.0',
),
).rejects.toThrow(
'Package "npm:@sentry/new-package" does not exist in the registry and no initial manifest data was provided',
);
});
it('works with APP type packages', async () => {
const initialData: InitialManifestData = {
canonical: 'app:craft',
repoUrl: 'https://github.com/getsentry/craft',
name: 'Craft',
};
const result = await getPackageManifest(
tempDir,
RegistryPackageType.APP,
'app:craft',
'2.0.0',
initialData,
);
// Check directory was created
const packageDir = path.join(tempDir, 'apps', 'craft');
expect(fs.existsSync(packageDir)).toBe(true);
expect(result.packageManifest).toEqual({
canonical: 'app:craft',
repo_url: 'https://github.com/getsentry/craft',
name: 'Craft',
});
});
it('includes apiDocsUrl when provided', async () => {
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/core',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
apiDocsUrl: 'https://docs.sentry.io/api/',
};
const result = await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/core',
'1.0.0',
initialData,
);
expect(result.packageManifest).toEqual({
canonical: 'npm:@sentry/core',
repo_url: 'https://github.com/getsentry/sentry-javascript',
api_docs_url: 'https://docs.sentry.io/api/',
});
});
describe('sdkName symlink creation', () => {
it('creates a sdks/ symlink when sdkName is provided', async () => {
fs.mkdirSync(path.join(tempDir, 'sdks'), { recursive: true });
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/hono',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
name: 'Sentry Hono SDK',
sdkName: 'sentry.javascript.hono',
};
await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/hono',
'1.0.0',
initialData,
);
const symlinkPath = path.join(tempDir, 'sdks', 'sentry.javascript.hono');
expect(fs.existsSync(symlinkPath)).toBe(true);
expect(fs.lstatSync(symlinkPath).isSymbolicLink()).toBe(true);
expect(fs.readlinkSync(symlinkPath)).toBe(
path.join('..', 'packages', 'npm', '@sentry', 'hono'),
);
});
it('does not create a sdks/ symlink when sdkName is not provided', async () => {
fs.mkdirSync(path.join(tempDir, 'sdks'), { recursive: true });
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/hono',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
};
await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/hono',
'1.0.0',
initialData,
);
const sdksDir = path.join(tempDir, 'sdks');
expect(fs.readdirSync(sdksDir)).toHaveLength(0);
});
it('skips symlink creation when the symlink already exists', async () => {
fs.mkdirSync(path.join(tempDir, 'sdks'), { recursive: true });
const symlinkPath = path.join(tempDir, 'sdks', 'sentry.javascript.hono');
const existingTarget = path.join(
'..',
'packages',
'npm',
'@sentry',
'hono',
);
fs.symlinkSync(existingTarget, symlinkPath);
const initialData: InitialManifestData = {
canonical: 'npm:@sentry/hono',
repoUrl: 'https://github.com/getsentry/sentry-javascript',
sdkName: 'sentry.javascript.hono',
};
// Should not throw or overwrite the existing symlink
await expect(
getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/hono',
'1.0.0',
initialData,
),
).resolves.not.toThrow();
expect(fs.readlinkSync(symlinkPath)).toBe(existingTarget);
});
it('does not create a sdks/ symlink for existing packages (only new ones)', async () => {
fs.mkdirSync(path.join(tempDir, 'sdks'), { recursive: true });
// Set up an existing package with latest.json
const packageDir = path.join(
tempDir,
'packages',
'npm',
'@sentry',
'browser',
);
fs.mkdirSync(packageDir, { recursive: true });
fs.writeFileSync(
path.join(packageDir, 'latest.json'),
JSON.stringify({
canonical: 'npm:@sentry/browser',
version: '1.0.0',
}),
);
await getPackageManifest(
tempDir,
RegistryPackageType.SDK,
'npm:@sentry/browser',
'1.1.0',
// sdkName is only consulted when the package is new
);
const sdksDir = path.join(tempDir, 'sdks');
expect(fs.readdirSync(sdksDir)).toHaveLength(0);
});
});
});
});