Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import * as assert from 'assert';
import * as path from 'path';
import { loadNativeModule } from './utils';

const nativeName = process.platform === 'win32' ? 'conpty' : 'pty';

describe('utils', () => {
describe('loadNativeModule', () => {
afterEach(() => {
delete process.env.NODE_PTY_PREBUILT_PATH;
});

it('should load from NODE_PTY_PREBUILT_PATH when set to a valid path', () => {
const validPath = path.resolve(__dirname, '..', 'build', 'Release');
process.env.NODE_PTY_PREBUILT_PATH = validPath;
const result = loadNativeModule(nativeName);
assert.strictEqual(result.dir, validPath);
assert.ok(result.module);
});

it('should fall through to default paths when NODE_PTY_PREBUILT_PATH is invalid', () => {
process.env.NODE_PTY_PREBUILT_PATH = '/nonexistent/path';
const result = loadNativeModule(nativeName);
assert.ok(result.dir !== '/nonexistent/path');
assert.ok(result.module);
});

it('should load normally when NODE_PTY_PREBUILT_PATH is not set', () => {
delete process.env.NODE_PTY_PREBUILT_PATH;
const result = loadNativeModule(nativeName);
assert.ok(result.dir);
assert.ok(result.module);
});

it('should include NODE_PTY_PREBUILT_PATH in error message when all paths fail', () => {
process.env.NODE_PTY_PREBUILT_PATH = '/nonexistent/path';
assert.throws(
() => loadNativeModule('does-not-exist'),
(err: Error) => err.message.includes('/nonexistent/path')
);
});
});
});
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export function assign(target: any, ...sources: any[]): any {


export function loadNativeModule(name: string): {dir: string, module: any} {
// Allow overriding the prebuilt path via environment variable
const envPath = process.env.NODE_PTY_PREBUILT_PATH;
if (envPath) {
try {
return { dir: envPath, module: require(`${envPath}/${name}.node`) };
} catch (e) {
// Fall through to default paths
}
}

// Check build, debug, and then prebuilds.
const dirs = ['build/Release', 'build/Debug', `prebuilds/${process.platform}-${process.arch}`];
// Check relative to the parent dir for unbundled and then the current dir for bundled
Expand All @@ -25,5 +35,5 @@ export function loadNativeModule(name: string): {dir: string, module: any} {
}
}
}
throw new Error(`Failed to load native module: ${name}.node, checked: ${dirs.join(', ')}: ${lastError}`);
throw new Error(`Failed to load native module: ${name}.node, checked: ${envPath ? envPath + ', ' : ''}${dirs.join(', ')}: ${lastError}`);
}
Loading