Skip to content

Commit

Permalink
fix: cover other arch types for cli download
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Oct 30, 2024
1 parent c82b0f1 commit 806e367
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
29 changes: 19 additions & 10 deletions src/snyk/cli/cliExecutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import { Platform } from '../common/platform';
export class CliExecutable {
public static filenameSuffixes: Record<CliSupportedPlatform, string> = {
linux: 'snyk-linux',
linux_arm64: 'snyk-linux-arm64',

Check warning on line 10 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Identifier 'linux_arm64' is not in camel case

Check warning on line 10 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Identifier 'linux_arm64' is not in camel case
linux_alpine: 'snyk-alpine',

Check warning on line 11 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Identifier 'linux_alpine' is not in camel case

Check warning on line 11 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Identifier 'linux_alpine' is not in camel case
linux_alpine_arm64: 'snyk-alpine-arm64',

Check warning on line 12 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Identifier 'linux_alpine_arm64' is not in camel case

Check warning on line 12 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Identifier 'linux_alpine_arm64' is not in camel case
macos: 'snyk-macos',
macos_arm64: 'snyk-macos-arm64',

Check warning on line 14 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Identifier 'macos_arm64' is not in camel case

Check warning on line 14 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Identifier 'macos_arm64' is not in camel case
windows: 'snyk-win.exe',
windows_arm64: 'snyk-win.exe',

Check warning on line 16 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Identifier 'windows_arm64' is not in camel case

Check warning on line 16 in src/snyk/cli/cliExecutable.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Identifier 'windows_arm64' is not in camel case
};
constructor(public readonly version: string, public readonly checksum: Checksum) {}

static async getPath(extensionDir: string, customPath?: string): Promise<string> {
// check if custom path is file
if (customPath) {
return customPath;
}
Expand All @@ -29,29 +33,34 @@ export class CliExecutable {
}

static async getCurrentWithArch(): Promise<CliSupportedPlatform> {
let platform = '';
const osName = Platform.getCurrent().toString().toLowerCase();
const archName = Platform.getArch().toLowerCase();
let archSuffix = Platform.getArch().toLowerCase();
if (archSuffix !== 'arm64') {
archSuffix = '';
}

const platform = await this.getPlatformName(osName);

return (platform + archSuffix) as CliSupportedPlatform;
}

static async getPlatformName(osName: string): Promise<string> {
let platform = '';
if (osName === 'linux') {
if (await this.isAlpine()) {
platform = 'linux_alpine';
} else {
platform = 'linux';
}
} else if (osName === 'darwin') {
if (archName === 'arm64') {
platform = 'macos_arm64';
} else {
platform = 'macos';
}
} else if (osName.includes('win')) {
platform = 'macos';
} else if (osName === 'win32') {
platform = 'windows';
}
if (!platform) {
throw new Error(`${osName} is unsupported.`);
}

return platform as CliSupportedPlatform;
return platform;
}

static async exists(extensionDir: string, customPath?: string): Promise<boolean> {
Expand Down
11 changes: 10 additions & 1 deletion src/snyk/cli/supportedPlatforms.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
const SupportedCliPlatformsList = ['linux', 'linux_alpine', 'windows', 'macos', 'macos_arm64'] as const;
const SupportedCliPlatformsList = [
'linux',
'linux_arm64',
'linux_alpine',
'linux_alpine_arm64',
'windows',
'windows_arm64',
'macos',
'macos_arm64',
] as const;
export type CliSupportedPlatform = typeof SupportedCliPlatformsList[number];
4 changes: 2 additions & 2 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class Configuration implements IConfiguration {
}

async setCliPath(cliPath: string | undefined): Promise<void> {
if (!cliPath && extensionContext) {
if (!cliPath) {
cliPath = await CliExecutable.getPath(extensionContext.extensionPath);
}
return this.workspace.updateConfiguration(
Expand Down Expand Up @@ -530,7 +530,7 @@ export class Configuration implements IConfiguration {
CONFIGURATION_IDENTIFIER,
this.getConfigName(ADVANCED_CLI_PATH),
);
if (!cliPath && extensionContext) {
if (!cliPath) {
cliPath = await CliExecutable.getPath(extensionContext.extensionPath);
await this.setCliPath(cliPath);
}
Expand Down
1 change: 0 additions & 1 deletion src/snyk/common/constants/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const DID_CHANGE_CONFIGURATION_METHOD = 'workspace/didChangeConfiguration

// custom methods
export const SNYK_HAS_AUTHENTICATED = '$/snyk.hasAuthenticated';
export const SNYK_CLI_PATH = '$/snyk.isAvailableCli';
export const SNYK_ADD_TRUSTED_FOLDERS = '$/snyk.addTrustedFolders';
export const SNYK_SCAN = '$/snyk.scan';
export const SNYK_FOLDERCONFIG = '$/snyk.folderConfigs';
1 change: 0 additions & 1 deletion src/snyk/common/languageServer/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IAuthenticationService } from '../../base/services/authenticationServic
import { FolderConfig, IConfiguration } from '../configuration/configuration';
import {
SNYK_ADD_TRUSTED_FOLDERS,
SNYK_CLI_PATH,
SNYK_FOLDERCONFIG,
SNYK_HAS_AUTHENTICATED,
SNYK_LANGUAGE_SERVER_NAME,
Expand Down
26 changes: 24 additions & 2 deletions src/test/unit/cli/cliExecutable.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strictEqual } from 'assert';
import path from 'path';
import fs from 'fs/promises';
import sinon from 'sinon';
import { CliExecutable } from '../../../snyk/cli/cliExecutable';
import { Platform } from '../../../snyk/common/platform';
Expand All @@ -19,25 +20,46 @@ suite('CliExecutable', () => {

test('Returns correct extension paths', async () => {
const unixExtensionDir = '/Users/user/.vscode/extensions/snyk-security.snyk-vulnerability-scanner-1.1.0';
const winExtensionDir = `C:\\Users\\user\\.vscode\\extensions`;

const osStub = sinon.stub(Platform, 'getCurrent').returns('darwin');
const archStub = sinon.stub(Platform, 'getArch').returns('x64');

let expectedCliPath = path.join(unixExtensionDir, 'snyk-macos');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

osStub.returns('linux');
expectedCliPath = path.join(unixExtensionDir, 'snyk-linux');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

const winExtensionDir = `C:\\Users\\user\\.vscode\\extensions`;
sinon.stub(fs, 'access').returns(Promise.resolve());
expectedCliPath = path.join(unixExtensionDir, 'snyk-linux-alpine');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);
sinon.stub(fs, 'access').returns(Promise.reject());

osStub.returns('win32');
expectedCliPath = path.join(winExtensionDir, 'snyk-win.exe');
strictEqual(await CliExecutable.getPath(winExtensionDir), expectedCliPath);

osStub.returns('darwin');
// test arm64
archStub.returns('arm64');

osStub.returns('darwin');
expectedCliPath = path.join(unixExtensionDir, 'snyk-macos-arm64');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

osStub.returns('linux');
expectedCliPath = path.join(unixExtensionDir, 'snyk-linux-arm64');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

sinon.stub(fs, 'access').returns(Promise.resolve());
expectedCliPath = path.join(unixExtensionDir, 'snyk-linux-alpine-arm64');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);
sinon.stub(fs, 'access').returns(Promise.reject());

osStub.returns('win32');
expectedCliPath = path.join(winExtensionDir, 'snyk-win.exe');
strictEqual(await CliExecutable.getPath(winExtensionDir), expectedCliPath);
});

test('Return custom path, if provided', async () => {
Expand Down

0 comments on commit 806e367

Please sign in to comment.