mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
Read and write files through Zed (#6169)
Co-authored-by: Agus Zubiaga <agus@zed.dev>
This commit is contained in:
59
packages/core/src/services/fileSystemService.test.ts
Normal file
59
packages/core/src/services/fileSystemService.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import fs from 'fs/promises';
|
||||
import { StandardFileSystemService } from './fileSystemService.js';
|
||||
|
||||
vi.mock('fs/promises');
|
||||
|
||||
describe('StandardFileSystemService', () => {
|
||||
let fileSystem: StandardFileSystemService;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
fileSystem = new StandardFileSystemService();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('readTextFile', () => {
|
||||
it('should read file content using fs', async () => {
|
||||
const testContent = 'Hello, World!';
|
||||
vi.mocked(fs.readFile).mockResolvedValue(testContent);
|
||||
|
||||
const result = await fileSystem.readTextFile('/test/file.txt');
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalledWith('/test/file.txt', 'utf-8');
|
||||
expect(result).toBe(testContent);
|
||||
});
|
||||
|
||||
it('should propagate fs.readFile errors', async () => {
|
||||
const error = new Error('ENOENT: File not found');
|
||||
vi.mocked(fs.readFile).mockRejectedValue(error);
|
||||
|
||||
await expect(fileSystem.readTextFile('/test/file.txt')).rejects.toThrow(
|
||||
'ENOENT: File not found',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeTextFile', () => {
|
||||
it('should write file content using fs', async () => {
|
||||
vi.mocked(fs.writeFile).mockResolvedValue();
|
||||
|
||||
await fileSystem.writeTextFile('/test/file.txt', 'Hello, World!');
|
||||
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
'/test/file.txt',
|
||||
'Hello, World!',
|
||||
'utf-8',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
41
packages/core/src/services/fileSystemService.ts
Normal file
41
packages/core/src/services/fileSystemService.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import fs from 'fs/promises';
|
||||
|
||||
/**
|
||||
* Interface for file system operations that may be delegated to different implementations
|
||||
*/
|
||||
export interface FileSystemService {
|
||||
/**
|
||||
* Read text content from a file
|
||||
*
|
||||
* @param filePath - The path to the file to read
|
||||
* @returns The file content as a string
|
||||
*/
|
||||
readTextFile(filePath: string): Promise<string>;
|
||||
|
||||
/**
|
||||
* Write text content to a file
|
||||
*
|
||||
* @param filePath - The path to the file to write
|
||||
* @param content - The content to write
|
||||
*/
|
||||
writeTextFile(filePath: string, content: string): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard file system implementation
|
||||
*/
|
||||
export class StandardFileSystemService implements FileSystemService {
|
||||
async readTextFile(filePath: string): Promise<string> {
|
||||
return fs.readFile(filePath, 'utf-8');
|
||||
}
|
||||
|
||||
async writeTextFile(filePath: string, content: string): Promise<void> {
|
||||
await fs.writeFile(filePath, content, 'utf-8');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user