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
24 changes: 23 additions & 1 deletion src/cli/tools/jq.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LinkToolService } from '../services/index.ts';
import { JqInstallService } from './jq.ts';
import { scope } from '~test/http-mock.ts';
import { ensurePaths } from '~test/path.ts';
import { checksum, toolContext } from '~test/tool.ts';
import { checksum, mockToolFiles, toolContext, toolFile } from '~test/tool.ts';

const { execaMock } = vi.hoisted(() => ({ execaMock: vi.fn() }));
vi.mock('execa', () => ({ execa: execaMock }));
Expand All @@ -27,6 +27,8 @@ describe('cli/tools/jq', () => {

beforeEach(() => {
vi.mocked(arch).mockReturnValue('x64');
// no tool-versions data, the binary is verified against the release list
mockToolFiles();
execaMock.mockResolvedValue({ failed: false });
});

Expand Down Expand Up @@ -59,6 +61,26 @@ describe('cli/tools/jq', () => {
},
);

test('install: downloads the tool-versions file without the list', async () => {
const filename = 'jq-linux-amd64';
mockToolFiles([
toolFile(
`${baseUrl}${releaseUrl}/jq-1.8.5/jq-linux-arm64`,
'arm',
'arm64',
),
toolFile(`${baseUrl}${releaseUrl}/jq-1.8.5/${filename}`, binary, 'amd64'),
]);
// there is no interceptor for the checksum list
scope(baseUrl).get(`${releaseUrl}/jq-1.8.5/${filename}`).reply(200, binary);
const { svc, pathSvc } = await toolContext(JqInstallService);

await expect(svc.install('1.8.5')).resolves.toBeUndefined();

const file = join(pathSvc.versionedToolPath('jq', '1.8.5'), 'bin/jq');
expect(await fs.readFile(file, 'utf8')).toBe(binary);
});

test('install: rejects a missing checksum', async () => {
const { svc } = await toolContext(JqInstallService);
scope(baseUrl)
Expand Down
17 changes: 7 additions & 10 deletions src/cli/tools/jq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,21 @@ export class JqInstallService extends BaseInstallService {
override readonly minVersion = '1.7.1';

/**
* Downloads the jq binary from GitHub, verified against the release's
* `sha256sum.txt`, into the versioned `bin` folder.
* Downloads the jq binary from GitHub into the versioned `bin` folder. The
* file and its checksum come from the tool-versions data, without data it
* is verified against the release's `sha256sum.txt`.
*/
override async install(version: string): Promise<void> {
const baseUrl = `https://github.com/jqlang/${this.name}/releases/download/jq-${version}/`;
const filename = `${this.name}-linux-${this.envSvc.arch}`;

const expectedChecksum = await this.findChecksum(
`${baseUrl}sha256sum.txt`,
const file = await this.downloadChecksumListed(
version,
baseUrl,
filename,
'sha256sum.txt',
);

const file = await this.http.download({
url: `${baseUrl}${filename}`,
checksumType: 'sha256',
expectedChecksum,
});

await this.pathSvc.ensureToolPath(this.name);

const path = await this.pathSvc.createVersionedToolPath(
Expand Down
Loading