Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/command/file-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class FileDownloadCommand extends Command {
try {
const container = createContainer();

const env = container.get(EnvService);
const env = await container.getAsync(EnvService);
const path = dirname(this.output);

await mkdir(path, { recursive: true });
Expand Down
2 changes: 1 addition & 1 deletion src/cli/command/file-exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class FileExistsCommand extends Command {
try {
const container = createContainer();

const env = container.get(EnvService);
const env = await container.getAsync(EnvService);
const nUrl = env.replaceUrl(this.url);
const res = await got.head(nUrl, {
throwHttpErrors: false,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/command/install-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class InstallToolCommand extends Command {
override async execute(): Promise<number | void> {
const start = Date.now();

if (isToolIgnored(this.name)) {
if (await isToolIgnored(this.name)) {
logger.info({ tool: this.name }, 'tool ignored');
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/command/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ describe('cli/command/utils', () => {
expect(getVersion('del-cli')).toBe('1.0.1');
});

test('isToolIgnored', () => {
expect(isToolIgnored('node')).toBe(false);
test('isToolIgnored', async () => {
expect(await isToolIgnored('node')).toBe(false);
env.IGNORED_TOOLS = 'node,pnpm';
expect(isToolIgnored('node')).toBe(true);
expect(isToolIgnored('php')).toBe(false);
expect(await isToolIgnored('node')).toBe(true);
expect(await isToolIgnored('php')).toBe(false);
});
});
4 changes: 2 additions & 2 deletions src/cli/command/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function getVersion(tool: string): string | undefined {
return env[tool.replace('-', '_').toUpperCase() + '_VERSION'];
}

export function isToolIgnored(tool: string): boolean {
export async function isToolIgnored(tool: string): Promise<boolean> {
const container = createContainer();
return container.get(EnvService).isToolIgnored(tool);
return (await container.getAsync(EnvService)).isToolIgnored(tool);
}
13 changes: 10 additions & 3 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function prepareResolveContainer(): Container {
return container;
}

export function installTool(
export async function installTool(
tool: string,
version: string,
dryRun = false,
Expand Down Expand Up @@ -225,7 +225,11 @@ export function installTool(
}
}
}
return container.get(InstallToolService).install(tool, version, dryRun);
return (await container.getAsync(InstallToolService)).install(
tool,
version,
dryRun,
);
}

export async function resolveVersion(
Expand Down Expand Up @@ -266,5 +270,8 @@ export async function resolveVersion(
}
}
}
return await container.get(ToolVersionResolverService).resolve(tool, version);
return (await container.getAsync(ToolVersionResolverService)).resolve(
tool,
version,
);
}
12 changes: 6 additions & 6 deletions src/cli/install-tool/install-tool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ describe('cli/install-tool/install-tool', () => {
}
});

beforeEach(() => {
beforeEach(async () => {
child = createContainer(parent);
install = child.get(InstallToolService);
install = await child.getAsync(InstallToolService);
});

test('writes version if tool is not installed', async () => {
const ver = child.get(VersionService);
const bun = child.get<BunInstallService>(INSTALL_TOOL_TOKEN);
const ver = await child.getAsync(VersionService);
const bun = await child.getAsync<BunInstallService>(INSTALL_TOOL_TOKEN);
vi.mocked(bun).needsInitialize.mockResolvedValueOnce(true);
vi.mocked(bun).needsPrepare.mockResolvedValueOnce(true);
expect(await install.install('bun', '1.0.0')).toBeUndefined();
expect(await ver.find('bun')).toBe('1.0.0');
});

test('writes version even if tool is installed', async () => {
const ver = child.get(VersionService);
const bun = child.get<BunInstallService>(INSTALL_TOOL_TOKEN);
const ver = await child.getAsync(VersionService);
const bun = await child.getAsync<BunInstallService>(INSTALL_TOOL_TOKEN);
vi.mocked(bun).isInstalled.mockResolvedValueOnce(true);
expect(await install.install('bun', '1.0.1')).toBeUndefined();
expect(await ver.find('bun')).toBe('1.0.1');
Expand Down
2 changes: 1 addition & 1 deletion src/cli/prepare-tool/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('cli/prepare-tool/index', () => {
);

const child = createContainer();
const pathSvc = child.get(PathService);
const pathSvc = await child.getAsync(PathService);
await pathSvc.setPrepared('bun');
});

Expand Down
11 changes: 7 additions & 4 deletions src/cli/prepare-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ function prepareContainer(): Container {
return container;
}

export function prepareTools(
export async function prepareTools(
tools: string[],
dryRun = false,
): Promise<number | void> {
const container = prepareContainer();
return container.get(PrepareToolService).prepare(tools, dryRun);
return (await container.getAsync(PrepareToolService)).prepare(tools, dryRun);
}

export function initializeTools(
export async function initializeTools(
tools: string[],
dryRun = false,
): Promise<number | void> {
const container = prepareContainer();
return container.get(PrepareToolService).initialize(tools, dryRun);
return (await container.getAsync(PrepareToolService)).initialize(
tools,
dryRun,
);
}
6 changes: 3 additions & 3 deletions src/cli/services/apt.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('cli/services/apt.service', () => {
});

test('skips install', async () => {
const svc = child.get(AptService);
const svc = await child.getAsync(AptService);

mocks.execa.mockResolvedValueOnce({
stdout: 'Status: install ok installed',
Expand All @@ -34,7 +34,7 @@ describe('cli/services/apt.service', () => {
});

test('works', async () => {
const svc = child.get(AptService);
const svc = await child.getAsync(AptService);

mocks.execa.mockRejectedValueOnce(new Error('not installed'));
await svc.install('some-pkg');
Expand All @@ -45,7 +45,7 @@ describe('cli/services/apt.service', () => {

test('uses proxy', async () => {
env.APT_HTTP_PROXY = 'http://proxy';
const svc = child.get(AptService);
const svc = await child.getAsync(AptService);

mocks.execa.mockRejectedValueOnce(new Error('not installed'));
await svc.install('some-pkg', 'other-pkg');
Expand Down
2 changes: 1 addition & 1 deletion src/cli/services/compression.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('cli/services/compression.service', () => {
});

test('extracts with bstar', async () => {
const svc = child.get(CompressionService);
const svc = await child.getAsync(CompressionService);

await expect(
svc.extract({ file: 'some.txz', cwd: globalThis.cacheDir }),
Expand Down
66 changes: 32 additions & 34 deletions src/cli/services/env.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,113 +19,111 @@ vi.mock('node:process', () => ({
describe('cli/services/env.service', () => {
let child!: Container;
let rootDir: string | undefined;
let svc!: EnvService;

beforeAll(() => {
rootDir = globalThis.rootDir;
});

beforeEach(() => {
beforeEach(async () => {
child = createContainer();
globalThis.rootDir = rootDir;
mocks.arch.mockReturnValue('x64');
svc = await child.getAsync(EnvService);
});

test('arch', () => {
mocks.arch.mockReturnValue('arm64');
expect(child.get(EnvService).arch).toBe('arm64');
test('arch', async () => {
expect(svc.arch).toBe('amd64');

mocks.arch.mockReturnValue('x64');
expect(child.get(EnvService).arch).toBe('amd64');
mocks.arch.mockReturnValue('arm64');
expect((await child.getAsync(EnvService)).arch).toBe('arm64');

mocks.arch.mockReturnValue('invalid');
expect(() => child.get(EnvService).arch).toThrow();
await expect(
async () => (await child.getAsync(EnvService)).arch,
).rejects.toThrow();
});

test('isRoot', () => {
expect(child.get(EnvService).isRoot).toBe(true);
expect(svc.isRoot).toBe(true);
});

test('home', () => {
expect(child.get(EnvService).home).toBeUndefined();
expect(svc.home).toBeUndefined();
});

test('rootHome', () => {
expect(child.get(EnvService).rootHome).toBe(rootPath('root'));
expect(svc.rootHome).toBe(rootPath('root'));
});

test('userHome', () => {
expect(child.get(EnvService).userHome).toBe(rootPath('home/ubuntu'));
expect(svc.userHome).toBe(rootPath('home/ubuntu'));
});

test('userId', () => {
expect(child.get(EnvService).userId).toBe(12021);
expect(svc.userId).toBe(12021);
});

test('umask', () => {
const e = child.get(EnvService);
expect(e.umask).toBe(0o755);
Object.assign(e, { uid: 12021 });
expect(e.umask).toBe(0o775);
expect(svc.umask).toBe(0o755);
Object.assign(svc, { uid: 12021 });
expect(svc.umask).toBe(0o775);
});

test('skipTests', () => {
expect(child.get(EnvService).skipTests).toBe(false);
expect(svc.skipTests).toBe(false);
});

test('cacheDir', () => {
delete env.CONTAINERBASE_CACHE_DIR;
expect(child.get(EnvService).cacheDir).toBeNull();
expect(svc.cacheDir).toBeNull();
});

describe('rootDir', () => {
test('uses test root', () => {
const e = child.get(EnvService);
expect(e.rootDir).toBe(rootPath());
expect(svc.rootDir).toBe(rootPath());
});

test('uses default root', () => {
globalThis.rootDir = undefined;
const e = child.get(EnvService);
expect(e.rootDir).toBe(sep);
expect(svc.rootDir).toBe(sep);
});
});

test('isToolIgnored', () => {
let e = child.get(EnvService);
expect(e.isToolIgnored('npm')).toBe(false);
expect(e.isToolIgnored('node')).toBe(false);
test('isToolIgnored', async () => {
expect(svc.isToolIgnored('npm')).toBe(false);
expect(svc.isToolIgnored('node')).toBe(false);

env.IGNORED_TOOLS = 'npm,yarn';
e = child.get(EnvService);
const e = await child.getAsync(EnvService);
expect(e.isToolIgnored('npm')).toBe(true);
expect(e.isToolIgnored('node')).toBe(false);
});

test('replaceUrl', () => {
const e = child.get(EnvService);
env.URL_REPLACE_0_FROM = 'https://example.com';
env.URL_REPLACE_0_TO = 'https://example.test';
env.URL_REPLACE_1_FROM = 'https://cdn.example.com/registry.npmjs.org';
env.URL_REPLACE_1_TO = 'https://npm.example.test';

expect(e.replaceUrl('https://example.com/file.txt')).toBe(
expect(svc.replaceUrl('https://example.com/file.txt')).toBe(
'https://example.test/file.txt',
);
expect(e.replaceUrl('https://example.org/file.txt')).toBe(
expect(svc.replaceUrl('https://example.org/file.txt')).toBe(
'https://example.org/file.txt',
);

env.CONTAINERBASE_CDN = `https://cdn.example.com/`;
expect(e.replaceUrl('https://localhost')).toBe(
expect(svc.replaceUrl('https://localhost')).toBe(
'https://cdn.example.com/localhost',
);
expect(e.replaceUrl('https://example.test/file.txt')).toBe(
expect(svc.replaceUrl('https://example.test/file.txt')).toBe(
'https://cdn.example.com/example.test/file.txt',
);
expect(e.replaceUrl('https://registry.npmjs.org')).toBe(
expect(svc.replaceUrl('https://registry.npmjs.org')).toBe(
'https://npm.example.test',
);
expect(e.replaceUrl('https://registry.npmjs.org', false)).toBe(
expect(svc.replaceUrl('https://registry.npmjs.org', false)).toBe(
'https://registry.npmjs.org',
);
});
Expand Down
Loading