Skip to content

Commit 8a65fee

Browse files
committed
fix tests
1 parent c9d5b86 commit 8a65fee

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/features/projectManager.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
2525
private readonly _onDidChangeProjects = new EventEmitter<ProjectArray | undefined>();
2626
public readonly onDidChangeProjects = this._onDidChangeProjects.event;
2727

28+
/**
29+
* Fires the onDidChangeProjects event if updateBool is true.
30+
*/
31+
private fireDidChangeProjects(updateBool: boolean, projects: ProjectArray) {
32+
if (updateBool) {
33+
this._onDidChangeProjects.fire(projects);
34+
}
35+
}
36+
2837
// Debounce the updateProjects method to avoid excessive update calls
2938
private readonly updateDebounce = createSimpleDebounce(100, () => this.updateProjects());
3039

3140
initialize(): void {
32-
this.add(this.getInitialProjects());
41+
this.add(this.getInitialProjects(), true);
3342
this.disposables.push(
3443
this._onDidChangeProjects,
3544
new Disposable(() => this._projects.clear()),
@@ -95,7 +104,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
95104
* then updates the internal _projects map to reflect the current state and
96105
* fires the onDidChangeProjects event if there are any changes.
97106
*/
98-
private updateProjects(): void {
107+
private updateProjects(updateBool: boolean = true): void {
99108
const newProjects: ProjectArray = this.getInitialProjects();
100109
const existingProjects = Array.from(this._projects.values());
101110

@@ -112,7 +121,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
112121
projectsToAdd.forEach((w) => this._projects.set(w.uri.toString(), w));
113122

114123
if (projectsToRemove.length > 0 || projectsToAdd.length > 0) {
115-
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
124+
this.fireDidChangeProjects(updateBool, Array.from(this._projects.values()));
116125
}
117126
}
118127

@@ -124,7 +133,7 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
124133
return new PythonProjectsImpl(name, uri, options);
125134
}
126135

127-
async add(projects: PythonProject | ProjectArray): Promise<void> {
136+
async add(projects: PythonProject | ProjectArray, updateBool: boolean = true): Promise<void> {
128137
const _projects = Array.isArray(projects) ? projects : [projects];
129138
if (_projects.length === 0) {
130139
return;
@@ -153,21 +162,21 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
153162
// handles adding the project to this._projects map
154163
return this._projects.set(currProject.uri.toString(), currProject);
155164
});
156-
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
165+
this.fireDidChangeProjects(updateBool, Array.from(this._projects.values()));
157166

158167
if (edits.length > 0) {
159168
await addPythonProjectSetting(edits);
160169
}
161170
}
162171

163-
remove(projects: PythonProject | ProjectArray): void {
172+
remove(projects: PythonProject | ProjectArray, updateBool: boolean = true): void {
164173
const _projects = Array.isArray(projects) ? projects : [projects];
165174
if (_projects.length === 0) {
166175
return;
167176
}
168177

169178
_projects.forEach((w) => this._projects.delete(w.uri.toString()));
170-
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
179+
this.fireDidChangeProjects(updateBool, Array.from(this._projects.values()));
171180
}
172181

173182
/**
@@ -188,8 +197,8 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
188197
return;
189198
}
190199

191-
// Remove the old project
192-
this.remove(project);
200+
// Remove the old project, do not fire event yet
201+
this.remove(project, false);
193202

194203
// Prepare new values
195204
const name = newName ?? project.name;
@@ -200,9 +209,12 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
200209
iconPath: newOptions?.iconPath ?? (project as PythonProjectsImpl).iconPath,
201210
};
202211

203-
// Create and add the new project
212+
// Create and add the new project, do not fire event yet
204213
const updatedProject = this.create(name, uri, options);
205-
await this.add(updatedProject);
214+
await this.add(updatedProject, false);
215+
216+
// Now trigger the update event only once
217+
this.updateDebounce.trigger();
206218
}
207219

208220
getProjects(uris?: Uri[]): ReadonlyArray<PythonProject> {

src/test/features/projectManager.updateUri.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ suite('Project Manager Update URI tests', () => {
1414
projectManager.dispose();
1515
});
1616

17-
test('updateProjectUri should update existing project URI', () => {
17+
test('updateProjectUri should update existing project URI', async () => {
1818
const oldUri = Uri.file('/path/to/old/project');
1919
const newUri = Uri.file('/path/to/new/project');
2020

@@ -36,7 +36,7 @@ suite('Project Manager Update URI tests', () => {
3636
assert.equal(oldProject.uri.fsPath, oldUri.fsPath, 'Old URI should match');
3737

3838
// Update the project URI
39-
projectManager.modifyProject(oldUri, 'project', newUri);
39+
await projectManager.modifyProject(oldUri, 'project', newUri);
4040

4141
// Verify project no longer exists with old URI
4242
const oldProjectAfterUpdate = projectManager.get(oldUri);
@@ -57,8 +57,8 @@ suite('Project Manager Update URI tests', () => {
5757

5858
// Try to update a project that doesn't exist
5959
// This should not throw an error
60-
assert.doesNotThrow(() => {
61-
projectManager.modifyProject(oldUri, 'project', newUri);
60+
assert.doesNotThrow(async () => {
61+
await projectManager.modifyProject(oldUri, 'project', newUri);
6262
}, 'Should handle non-existent project gracefully');
6363

6464
// Verify no project was created

0 commit comments

Comments
 (0)