refactor to simplify approach without calling wslpath to fix the wsl paths.

This commit is contained in:
Steve Rhoades 2024-10-03 23:59:08 -07:00
parent 5ae106537d
commit 0a65ead04c
3 changed files with 2 additions and 115 deletions

View file

@ -431,42 +431,7 @@ class VsCodeIde implements IDE {
}
async getOpenFiles(): Promise<string[]> {
const openFiles = await this.ideUtils.getOpenFiles();
return this.convertWslPathsIfNeeded(openFiles);
}
private async convertWslPathsIfNeeded(paths: string[]): Promise<string[]> {
if (!this.isWsl()) {
return paths;
}
const convertedPaths = await Promise.all(paths.map(async (path) => {
if (path.startsWith('/mnt/')) {
try {
const [stdout, stderr] = await this.subprocess(`wslpath -w "${path}"`);
if (stderr) {
console.error(`Error converting WSL path: ${stderr}`);
return path;
}
return stdout.trim();
} catch (error) {
console.error(`Error executing wslpath command: ${error}`);
return path;
}
}
return path;
}));
return convertedPaths;
}
/**
* Tests the remote name of the vscode.env.remoteName to see if it starts with 'wsl'
*
* @returns boolean
*/
private isWsl(): boolean {
return !!vscode.env.remoteName?.toLowerCase().startsWith('wsl');
return await this.ideUtils.getOpenFiles();
}
async getCurrentFile(): Promise<string | undefined> {

View file

@ -1,78 +0,0 @@
import { describe, test, beforeEach, afterEach } from "mocha";
import assert from "node:assert";
import sinon from "sinon";
import { VsCodeIde } from '../../VsCodeIde';
// Mock the vscode module
const mockVscode = {
Uri: {
file: (path: string) => ({ fsPath: path })
},
workspace: {
fs: {
readDirectory: sinon.stub().resolves([])
}
}
};
// Replace the real vscode module with our mock
import * as vscode from 'vscode';
(global as any).vscode = mockVscode;
describe('VsCodeIde', () => {
let ide: VsCodeIde;
let mockIdeUtils: any;
beforeEach(() => {
mockIdeUtils = {
getOpenFiles: sinon.stub().resolves([])
};
ide = new VsCodeIde(null as any, Promise.resolve(null as any));
(ide as any).ideUtils = mockIdeUtils;
(ide as any).subprocess = sinon.stub().resolves(['', '']);
});
afterEach(() => {
sinon.restore();
});
describe('getOpenFiles', () => {
test('should return open files without conversion when not in WSL', async () => {
sinon.stub(ide as any, 'isWsl').returns(false);
const mockFiles = ['/path/to/file1', '/path/to/file2'];
mockIdeUtils.getOpenFiles.resolves(mockFiles);
const result = await ide.getOpenFiles();
assert.deepStrictEqual(result, mockFiles);
});
test('should convert WSL paths when in WSL environment', async () => {
sinon.stub(ide as any, 'isWsl').returns(true);
const mockFiles = ['/mnt/c/path/to/file1', '/path/to/file2'];
mockIdeUtils.getOpenFiles.resolves(mockFiles);
(ide as any).subprocess.withArgs('wslpath -w "/mnt/c/path/to/file1"').resolves(['C:\\path\\to\\file1', '']);
const result = await ide.getOpenFiles();
assert.deepStrictEqual(result, ['C:\\path\\to\\file1', '/path/to/file2']);
});
test('should handle errors in WSL path conversion', async () => {
sinon.stub(ide as any, 'isWsl').returns(true);
const mockFiles = ['/mnt/c/path/to/file1', '/path/to/file2'];
mockIdeUtils.getOpenFiles.resolves(mockFiles);
(ide as any).subprocess.throws(new Error('Command failed'));
const result = await ide.getOpenFiles();
assert.deepStrictEqual(result, mockFiles);
});
});
});

View file

@ -225,7 +225,7 @@ export class VsCodeIdeUtils {
.flat()
.filter(Boolean) // filter out undefined values
.filter((uri) => this.documentIsCode(uri)) // Filter out undesired documents
.map((uri) => uri.fsPath);
.map((uri) => uriFromFilePath(uri.fsPath).fsPath);
}
getVisibleFiles(): string[] {