mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-27 05:24:34 -07:00
fix(core): auto-correct file paths in smart edit where possible (#9526)
This commit is contained in:
@@ -242,6 +242,78 @@ describe('SmartEditTool', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('correctPath', () => {
|
||||
it('should correct a relative path if it is unambiguous', () => {
|
||||
const testFile = 'unique.txt';
|
||||
fs.writeFileSync(path.join(rootDir, testFile), 'content');
|
||||
|
||||
const params: EditToolParams = {
|
||||
file_path: testFile,
|
||||
instruction: 'An instruction',
|
||||
old_string: 'old',
|
||||
new_string: 'new',
|
||||
};
|
||||
|
||||
const validationResult = (tool as any).correctPath(params);
|
||||
|
||||
expect(validationResult).toBeNull();
|
||||
expect(params.file_path).toBe(path.join(rootDir, testFile));
|
||||
});
|
||||
|
||||
it('should correct a partial relative path if it is unambiguous', () => {
|
||||
const subDir = path.join(rootDir, 'sub');
|
||||
fs.mkdirSync(subDir);
|
||||
const testFile = 'file.txt';
|
||||
const partialPath = path.join('sub', testFile);
|
||||
const fullPath = path.join(subDir, testFile);
|
||||
fs.writeFileSync(fullPath, 'content');
|
||||
|
||||
const params: EditToolParams = {
|
||||
file_path: partialPath,
|
||||
instruction: 'An instruction',
|
||||
old_string: 'old',
|
||||
new_string: 'new',
|
||||
};
|
||||
|
||||
const validationResult = (tool as any).correctPath(params);
|
||||
|
||||
expect(validationResult).toBeNull();
|
||||
expect(params.file_path).toBe(fullPath);
|
||||
});
|
||||
|
||||
it('should return an error for a relative path that does not exist', () => {
|
||||
const params: EditToolParams = {
|
||||
file_path: 'test.txt',
|
||||
instruction: 'An instruction',
|
||||
old_string: 'old',
|
||||
new_string: 'new',
|
||||
};
|
||||
const result = (tool as any).correctPath(params);
|
||||
expect(result).toMatch(/File not found for 'test.txt'/);
|
||||
});
|
||||
|
||||
it('should return an error for an ambiguous path', () => {
|
||||
const subDir1 = path.join(rootDir, 'module1');
|
||||
const subDir2 = path.join(rootDir, 'module2');
|
||||
fs.mkdirSync(subDir1, { recursive: true });
|
||||
fs.mkdirSync(subDir2, { recursive: true });
|
||||
|
||||
const ambiguousFile = 'component.ts';
|
||||
fs.writeFileSync(path.join(subDir1, ambiguousFile), 'content 1');
|
||||
fs.writeFileSync(path.join(subDir2, ambiguousFile), 'content 2');
|
||||
|
||||
const params: EditToolParams = {
|
||||
file_path: ambiguousFile,
|
||||
instruction: 'An instruction',
|
||||
old_string: 'old',
|
||||
new_string: 'new',
|
||||
};
|
||||
|
||||
const validationResult = (tool as any).correctPath(params);
|
||||
expect(validationResult).toMatch(/ambiguous and matches multiple files/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateToolParams', () => {
|
||||
it('should return null for valid params', () => {
|
||||
const params: EditToolParams = {
|
||||
@@ -253,15 +325,15 @@ describe('SmartEditTool', () => {
|
||||
expect(tool.validateToolParams(params)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return error for relative path', () => {
|
||||
it('should return an error if path is outside the workspace', () => {
|
||||
const params: EditToolParams = {
|
||||
file_path: 'test.txt',
|
||||
file_path: path.join(os.tmpdir(), 'outside.txt'),
|
||||
instruction: 'An instruction',
|
||||
old_string: 'old',
|
||||
new_string: 'new',
|
||||
};
|
||||
expect(tool.validateToolParams(params)).toMatch(
|
||||
/File path must be absolute/,
|
||||
/must be within one of the workspace directories/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user