Skip to content

Commit

Permalink
refactor: consolidate download locations to single method
Browse files Browse the repository at this point in the history
  • Loading branch information
sandor-trombitas committed Jul 26, 2024
1 parent 1161daf commit 8705093
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
6 changes: 4 additions & 2 deletions ts-binary-wrapper/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const errorContextMessage = 'Download Error';

if (process.argv.includes('exec')) {
const filenameShasum = config.getShasumFile();
const downloadUrl = config.getDownloadLocation();
const backupUrl = config.getBackupDownloadLocation();
const {
downloadUrl,
backupUrl
} = config.getDownloadLocations();

const downloadError = await common.downloadWithBackup(
downloadUrl,
Expand Down
22 changes: 10 additions & 12 deletions ts-binary-wrapper/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export class WrapperConfiguration {
this.expectedSha256sum = expectedSha256sum;
}

private buildDownloadUrl(baseUrl: string): string {
return `${baseUrl}/v${this.version}/${this.binaryName}`;
}
// private buildDownloadUrl(baseUrl: string): string {
// return `${baseUrl}/v${this.version}/${this.binaryName}`;
// }

public getVersion(): string {
return this.version;
Expand All @@ -50,16 +50,14 @@ export class WrapperConfiguration {
return this.binaryName;
}

public getDownloadLocation(): string {
public getDownloadLocations(): { downloadUrl: string; backupUrl: string } {
const baseUrl = 'https://downloads.snyk.io/cli';
const backupUrl = 'https://static.snyk.io/cli';

return this.buildDownloadUrl(baseUrl);
}

public getBackupDownloadLocation(): string {
const baseUrl = 'https://static.snyk.io/cli';

return this.buildDownloadUrl(baseUrl);
return {
downloadUrl: `${baseUrl}/v${this.version}/${this.binaryName}`,
backupUrl: `${backupUrl}/v${this.version}/${this.binaryName}`,
};
}

public getLocalLocation(): string {
Expand Down Expand Up @@ -374,7 +372,7 @@ export async function downloadWithBackup(

export async function logError(
context: string,
err,
err: Error,
printToConsole = true,
): Promise<void> {
if (isAnalyticsEnabled()) {
Expand Down
6 changes: 4 additions & 2 deletions ts-binary-wrapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ function run(executable: string): number {
try {
const config = common.getCurrentConfiguration();
const executable = config.getLocalLocation();
const { downloadUrl, backupUrl } = config.getDownloadLocations();

if (!fs.existsSync(executable)) {
console.error("Executable doesn't exist, trying to download.");

const downloadError = await common.downloadExecutable(
config.getDownloadLocation(),
const downloadError = await common.downloadWithBackup(
downloadUrl,
backupUrl,
executable,
config.getShasumFile(),
);
Expand Down
2 changes: 2 additions & 0 deletions ts-binary-wrapper/test/acceptance/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ describe('Basic acceptance test', () => {
{ shell: true },
);

console.log(resultIndex);

if (resultIndex.status != 0) {
console.debug(resultIndex.stdout.toString());
console.debug(resultIndex.stderr.toString());
Expand Down
30 changes: 17 additions & 13 deletions ts-binary-wrapper/test/unit/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('Configuration', () => {
'1234abcdef',
);

const actualDownloadLocation = config.getDownloadLocation();
const actualDownloadLocation = config.getDownloadLocations().downloadUrl;
expect(actualDownloadLocation).toEqual(expectedDownloadLocation);

const actualLocalLocation = config.getLocalLocation();
Expand Down Expand Up @@ -219,17 +219,18 @@ describe('Testing binary bootstrapper', () => {

// download the shasum first, here we don't expect a shasum comparison
const shasumDownload = await common.downloadExecutable(
config.getDownloadLocation() + shafileExtension,
config.getDownloadLocations().downloadUrl + shafileExtension,
shasumFile,
'',
);
expect(shasumDownload).toBeUndefined();
expect(fs.existsSync(shasumFile)).toBeTruthy();
const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile);

const { downloadUrl } = config.getDownloadLocations();
// download binary next and use previously downloaded shasum to check validity
const binaryDownload = await common.downloadExecutable(
config.getDownloadLocation(),
downloadUrl,
config.getLocalLocation(),
expectedShasum,
);
Expand All @@ -241,10 +242,9 @@ describe('Testing binary bootstrapper', () => {

try {
// check if the binary is executable
fs.accessSync(config.getLocalLocation(), fs.constants.X_OK);
expect(fs.accessSync(config.getLocalLocation(), fs.constants.X_OK)).not.toThrow();
} catch {
// execution of binary not possible
expect(false).toBeTruthy();
}

fs.unlinkSync(shasumFile);
Expand All @@ -256,11 +256,12 @@ describe('Testing binary bootstrapper', () => {
const config = new common.WrapperConfiguration('1.1080.0', binaryName, '');
const shasumFile =
config.getLocalLocation() + Math.random() + shafileExtension;
const { downloadUrl } = config.getDownloadLocations();

// download the shasum first, here we don't expect a shasum comparison
const shasumDownload = await common.downloadWithBackup(
'https://notdownloads.snyk.io/cli/v1.1080.0/snyk-macos.sha256',
config.getDownloadLocation() + shafileExtension,
downloadUrl + shafileExtension,
shasumFile,
'',
);
Expand All @@ -269,8 +270,9 @@ describe('Testing binary bootstrapper', () => {
const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile);

// download binary next and use previously downloaded shasum to check validity
const binaryDownload = await common.downloadExecutable(
config.getDownloadLocation(),
const binaryDownload = await common.downloadWithBackup(
'https://notdownloads.snyk.io/cli/v1.1080.0/snyk-macos',
downloadUrl,
config.getLocalLocation(),
expectedShasum,
);
Expand All @@ -282,10 +284,9 @@ describe('Testing binary bootstrapper', () => {

try {
// check if the binary is executable
fs.accessSync(config.getLocalLocation(), fs.constants.X_OK);
expect(fs.accessSync(config.getLocalLocation(), fs.constants.X_OK)).not.toThrow();
} catch {
// execution of binary not possible
expect(false).toBeTruthy();
}

fs.unlinkSync(shasumFile);
Expand All @@ -298,10 +299,12 @@ describe('Testing binary bootstrapper', () => {
const config = new common.WrapperConfiguration('1.1080.0', binaryName, '');
const shasumFile =
config.getLocalLocation() + Math.random() + shafileExtension;
const { downloadUrl } = config.getDownloadLocations();


// download just any file and state a shasum expectation that never can be fullfilled
const shasumDownload = await common.downloadExecutable(
config.getDownloadLocation() + shafileExtension,
downloadUrl + shafileExtension,
shasumFile,
'incorrect-shasum',
);
Expand All @@ -315,10 +318,11 @@ describe('Testing binary bootstrapper', () => {
const config = new common.WrapperConfiguration('1.1080.0', binaryName, '');
const shasumFile =
config.getLocalLocation() + Math.random() + shafileExtension;
const { downloadUrl } = config.getDownloadLocations();

// try to download a file that doesn't exis
const shasumDownload = await common.downloadExecutable(
config.getDownloadLocation() + shafileExtension,
downloadUrl + shafileExtension,
shasumFile,
'incorrect-shasum',
);
Expand All @@ -339,7 +343,7 @@ describe('Testing binary bootstrapper', () => {
});
});

describe('isAnalyticsEnabled ', () => {
describe('isAnalyticsEnabled', () => {
it('enabled', async () => {
delete process.env.SNYK_DISABLE_ANALYTICS;
expect(common.isAnalyticsEnabled()).toBeTruthy();
Expand Down

0 comments on commit 8705093

Please sign in to comment.