|
|
@@ -1,15 +1,5 @@
|
|
|
-import { beforeEach, describe, expect, mock, test } from 'bun:test';
|
|
|
-
|
|
|
-// Mock fs BEFORE importing modules
|
|
|
-mock.module('fs', () => ({
|
|
|
- readFileSync: mock(() => ''),
|
|
|
- writeFileSync: mock(),
|
|
|
- unlinkSync: mock(),
|
|
|
- existsSync: mock(() => true),
|
|
|
- statSync: mock(() => ({ isDirectory: () => false })),
|
|
|
-}));
|
|
|
-
|
|
|
-import { readFileSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
|
+import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from 'bun:test';
|
|
|
+import * as fs from 'node:fs';
|
|
|
import {
|
|
|
applyWorkspaceEdit,
|
|
|
filterDiagnosticsBySeverity,
|
|
|
@@ -22,9 +12,13 @@ import {
|
|
|
|
|
|
describe('utils', () => {
|
|
|
beforeEach(() => {
|
|
|
- (readFileSync as any).mockClear();
|
|
|
- (writeFileSync as any).mockClear();
|
|
|
- (unlinkSync as any).mockClear();
|
|
|
+ spyOn(fs, 'readFileSync').mockImplementation((() => '') as any);
|
|
|
+ spyOn(fs, 'writeFileSync').mockImplementation(() => undefined);
|
|
|
+ spyOn(fs, 'unlinkSync').mockImplementation(() => undefined);
|
|
|
+ });
|
|
|
+
|
|
|
+ afterEach(() => {
|
|
|
+ mock.restore();
|
|
|
});
|
|
|
|
|
|
describe('uriToPath', () => {
|
|
|
@@ -96,7 +90,7 @@ describe('utils', () => {
|
|
|
test('should apply single file edit', () => {
|
|
|
const uri = 'file:///test.ts';
|
|
|
const filePath = uriToPath(uri);
|
|
|
- (readFileSync as any).mockReturnValue('line1\nline2\nline3');
|
|
|
+ (fs.readFileSync as any).mockReturnValue('line1\nline2\nline3');
|
|
|
|
|
|
const edit = {
|
|
|
changes: {
|
|
|
@@ -115,12 +109,12 @@ describe('utils', () => {
|
|
|
const result = applyWorkspaceEdit(edit as any);
|
|
|
expect(result.success).toBe(true);
|
|
|
expect(result.filesModified).toContain(filePath);
|
|
|
- expect(writeFileSync).toHaveBeenCalled();
|
|
|
+ expect(fs.writeFileSync).toHaveBeenCalled();
|
|
|
});
|
|
|
|
|
|
test('should handle overlapping edits by sorting them in reverse order', () => {
|
|
|
const uri = 'file:///test.ts';
|
|
|
- (readFileSync as any).mockReturnValue('abcde');
|
|
|
+ (fs.readFileSync as any).mockReturnValue('abcde');
|
|
|
|
|
|
const edit = {
|
|
|
changes: {
|
|
|
@@ -145,7 +139,7 @@ describe('utils', () => {
|
|
|
|
|
|
const result = applyWorkspaceEdit(edit as any);
|
|
|
expect(result.success).toBe(true);
|
|
|
- const writtenContent = (writeFileSync as any).mock.calls[0][1];
|
|
|
+ const writtenContent = (fs.writeFileSync as any).mock.calls[0][1];
|
|
|
expect(writtenContent).toBe('1b3de');
|
|
|
});
|
|
|
|
|
|
@@ -156,7 +150,7 @@ describe('utils', () => {
|
|
|
|
|
|
const result = applyWorkspaceEdit(edit as any);
|
|
|
expect(result.success).toBe(true);
|
|
|
- expect(writeFileSync).toHaveBeenCalledWith(
|
|
|
+ expect(fs.writeFileSync).toHaveBeenCalledWith(
|
|
|
uriToPath('file:///new.ts'),
|
|
|
'',
|
|
|
'utf-8',
|
|
|
@@ -166,7 +160,7 @@ describe('utils', () => {
|
|
|
test('should handle rename file operation', () => {
|
|
|
const oldUri = 'file:///old.ts';
|
|
|
const newUri = 'file:///new.ts';
|
|
|
- (readFileSync as any).mockReturnValue('some content');
|
|
|
+ (fs.readFileSync as any).mockReturnValue('some content');
|
|
|
|
|
|
const edit = {
|
|
|
documentChanges: [{ kind: 'rename', oldUri, newUri }],
|
|
|
@@ -174,12 +168,12 @@ describe('utils', () => {
|
|
|
|
|
|
const result = applyWorkspaceEdit(edit as any);
|
|
|
expect(result.success).toBe(true);
|
|
|
- expect(writeFileSync).toHaveBeenCalledWith(
|
|
|
+ expect(fs.writeFileSync).toHaveBeenCalledWith(
|
|
|
uriToPath(newUri),
|
|
|
'some content',
|
|
|
'utf-8',
|
|
|
);
|
|
|
- expect(unlinkSync).toHaveBeenCalledWith(uriToPath(oldUri));
|
|
|
+ expect(fs.unlinkSync).toHaveBeenCalledWith(uriToPath(oldUri));
|
|
|
});
|
|
|
|
|
|
test('should handle delete file operation', () => {
|
|
|
@@ -190,7 +184,7 @@ describe('utils', () => {
|
|
|
|
|
|
const result = applyWorkspaceEdit(edit as any);
|
|
|
expect(result.success).toBe(true);
|
|
|
- expect(unlinkSync).toHaveBeenCalledWith(uriToPath(uri));
|
|
|
+ expect(fs.unlinkSync).toHaveBeenCalledWith(uriToPath(uri));
|
|
|
});
|
|
|
|
|
|
test('should return error if no edit provided', () => {
|