Skip to content

Commit

Permalink
test: add test case for downloadWithBackup()
Browse files Browse the repository at this point in the history
  • Loading branch information
sandor-trombitas committed Jul 26, 2024
1 parent 5380359 commit 1836618
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ts-binary-wrapper/test/unit/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,47 @@ describe('Testing binary bootstrapper', () => {
fs.unlinkSync(shasumFile);
fs.unlinkSync(config.getLocalLocation());
});
it('downloadWithBackup() succesfull', async () => {
const binaryName = 'snyk-macos';
const shafileExtension = '.sha256';
const config = new common.WrapperConfiguration('1.1080.0', binaryName, '');
const shasumFile =
config.getLocalLocation() + Math.random() + shafileExtension;

// 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,
shasumFile,
'',
);
expect(shasumDownload).toBeUndefined();
expect(fs.existsSync(shasumFile)).toBeTruthy();
const expectedShasum = common.getCurrentSha256sum(binaryName, shasumFile);

// download binary next and use previously downloaded shasum to check validity
const binaryDownload = await common.downloadExecutable(
config.getDownloadLocation(),
config.getLocalLocation(),
expectedShasum,
);
expect(binaryDownload).toBeUndefined();
expect(fs.existsSync(config.getLocalLocation())).toBeTruthy();

const stats = fs.statSync(config.getLocalLocation());
expect(stats.mode).toEqual(0o100755);

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

fs.unlinkSync(shasumFile);
fs.unlinkSync(config.getLocalLocation());
});

it('downloadExecutable() fails due to incorrect shasum', async () => {
const binaryName = 'snyk-macos';
Expand Down

0 comments on commit 1836618

Please sign in to comment.