44 */
55
66// This test runs in plain Node, not inside a real VS Code window.
7- // Set the minimum localization config early so extension imports do not crash.
8- process . env . VSCODE_NLS_CONFIG = JSON . stringify ( { locale : "en" } ) ;
9-
10- // When production code imports "vscode", return a tiny fake object instead.
11- // We only provide the pieces ProjectProvider touches during this test.
12- // eslint-disable-next-line @typescript-eslint/no-var-requires
13- const Module = require ( "module" ) ;
14- const originalLoad = Module . _load ;
15- Module . _load = function ( request : string , ...args : any [ ] ) {
16- if ( request === "vscode" ) {
17- return {
18- // ProjectProvider creates an EventEmitter in its constructor.
19- EventEmitter : class { event = ( ) => { } ; fire ( ) { } } ,
20- TreeItemCollapsibleState : { None : 0 } ,
21- TreeItem : class { constructor ( label : string ) { ( this as any ) . label = label ; } } ,
22- Uri : { file : ( p : string ) => ( { fsPath : p } ) } ,
23- // refresh() temporarily shows a status bar message and later disposes it.
24- window : {
25- setStatusBarMessage : ( ) => ( { dispose ( ) { } } )
26- }
27- } ;
28- }
29- return originalLoad . call ( this , request , ...args ) ;
30- } ;
7+ import { installFakeVscode } from "./fakeVscode" ;
8+ import * as sinon from "sinon" ;
319
10+ // Install the shared vscode fake — must happen before any extension imports.
11+ installFakeVscode ( ) ;
3212
3313import * as assert from "assert" ;
34- import * as sinon from "sinon" ;
35-
3614import { ProjectProvider } from "../../liberty/libertyProject" ;
3715import { DashboardData } from "../../liberty/dashboard" ;
3816
3917// Fake ExtensionContext for ProjectProvider.
40- // addUserSelectedPath() reads saved dashboard data from workspaceState and saves updates back to it .
18+ // addUserSelectedPath() reads saved dashboard data from workspaceState and saves updates back.
4119const fakeContext : any = {
4220 workspaceState : {
43- // Start with empty stored dashboard data.
4421 get : ( ) => new DashboardData ( [ ] , [ ] ) ,
45- // Stubbed so each test can check whether a save happened.
4622 update : sinon . stub ( )
4723 } ,
4824 globalState : {
4925 get : ( ) => undefined ,
50- update : sinon . stub ( ) ,
26+ update : sinon . stub ( )
5127 }
5228} ;
5329
@@ -56,112 +32,59 @@ describe("addUserSelectedPath", () => {
5632 let provider : ProjectProvider ;
5733
5834 before ( ( ) => {
59- // Create the real ProjectProvider instance that owns addUserSelectedPath().
6035 provider = new ProjectProvider ( fakeContext ) ;
6136 } ) ;
6237
6338 afterEach ( ( ) => {
64- // Reset Sinon-created stubs so one test's fake behavior does not leak into another.
6539 sinon . restore ( ) ;
6640 } ) ;
6741
6842 // ─── Test 1 ───────────────────────────────────────────────────────────────
69- // Scenario: the user manually picks a Maven project that is not already in the dashboard.
70- // The add should succeed, update the live project map, and save the change.
43+ // Scenario: the user manually picks a Maven project not already in the dashboard.
7144 it ( "returns 0 and adds project to map when valid pom.xml exists" , async ( ) => {
45+ ( provider as any ) . createLibertyProject = async ( ) => ( {
46+ getPath : ( ) => "/my/project/pom.xml" ,
47+ getLabel : ( ) => "my-app" ,
48+ getContextValue : ( ) => "libertyMavenProject"
49+ } ) ;
7250
73- // These two stubs model the successful Maven path inside createLibertyProject().
74- // existsSyncStub() means "pretend pom.xml exists".
75- const existsSyncStub = sinon . stub ( ) . returns ( true ) ;
76- // readFileStub() means "pretend reading pom.xml returned this XML".
77- const readFileStub = sinon . stub ( ) . resolves ( `<project><artifactId>my-app</artifactId></project>` ) ;
78-
79- // Replace ProjectProvider's internal createLibertyProject() helper for this test only.
80- // That keeps the test focused on addUserSelectedPath(): when project creation succeeds,
81- // does it add the project, save it, and return the success code?
82- ( provider as any ) . createLibertyProject = async ( ) => {
83- if ( existsSyncStub ( ) ) {
84- await readFileStub ( ) ;
85- // Return the smallest fake project object addUserSelectedPath() needs.
86- return {
87- getPath : ( ) => "/my/project/pom.xml" ,
88- getLabel : ( ) => "my-app" ,
89- getContextValue : ( ) => "libertyMavenProject"
90- } ;
91- }
92- // If project creation fails, the real helper would return undefined.
93- return undefined ;
94- } ;
95-
96- // This map represents the live set of projects currently shown in the Liberty dashboard.
97- // Start empty to model "project was not already in the dashboard".
9851 const existingProjects = new Map ( ) ;
99-
100- // Call the real method under test.
10152 const result = await provider . addUserSelectedPath ( "/my/project" , existingProjects ) ;
10253
103- // ASSERT
104- // 0 means the manual add succeeded.
10554 assert . equal ( result , 0 ) ;
106- // The project should now appear in the live dashboard map.
10755 assert . equal ( existingProjects . has ( "/my/project/pom.xml" ) , true ) ;
108- // The dashboard update should also be persisted to workspace storage.
10956 assert . equal ( fakeContext . workspaceState . update . called , true ) ;
11057 } ) ;
11158
11259 // ─── Test 2 ───────────────────────────────────────────────────────────────
11360 // Scenario: the user picks a folder that is already in the Liberty dashboard.
114- // addUserSelectedPath() should detect the duplicate and refuse to add it again.
11561 it ( "returns 1 and adds nothing when project already exists" , async ( ) => {
116-
117- // Clear any save calls left over from earlier tests.
11862 fakeContext . workspaceState . update . resetHistory ( ) ;
11963
120- // provider.getProjects() is the provider's internal "already in the dashboard" map.
121- // Put this path in the map first to simulate a project that Liberty Tools already knows about.
12264 const dashboardProjects = provider . getProjects ( ) ;
12365 dashboardProjects . set ( "/my/project/pom.xml" , { } as any ) ;
12466
125- // This separate map is the live map passed into addUserSelectedPath().
126- // It starts empty because this test is checking that nothing new gets added.
12767 const existingProjects = new Map ( ) ;
128-
129- // Try to manually add the same project again.
13068 const result = await provider . addUserSelectedPath ( "/my/project" , existingProjects ) ;
13169
132- // ASSERT
133- // 1 means "project already exists".
13470 assert . equal ( result , 1 ) ;
135-
136- // Because the project was a duplicate, addUserSelectedPath() should not add anything new.
13771 assert . equal ( existingProjects . size , 0 ) ;
138-
139- // Because nothing changed, it should not save anything to workspace storage.
14072 assert . equal ( fakeContext . workspaceState . update . called , false ) ;
14173
142- // Clean up the provider's internal dashboard map so other tests stay isolated.
14374 dashboardProjects . clear ( ) ;
14475 } ) ;
14576
14677 // ─── Test 3 ───────────────────────────────────────────────────────────────
147- // Scenario: the user picks a folder that is not a Maven or Gradle project.
148- // The add should fail without changing dashboard state.
78+ // Scenario: the user picks a folder with no pom.xml or build.gradle.
14979 it ( "returns 2 and adds nothing when no build file exists" , async ( ) => {
150-
151- // Clear any save calls recorded by earlier tests.
15280 fakeContext . workspaceState . update . resetHistory ( ) ;
153- // Force project creation to fail to model a folder with no pom.xml or build.gradle.
15481 ( provider as any ) . createLibertyProject = async ( ) => undefined ;
155-
82+
15683 const existingProjects = new Map ( ) ;
15784 const result = await provider . addUserSelectedPath ( "/my/project" , existingProjects ) ;
15885
159- // ASSERT
160- // 2 means the selected folder is not a Maven or Gradle project.
16186 assert . equal ( result , 2 ) ;
162- // Nothing should be added to the live dashboard map.
16387 assert . equal ( existingProjects . size , 0 ) ;
164- // Nothing should be saved because the add failed.
16588 assert . equal ( fakeContext . workspaceState . update . called , false ) ;
16689 } ) ;
16790
0 commit comments