Skip to content

Commit adc82df

Browse files
test: add unit tests for isNativeAvailable in nativeWorker.ts (#235)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 4bc96ac commit adc82df

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

src/nativeWorker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import * as fs from 'fs';
1414
import { spawn, ChildProcess } from 'child_process';
1515
import * as v8 from 'node:v8';
1616

17+
import { uiKindToString } from './helpers';
18+
1719
import type { TelemetryReporter } from '@vscode/extension-telemetry';
1820
import type { DatabaseConnectionBundle } from './connectionTypes';
1921
import type {
@@ -393,6 +395,9 @@ export function mapRowsByName<T = Record<string, CellValue>>(result: NativeQuery
393395
* @returns True if native binary is available
394396
*/
395397
export async function isNativeAvailable(extensionPath: string): Promise<boolean> {
398+
if (uiKindToString(vsc.env.uiKind) === 'web') {
399+
return false;
400+
}
396401
return (await getNativeBinaryPath(extensionPath)) !== null;
397402
}
398403

tests/unit/mocks/vscode.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ export const mockVscode = {
129129
uriScheme: 'vscode',
130130
appName: 'VS Code',
131131
language: 'en',
132-
remoteName: undefined as string | undefined
132+
remoteName: undefined as string | undefined,
133+
uiKind: 2 // Desktop
134+
},
135+
UIKind: {
136+
Desktop: 2,
137+
Web: 1
133138
},
134139
ColorThemeKind: {
135140
Light: 1,

tests/unit/nativeWorker.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import './vscode_mock_setup';
2+
import { describe, it, mock, afterEach, beforeEach } from 'node:test';
3+
import assert from 'node:assert';
4+
import * as fs from 'node:fs';
5+
import * as path from 'node:path';
6+
import * as vscode from 'vscode';
7+
import { isNativeAvailable } from '../../src/nativeWorker';
8+
9+
describe('isNativeAvailable', () => {
10+
let originalPlatform: string;
11+
let originalArch: string;
12+
13+
beforeEach(() => {
14+
originalPlatform = process.platform;
15+
originalArch = process.arch;
16+
vscode.env.uiKind = 2; // Desktop
17+
});
18+
19+
afterEach(() => {
20+
Object.defineProperty(process, 'platform', { value: originalPlatform });
21+
Object.defineProperty(process, 'arch', { value: originalArch });
22+
vscode.env.uiKind = 2; // Desktop
23+
mock.restoreAll();
24+
});
25+
26+
it('should return false when UI kind is web', async () => {
27+
vscode.env.uiKind = 1; // Web
28+
29+
const result = await isNativeAvailable('/ext/path');
30+
assert.strictEqual(result, false);
31+
});
32+
33+
it('should return false when platform is unsupported', async () => {
34+
Object.defineProperty(process, 'platform', { value: 'freebsd' });
35+
36+
const result = await isNativeAvailable('/ext/path');
37+
assert.strictEqual(result, false);
38+
});
39+
40+
it('should return false when binary does not exist', async () => {
41+
Object.defineProperty(process, 'platform', { value: 'linux' });
42+
Object.defineProperty(process, 'arch', { value: 'x64' });
43+
44+
mock.method(fs.promises, 'access', async () => {
45+
throw new Error('ENOENT');
46+
});
47+
48+
const result = await isNativeAvailable('/ext/path');
49+
assert.strictEqual(result, false);
50+
});
51+
52+
it('should return true when binary exists on linux x64', async () => {
53+
Object.defineProperty(process, 'platform', { value: 'linux' });
54+
Object.defineProperty(process, 'arch', { value: 'x64' });
55+
56+
const accessMock = mock.method(fs.promises, 'access', async () => {});
57+
58+
const result = await isNativeAvailable('/ext/path');
59+
assert.strictEqual(result, true);
60+
61+
const expectedPath = path.join('/ext/path', 'natives', 'x86_64-linux-gnu', 'tjs');
62+
assert.strictEqual(accessMock.mock.calls[0].arguments[0], expectedPath);
63+
});
64+
65+
it('should return true when binary exists on linux arm64', async () => {
66+
Object.defineProperty(process, 'platform', { value: 'linux' });
67+
Object.defineProperty(process, 'arch', { value: 'arm64' });
68+
69+
const accessMock = mock.method(fs.promises, 'access', async () => {});
70+
71+
const result = await isNativeAvailable('/ext/path');
72+
assert.strictEqual(result, true);
73+
74+
const expectedPath = path.join('/ext/path', 'natives', 'aarch64-linux-gnu', 'tjs');
75+
assert.strictEqual(accessMock.mock.calls[0].arguments[0], expectedPath);
76+
});
77+
78+
it('should return true when binary exists on darwin arm64', async () => {
79+
Object.defineProperty(process, 'platform', { value: 'darwin' });
80+
Object.defineProperty(process, 'arch', { value: 'arm64' });
81+
82+
const accessMock = mock.method(fs.promises, 'access', async () => {});
83+
84+
const result = await isNativeAvailable('/ext/path');
85+
assert.strictEqual(result, true);
86+
87+
const expectedPath = path.join('/ext/path', 'natives', 'aarch64-macos', 'tjs');
88+
assert.strictEqual(accessMock.mock.calls[0].arguments[0], expectedPath);
89+
});
90+
91+
it('should return true when binary exists on win32', async () => {
92+
Object.defineProperty(process, 'platform', { value: 'win32' });
93+
Object.defineProperty(process, 'arch', { value: 'x64' });
94+
95+
const accessMock = mock.method(fs.promises, 'access', async () => {});
96+
97+
const result = await isNativeAvailable('/ext/path');
98+
assert.strictEqual(result, true);
99+
100+
const expectedPath = path.join('/ext/path', 'natives', 'x86_64-windows', 'tjs.exe');
101+
assert.strictEqual(accessMock.mock.calls[0].arguments[0], expectedPath);
102+
});
103+
});

0 commit comments

Comments
 (0)