Repository navigation
Expand file tree
/
Copy pathtool.ts
More file actions
73 lines (68 loc) · 2 KB
/
Copy pathtool.ts
File metadata and controls
73 lines (68 loc) · 2 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
import { createHash } from 'node:crypto';
import type { Container, Newable } from 'inversify';
import { type MockInstance, vi } from 'vitest';
import type { ToolFile } from '../packages/base/src/tool-versions-types.ts';
import type { HttpChecksumType } from '../src/cli/services/http.service.ts';
import { PathService, ToolVersionsService } from '../src/cli/services/index.ts';
import { testContainer } from './di.ts';
export interface ToolContext<T> {
child: Container;
svc: T;
pathSvc: PathService;
}
/**
* Create a fresh container with the given tool service bound to itself.
*/
export async function toolContext<T>(
ctor: Newable<T>,
): Promise<ToolContext<T>> {
const child = await testContainer();
child.bind(ctor).toSelf();
return {
child,
svc: await child.getAsync(ctor),
pathSvc: await child.getAsync(PathService),
};
}
/**
* The checksum of `content`, as the checksum files of the tool releases list
* it.
*/
export function checksum(
content: string,
type: HttpChecksumType = 'sha256',
): string {
return createHash(type).update(content).digest('hex');
}
/**
* Mocks the tool-versions files of every version of every tool.
* @param files - the files of each version, without them there is no data, so
* the installers use their own download
* @returns the spy of the lookups
*/
export function mockToolFiles(
files?: ToolFile[],
): MockInstance<ToolVersionsService['getVersionFiles']> {
return vi
.spyOn(ToolVersionsService.prototype, 'getVersionFiles')
.mockImplementation((tool, version) =>
Promise.resolve(files && { tool, version, files }),
);
}
/**
* A tool-versions file of `url`, named like the last url segment, with the
* sha256 checksum of `content`.
* @param arch - the arch of the file, by default for any arch
*/
export function toolFile(
url: string,
content: string,
arch?: string,
): ToolFile {
return {
name: url.slice(url.lastIndexOf('/') + 1),
url,
checksum: `sha256:${checksum(content)}`,
...(arch ? { arch } : {}),
};
}