Skip to content

Commit 136eb91

Browse files
authored
fix(build): fix hash file parsing (#1950)
In 2da3c31, we moved file hash computation for our produced artifacts from the JSON feed generation step to the newly introduced "download artifacts" step of the `release_publish` tasks (because we need to download artifacts before we can submit them to the papertrail service). We used the standard-ish SHASUMS format (output of `sha256sum`/input of `sha256sum -c`) for that, but failed to parse it properly when consuming it. This fixes that and extends the download center config tests to also perform the full "download artifacts" step as part of the test setup, to ensure that the two parts work together properly.
1 parent ffb9fa0 commit 136eb91

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

packages/build/src/download-center/config.spec.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { DownloadCenterConfig } from '@mongodb-js/dl-center/dist/download-center-config';
2-
import { getPackageFile, type PackageInformationProvider } from '../packaging';
2+
import { type PackageInformationProvider } from '../packaging';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
5-
import { ALL_PACKAGE_VARIANTS, type PackageVariant } from '../config';
5+
import type { Config } from '../config';
6+
import { type PackageVariant } from '../config';
67
import {
78
createVersionConfig,
89
createDownloadCenterConfig,
@@ -13,6 +14,10 @@ import {
1314
import { promises as fs } from 'fs';
1415
import path from 'path';
1516
import fetch from 'node-fetch';
17+
import { createServer } from 'http';
18+
import { once } from 'events';
19+
import { runDownloadAndListArtifacts } from '../run-download-and-list-artifacts';
20+
import type { AddressInfo } from 'net';
1621

1722
const packageInformation = (version: string) =>
1823
((packageVariant: PackageVariant) => {
@@ -33,7 +38,7 @@ const packageInformation = (version: string) =>
3338

3439
describe('DownloadCenter config', function () {
3540
let outputDir: string;
36-
beforeEach(async function () {
41+
before(async function () {
3742
outputDir = path.join(
3843
__dirname,
3944
'..',
@@ -45,26 +50,32 @@ describe('DownloadCenter config', function () {
4550
);
4651
await fs.mkdir(outputDir, { recursive: true });
4752

48-
const testVersions = ['2.0.1', '2.0.0', '1.2.2'];
49-
const allFiles = testVersions
50-
.flatMap((version) =>
51-
ALL_PACKAGE_VARIANTS.map(
52-
(packageVariant) =>
53-
getPackageFile(packageVariant, packageInformation(version)).path
54-
)
55-
)
56-
.flatMap((file) => [file, `${file}.sig`]);
57-
await fs.writeFile(
58-
path.join(outputDir, 'SHASUMS1.txt'),
59-
allFiles.map((f) => `ghjklm ${f}`).join('\n')
60-
);
61-
await fs.writeFile(
62-
path.join(outputDir, 'SHASUMS256.txt'),
63-
allFiles.map((f) => `abcdef ${f}`).join('\n')
64-
);
53+
const httpServer = createServer((req, res) => {
54+
res.end(req.url);
55+
});
56+
httpServer.listen(0);
57+
await once(httpServer, 'listening');
58+
try {
59+
const testVersions = ['2.0.1', '2.0.0', '1.2.2'];
60+
61+
for (const version of testVersions) {
62+
const config: Partial<Config> = {
63+
outputDir,
64+
packageInformation: packageInformation(version),
65+
};
66+
await runDownloadAndListArtifacts(
67+
config as Config,
68+
`http://localhost:${(httpServer.address() as AddressInfo).port}/`,
69+
'append-to-hash-file-for-testing'
70+
);
71+
}
72+
} finally {
73+
httpServer.close();
74+
await once(httpServer, 'close');
75+
}
6576
});
6677

67-
afterEach(async function () {
78+
after(async function () {
6879
await fs.rm(outputDir, { recursive: true });
6980
});
7081

@@ -376,8 +387,9 @@ describe('DownloadCenter config', function () {
376387
archive: {
377388
type: 'zip',
378389
url: `${baseUrl}mongosh-1.2.2-darwin-x64.zip`,
379-
sha256: 'abcdef',
380-
sha1: 'ghjklm',
390+
sha256:
391+
'ed8922ef572c307634150bf78ea02338349949f29245123884b1318cdc402dd9',
392+
sha1: '4f1b987549703c58940be9f8582f4032374b1074',
381393
},
382394
});
383395
});

packages/build/src/download-center/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@ async function getHashes(
293293
);
294294
const line = content
295295
.split('\n')
296-
.find((line) => line.endsWith(packagedFilename));
296+
.find((line) => line.trim().startsWith(packagedFilename));
297297
if (!line) {
298298
throw new Error(
299299
`Could not find entry for ${packagedFilename} in ${filename}`
300300
);
301301
}
302-
return [hash, line.trim().split(/\s/)[0]] as const;
302+
return [hash, line.trim().split(/\s+/)[1]] as const;
303303
})
304304
)
305305
) as { sha1: string; sha256: string };

packages/build/src/run-download-and-list-artifacts.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const hashListFiles = [
1717

1818
export async function runDownloadAndListArtifacts(
1919
config: Config,
20-
publicArtifactBaseUrl: string = ARTIFACTS_URL_PUBLIC_BASE
20+
publicArtifactBaseUrl: string = ARTIFACTS_URL_PUBLIC_BASE,
21+
hashFileWriteMode: 'normal' | 'append-to-hash-file-for-testing' = 'normal'
2122
): Promise<void> {
2223
const requiredConfigKeys: (keyof Config)[] = ['outputDir'];
2324
for (const key of requiredConfigKeys) {
@@ -66,7 +67,10 @@ export async function runDownloadAndListArtifacts(
6667
.filter(Boolean)
6768
.map((file) => `${file?.filename} ${file?.[hash]}`)
6869
.join('\n') + '\n';
69-
await fs.writeFile(filepath, contents);
70+
await (hashFileWriteMode === 'normal' ? fs.writeFile : fs.appendFile)(
71+
filepath,
72+
contents
73+
);
7074
console.log('wrote hash list to', filepath);
7175
}
7276
}

0 commit comments

Comments
 (0)