Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Oct 29, 2024
1 parent dd2d8a4 commit e2d0d8b
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/snyk/cli/cliExecutable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os from 'os';
import path from 'path';
import fs from 'fs/promises';
import { CliSupportedPlatform } from './supportedPlatforms';
import { Checksum } from './checksum';
import { Platform } from '../common/platform';

export class CliExecutable {
public static filenameSuffixes: Record<CliSupportedPlatform, string> = {
Expand Down Expand Up @@ -30,8 +30,8 @@ export class CliExecutable {

static async getCurrentWithArch(): Promise<CliSupportedPlatform> {
let platform = '';
const osName = os.platform().toString().toLowerCase();
const archName = os.arch().toLowerCase();
const osName = Platform.getCurrent().toString().toLowerCase();
const archName = Platform.getArch().toLowerCase();
if (osName === 'linux') {
if (await this.isAlpine()) {
platform = 'linux_alpine';
Expand Down
6 changes: 2 additions & 4 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import SecretStorageAdapter from '../vscode/secretStorage';
import { IVSCodeWorkspace } from '../vscode/workspace';
import { CliExecutable } from '../../cli/cliExecutable';
import SnykExtension from '../../extension';
import { extensionContext } from '../vscode/extensionContext';

const NEWISSUES = 'Net new issues';

Expand Down Expand Up @@ -321,7 +321,6 @@ export class Configuration implements IConfiguration {
}

async setCliPath(cliPath: string | undefined): Promise<void> {
const extensionContext = SnykExtension.getExtensionContext();
if (!cliPath && extensionContext) {
cliPath = await CliExecutable.getPath(extensionContext.extensionPath);
}
Expand Down Expand Up @@ -531,9 +530,8 @@ export class Configuration implements IConfiguration {
CONFIGURATION_IDENTIFIER,
this.getConfigName(ADVANCED_CLI_PATH),
);
const extensionContext = SnykExtension.getExtensionContext();
if (!cliPath && extensionContext) {
cliPath = await CliExecutable.getPath(SnykExtension.getExtensionContext().extensionPath);
cliPath = await CliExecutable.getPath(extensionContext.extensionPath);
await this.setCliPath(cliPath);
}
return cliPath;
Expand Down
12 changes: 12 additions & 0 deletions src/snyk/common/download/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export class Downloader {
this.extensionContext.extensionPath,
await this.configuration.getCliPath(),
);
if (await this.binaryExists(cliPath)) {
await this.deleteFileAtPath(cliPath);
}
const lsVersion = await this.cliApi.getLatestCliVersion(this.configuration.getCliReleaseChannel());
const sha256 = await this.cliApi.getSha256Checksum(lsVersion, platform);
const checksum = await this.downloadCli(cliPath, platform, sha256);
Expand All @@ -56,6 +59,15 @@ export class Downloader {
return new CliExecutable(lsVersion, checksum);
}

private async binaryExists(filePath: string): Promise<boolean> {
try {
await fsPromises.access(filePath);
return true;
} catch {
return false;
}
}

private async deleteFileAtPath(filePath: string): Promise<void> {
try {
await fsPromises.unlink(filePath);
Expand Down
4 changes: 4 additions & 0 deletions src/snyk/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class Platform {
return os.platform();
}

static getArch(): string {
return os.arch();
}

static getVersion(): string {
return `${os.release()}-${os.arch}`;
}
Expand Down
4 changes: 0 additions & 4 deletions src/snyk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ class SnykExtension extends SnykLib implements IExtension {
}
}

public static getExtensionContext(): ExtensionContext {
return extensionContext;
}

private configureGitHandlers(): void {
// Get the Git extension
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git')?.exports;
Expand Down
16 changes: 10 additions & 6 deletions src/test/unit/cli/cliExecutable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@ suite('CliExecutable', () => {
strictEqual(CliExecutable.getFileName('windows'), 'snyk-win.exe');
});

test('Returns correct extension paths', () => {
test('Returns correct extension paths', async() => {

Check failure on line 20 in src/test/unit/cli/cliExecutable.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Insert `·`
const unixExtensionDir = '/Users/user/.vscode/extensions/snyk-security.snyk-vulnerability-scanner-1.1.0';

const stub = sinon.stub(Platform, 'getCurrent').returns('darwin');
let expectedCliPath = path.join(unixExtensionDir, 'snyk-macos');
strictEqual(CliExecutable.getPath(unixExtensionDir), expectedCliPath);
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

sinon.stub(Platform, 'getArch').returns('arm64');
expectedCliPath = path.join(unixExtensionDir, 'snyk-macos-arm64');
strictEqual(await CliExecutable.getPath(unixExtensionDir), expectedCliPath);

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

const winExtensionDir = `C:\\Users\\user\\.vscode\\extensions`;
stub.returns('win32');
expectedCliPath = path.join(winExtensionDir, 'snyk-win.exe');
strictEqual(CliExecutable.getPath(winExtensionDir), expectedCliPath);
strictEqual(await CliExecutable.getPath(winExtensionDir), expectedCliPath);
});

test('Return custom path, if provided', () => {
test('Return custom path, if provided', async () => {
const customPath = '/path/to/cli';
strictEqual(CliExecutable.getPath('', customPath), customPath);
strictEqual(await CliExecutable.getPath('', customPath), customPath);
});
});
1 change: 0 additions & 1 deletion src/test/unit/common/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { deepStrictEqual, strictEqual } from 'assert';
import sinon from 'sinon';
import { Configuration, PreviewFeatures } from '../../../snyk/common/configuration/configuration';
import { SNYK_TOKEN_KEY } from '../../../snyk/common/constants/general';
import {
ADVANCED_CUSTOM_ENDPOINT,
FEATURES_PREVIEW_SETTING,
Expand Down
5 changes: 1 addition & 4 deletions src/test/unit/common/languageServer/languageServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ suite('Language Server', () => {
return Promise.resolve('testToken');
},
shouldReportErrors: true,
getSnykLanguageServerPath(): string {
return path;
},
getAdditionalCliParameters() {
return '--all-projects -d';
},
Expand Down Expand Up @@ -88,7 +85,7 @@ suite('Language Server', () => {
return [];
},
scanningMode: 'auto',
} as unknown as IConfiguration;
} as IConfiguration;

extensionContextMock = {
extensionPath: 'test/path',
Expand Down
6 changes: 3 additions & 3 deletions src/test/unit/common/languageServer/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite('Language Server: Middleware', () => {
organization: 'org',
getToken: () => Promise.resolve('token'),
isAutomaticDependencyManagementEnabled: () => true,
getCliPath: () => '/path/to/cli',
getCliPath: (): Promise<string> => Promise.resolve('/path/to/cli'),
getInsecure(): boolean {
return true;
},
Expand All @@ -58,7 +58,7 @@ suite('Language Server: Middleware', () => {
getFolderConfigs(): FolderConfig[] {
return [];
},
} as unknown as IConfiguration;
} as IConfiguration;
extensionContextMock = {
extensionPath: 'test/path',
getGlobalStateValue: contextGetGlobalStateValue,
Expand Down Expand Up @@ -112,7 +112,7 @@ suite('Language Server: Middleware', () => {
);
assert.strictEqual(
serverResult.cliPath,
CliExecutable.getPath(extensionContextMock.extensionPath, await configuration.getCliPath()),
await CliExecutable.getPath(extensionContextMock.extensionPath, await configuration.getCliPath()),
);
assert.strictEqual(serverResult.enableTrustedFoldersFeature, 'true');
assert.deepStrictEqual(serverResult.trustedFolders, configuration.getTrustedFolders());
Expand Down
24 changes: 12 additions & 12 deletions src/test/unit/common/services/downloadService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ suite('DownloadService', () => {

configuration = {
isAutomaticDependencyManagementEnabled: () => true,
getCustomCliPath: () => undefined,
getSnykLanguageServerPath: () => 'ab/c',
} as unknown as IConfiguration;
getCliReleaseChannel: () => "stable",

Check failure on line 50 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"stable"` with `'stable'`
getCliPath: () => Promise.resolve("path/to/cli"),

Check failure on line 51 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"path/to/cli"` with `'path/to/cli'`
} as IConfiguration;

downloader = new Downloader(configuration, lsApi, windowMock, logger, context);
});
Expand All @@ -61,9 +61,9 @@ suite('DownloadService', () => {
test('Tries to download LS if not installed', async () => {
configuration = {
isAutomaticDependencyManagementEnabled: () => true,
getCustomCliPath: () => undefined,
getSnykLanguageServerPath: () => 'abc/d',
} as unknown as IConfiguration;
getCliReleaseChannel: () => "stable",

Check failure on line 64 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"stable"` with `'stable'`
getCliPath: () => Promise.resolve("path/to/cli"),

Check failure on line 65 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"path/to/cli"` with `'path/to/cli'`
} as IConfiguration;
const service = new DownloadService(context, configuration, lsApi, windowMock, logger, downloader);
const downloadSpy = stub(service, 'download');
const updateSpy = stub(service, 'update');
Expand All @@ -76,9 +76,9 @@ suite('DownloadService', () => {
test('Tries to update LS if installed', async () => {
configuration = {
isAutomaticDependencyManagementEnabled: () => true,
getCustomCliPath: () => undefined,
getSnykLanguageServerPath: () => 'abc/d',
} as unknown as IConfiguration;
getCliReleaseChannel: () => "stable",

Check failure on line 79 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"stable"` with `'stable'`
getCliPath: () => Promise.resolve("path/to/cli"),

Check failure on line 80 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"path/to/cli"` with `'path/to/cli'`
} as IConfiguration;
const service = new DownloadService(context, configuration, lsApi, windowMock, logger, downloader);
stub(service, 'isCliInstalled').resolves(true);
const downloadSpy = stub(service, 'download');
Expand All @@ -93,9 +93,9 @@ suite('DownloadService', () => {
test("Doesn't download LS if automatic dependency management disabled", async () => {
configuration = {
isAutomaticDependencyManagementEnabled: () => false,
getCustomCliPath: () => undefined,
getSnykLanguageServerPath: () => 'abc/d',
} as unknown as IConfiguration;
getCliReleaseChannel: () => "stable",

Check failure on line 96 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"stable"` with `'stable'`
getCliPath: () => Promise.resolve("path/to/cli"),

Check failure on line 97 in src/test/unit/common/services/downloadService.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"path/to/cli"` with `'path/to/cli'`
} as IConfiguration;
const service = new DownloadService(context, configuration, lsApi, windowMock, logger, downloader);
stub(service, 'isCliInstalled').resolves(false);
const downloadSpy = stub(service, 'download');
Expand Down
10 changes: 5 additions & 5 deletions src/test/unit/download/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ suite('LS Downloader (LS)', () => {
logger = new LoggerMock();
configuration = {
isAutomaticDependencyManagementEnabled: () => true,
getCliPath(): string {
return 'abc/d';
getCliReleaseChannel: () => "stable",

Check failure on line 28 in src/test/unit/download/downloader.test.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Replace `"stable"` with `'stable'`
getCliPath(): Promise<string> {
return Promise.resolve('abc/d');
},
} as unknown as IConfiguration;
} as IConfiguration;
extensionContextMock = {
extensionPath: 'test/path',
updateGlobalStateValue: sinon.fake(),
Expand Down Expand Up @@ -58,8 +59,7 @@ suite('LS Downloader (LS)', () => {
const unlink = sinon.stub(fs, 'unlink');

await downloader.download();
const cliPath = await CliExecutable.getPath((await configuration.getCliPath()) as string);

const cliPath = await configuration.getCliPath() ?? "";
strictEqual(unlink.calledOnceWith(cliPath), true);
});

Expand Down
2 changes: 0 additions & 2 deletions src/test/unit/mocks/workspace.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as os from 'os';
import path from 'path';
import { IVSCodeWorkspace } from '../../../snyk/common/vscode/workspace';

export function stubWorkspaceConfiguration<T>(configSetting: string, returnValue: T | undefined): IVSCodeWorkspace {
Expand Down

0 comments on commit e2d0d8b

Please sign in to comment.