@@ -2,7 +2,7 @@ const path = require('path');
22const fs = require ( 'fs' ) ;
33const os = require ( 'os' ) ;
44const yaml = require ( 'js-yaml' ) ;
5- const { reorderWorkspaceCollections, renameWorkspace } = require ( '../../src/utils/workspace-config' ) ;
5+ const { reorderWorkspaceCollections } = require ( '../../src/utils/workspace-config' ) ;
66
77const collection = ( name , pathSegment ) => ( { name, path : pathSegment } ) ;
88
@@ -74,136 +74,3 @@ describe('reorderWorkspaceCollections', () => {
7474 expect ( getCollectionPathsFromYml ( ) ) . toEqual ( [ 'collections/api' , 'collections/backend' ] ) ;
7575 } ) ;
7676} ) ;
77-
78- describe ( 'renameWorkspace' , ( ) => {
79- let parentDir ;
80- let workspacePath ;
81-
82- /** Creates a workspace directory with workspace.yml */
83- const createWorkspace = ( folderName , workspaceName ) => {
84- const wsPath = path . join ( parentDir , folderName ) ;
85- fs . mkdirSync ( wsPath , { recursive : true } ) ;
86- const content = [
87- 'opencollection: 1.0.0' ,
88- 'info:' ,
89- ` name: "${ workspaceName } "` ,
90- ' type: workspace' ,
91- 'collections:' ,
92- 'specs:' ,
93- 'docs: \'\''
94- ] . join ( '\n' ) ;
95- fs . writeFileSync ( path . join ( wsPath , 'workspace.yml' ) , content ) ;
96- return wsPath ;
97- } ;
98-
99- /** Gets the workspace name from workspace.yml */
100- const getWorkspaceName = ( wsPath ) => {
101- const raw = fs . readFileSync ( path . join ( wsPath , 'workspace.yml' ) , 'utf8' ) ;
102- const config = yaml . load ( raw ) ;
103- return config . info ?. name ;
104- } ;
105-
106- beforeEach ( ( ) => {
107- parentDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'bruno-ws-parent-' ) ) ;
108- workspacePath = createWorkspace ( 'Untitled Workspace' , 'Untitled Workspace' ) ;
109- } ) ;
110-
111- afterEach ( ( ) => {
112- fs . rmSync ( parentDir , { recursive : true , force : true } ) ;
113- } ) ;
114-
115- test ( 'renames workspace folder and updates config' , async ( ) => {
116- const result = await renameWorkspace ( workspacePath , 'My API Project' ) ;
117-
118- expect ( result . newWorkspacePath ) . toBe ( path . join ( parentDir , 'My API Project' ) ) ;
119- expect ( fs . existsSync ( path . join ( parentDir , 'Untitled Workspace' ) ) ) . toBe ( false ) ;
120- expect ( fs . existsSync ( path . join ( parentDir , 'My API Project' ) ) ) . toBe ( true ) ;
121- expect ( getWorkspaceName ( result . newWorkspacePath ) ) . toBe ( 'My API Project' ) ;
122- } ) ;
123-
124- test ( 'only updates config when folder name stays the same' , async ( ) => {
125- // Create workspace where folder name matches sanitized target but display name differs
126- const sameFolderPath = createWorkspace ( 'My-Project' , 'Old Name' ) ;
127-
128- // Rename to name that sanitizes to same folder name
129- const result = await renameWorkspace ( sameFolderPath , 'My:Project' ) ;
130-
131- expect ( result . newWorkspacePath ) . toBeNull ( ) ;
132- expect ( fs . existsSync ( sameFolderPath ) ) . toBe ( true ) ;
133- // Verify config was actually updated
134- expect ( getWorkspaceName ( sameFolderPath ) ) . toBe ( 'My:Project' ) ;
135- } ) ;
136-
137- test ( 'sanitizes special characters in folder name' , async ( ) => {
138- const result = await renameWorkspace ( workspacePath , 'My:API/Project<Test>' ) ;
139-
140- expect ( result . newWorkspacePath ) . toBe ( path . join ( parentDir , 'My-API-Project-Test-' ) ) ;
141- expect ( fs . existsSync ( result . newWorkspacePath ) ) . toBe ( true ) ;
142- expect ( getWorkspaceName ( result . newWorkspacePath ) ) . toBe ( 'My:API/Project<Test>' ) ;
143- } ) ;
144-
145- test ( 'throws error when target folder already exists' , async ( ) => {
146- // Create another workspace with the target name
147- createWorkspace ( 'Existing Project' , 'Existing Project' ) ;
148-
149- await expect ( renameWorkspace ( workspacePath , 'Existing Project' ) )
150- . rejects . toThrow ( 'A folder named "Existing Project" already exists at this location' ) ;
151-
152- // Original workspace should still exist
153- expect ( fs . existsSync ( workspacePath ) ) . toBe ( true ) ;
154- } ) ;
155-
156- test ( 'handles case-only rename by updating config without renaming folder' , async ( ) => {
157- // Create workspace with lowercase name
158- const lowerPath = createWorkspace ( 'myworkspace' , 'myworkspace' ) ;
159-
160- // Rename to different case - code uses case-insensitive comparison
161- // so this only updates config, doesn't rename folder (cross-platform safety)
162- const result = await renameWorkspace ( lowerPath , 'MyWorkspace' ) ;
163-
164- expect ( result . newWorkspacePath ) . toBeNull ( ) ;
165- expect ( fs . existsSync ( lowerPath ) ) . toBe ( true ) ;
166- expect ( getWorkspaceName ( lowerPath ) ) . toBe ( 'MyWorkspace' ) ;
167- } ) ;
168-
169- test ( 'preserves workspace.yml content after rename' , async ( ) => {
170- // Add collections, specs, and other fields to the workspace
171- const configPath = path . join ( workspacePath , 'workspace.yml' ) ;
172- const content = [
173- 'opencollection: 1.0.0' ,
174- 'info:' ,
175- ' name: "Untitled Workspace"' ,
176- ' type: workspace' ,
177- 'collections:' ,
178- ' - name: "API"' ,
179- ' path: "collections/api"' ,
180- ' remote: "https://github.com/example/api"' ,
181- 'specs:' ,
182- ' - name: "OpenAPI"' ,
183- ' path: "specs/openapi.yaml"' ,
184- 'docs: \'Some documentation\'' ,
185- '' ,
186- 'activeEnvironmentUid: env_123'
187- ] . join ( '\n' ) ;
188- fs . writeFileSync ( configPath , content ) ;
189-
190- const result = await renameWorkspace ( workspacePath , 'My Project' ) ;
191-
192- const newConfigPath = path . join ( result . newWorkspacePath , 'workspace.yml' ) ;
193- const newContent = fs . readFileSync ( newConfigPath , 'utf8' ) ;
194- const config = yaml . load ( newContent ) ;
195-
196- // Verify all fields are preserved
197- expect ( config . opencollection ) . toBe ( '1.0.0' ) ;
198- expect ( config . info . name ) . toBe ( 'My Project' ) ;
199- expect ( config . info . type ) . toBe ( 'workspace' ) ;
200- expect ( config . collections ) . toHaveLength ( 1 ) ;
201- expect ( config . collections [ 0 ] . name ) . toBe ( 'API' ) ;
202- expect ( config . collections [ 0 ] . path ) . toBe ( 'collections/api' ) ;
203- expect ( config . collections [ 0 ] . remote ) . toBe ( 'https://github.com/example/api' ) ;
204- expect ( config . specs ) . toHaveLength ( 1 ) ;
205- expect ( config . specs [ 0 ] ) . toEqual ( { name : 'OpenAPI' , path : 'specs/openapi.yaml' } ) ;
206- expect ( config . docs ) . toBe ( 'Some documentation' ) ;
207- expect ( config . activeEnvironmentUid ) . toBe ( 'env_123' ) ;
208- } ) ;
209- } ) ;
0 commit comments