Skip to content

Commit f121932

Browse files
committed
refactor: Eliminate code duplication in test helpers
Removed duplicate createTempDocument() and deleteTempDocument() functions from comparison test adapters by importing from the shared test-helpers module. Before: - createTempDocument() defined 3 times (identical code) - deleteTempDocument() defined 3 times (identical code) - 66 lines of duplicated code After: - Both functions imported from src/test/test-helpers.ts - Single source of truth for test document management - 66 lines removed, DRY principle applied All tests still passing with zero listener leak warnings: - Main Extension: 226/226 passing - Comparison Tests: 144/144 passing Thanks to user for spotting this code smell!
1 parent a014958 commit f121932

2 files changed

Lines changed: 4 additions & 66 deletions

File tree

comparison-test-harness/new-extension/adapter.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,11 @@
1010
* - Reference real code via relative paths (no copying!)
1111
*/
1212

13-
import { Uri, TextEdit, TextDocument, OutputChannel, workspace, WorkspaceEdit, commands } from 'vscode';
13+
import { Uri, TextEdit, TextDocument, OutputChannel, workspace, WorkspaceEdit } from 'vscode';
1414
import { ImportManager } from '../../src/imports/import-manager';
1515
import { ImportsConfig } from '../../src/configuration';
1616
import { ImportGroup, ImportGroupSettingParser, RemainImportGroup } from '../../src/imports/import-grouping';
17-
import * as fs from 'fs';
18-
import * as path from 'path';
19-
import * as os from 'os';
20-
21-
/**
22-
* Create a REAL temporary file and open it as a TextDocument
23-
* This allows us to use workspace.applyEdit() which requires real files
24-
*/
25-
async function createTempDocument(content: string): Promise<TextDocument> {
26-
const tempDir = os.tmpdir();
27-
const tempFile = path.join(tempDir, `test-${Date.now()}-${Math.random()}.ts`);
28-
fs.writeFileSync(tempFile, content, 'utf-8');
29-
30-
const doc = await workspace.openTextDocument(Uri.file(tempFile));
31-
return doc;
32-
}
33-
34-
/**
35-
* Clean up temporary file after test completes
36-
* Closes the document in VSCode AND deletes the file to prevent listener leaks
37-
*/
38-
async function deleteTempDocument(doc: TextDocument): Promise<void> {
39-
try {
40-
// Close the document in VSCode to release listeners
41-
await commands.executeCommand('workbench.action.closeAllEditors');
42-
43-
// Delete the physical file
44-
fs.unlinkSync(doc.uri.fsPath);
45-
} catch (e) {
46-
// Ignore errors - best effort cleanup
47-
}
48-
}
17+
import { createTempDocument, deleteTempDocument } from '../../src/test/test-helpers';
4918

5019
/**
5120
* Mock OutputChannel (same as in existing tests)

comparison-test-harness/old-extension/adapter.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,15 @@
1212

1313
import 'reflect-metadata';
1414
import { TypescriptParser, TypescriptCodeGenerator, File, Generatable, GENERATORS, TypescriptGenerationOptions, MultiLineImportRule } from 'typescript-parser';
15-
import { Uri, TextDocument, TextEdit, window, TextEditor, workspace, WorkspaceEdit, commands } from 'vscode';
15+
import { Uri, TextDocument, TextEdit, window, TextEditor, workspace, WorkspaceEdit } from 'vscode';
1616
import { ImportManager } from '../old-typescript-hero/src/imports/import-manager';
1717
import { Configuration } from '../old-typescript-hero/src/configuration';
1818
import { ImportsConfig } from '../old-typescript-hero/src/configuration/imports-config';
1919
import { TypescriptCodeGeneratorFactory } from '../old-typescript-hero/src/ioc-symbols';
2020
import { getScriptKind } from '../old-typescript-hero/src/utilities/utility-functions';
2121
import { ImportGroupSettingParser } from '../old-typescript-hero/src/imports/import-grouping/import-group-setting-parser';
2222
import { ImportGroup, KeywordImportGroup, RegexImportGroup, RemainImportGroup } from '../old-typescript-hero/src/imports/import-grouping';
23-
import * as fs from 'fs';
24-
import * as path from 'path';
25-
import * as os from 'os';
26-
27-
/**
28-
* Create a REAL temporary file and open it as a TextDocument
29-
* This allows us to use workspace.applyEdit() which requires real files
30-
*/
31-
async function createTempDocument(content: string): Promise<TextDocument> {
32-
const tempDir = os.tmpdir();
33-
const tempFile = path.join(tempDir, `test-${Date.now()}-${Math.random()}.ts`);
34-
fs.writeFileSync(tempFile, content, 'utf-8');
35-
36-
const doc = await workspace.openTextDocument(Uri.file(tempFile));
37-
return doc;
38-
}
39-
40-
/**
41-
* Clean up temporary file after test completes
42-
* Closes the document in VSCode AND deletes the file to prevent listener leaks
43-
*/
44-
async function deleteTempDocument(doc: TextDocument): Promise<void> {
45-
try {
46-
// Close the document in VSCode to release listeners
47-
await commands.executeCommand('workbench.action.closeAllEditors');
48-
49-
// Delete the physical file
50-
fs.unlinkSync(doc.uri.fsPath);
51-
} catch (e) {
52-
// Ignore errors - best effort cleanup
53-
}
54-
}
23+
import { createTempDocument, deleteTempDocument } from '../../src/test/test-helpers';
5524

5625
/**
5726
* Mock Logger for old extension

0 commit comments

Comments
 (0)