Skip to content

Commit 626d925

Browse files
authored
Merge pull request #242 from tpyle/bug/handle-collection-names-with-slashes
Corrects issue when collection names in imports have slashes
2 parents 516411b + 2c0ccf7 commit 626d925

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

Diff for: .github/workflows/unit-tests.yml

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
name: Unit Tests
22
on:
33
push:
4-
branches: [ main ]
4+
branches: [main]
55
pull_request:
6-
branches: [ main ]
6+
branches: [main]
77
jobs:
88
test:
99
timeout-minutes: 60
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v3
13-
- uses: actions/setup-node@v3
14-
with:
15-
node-version: 16
16-
- name: Install dependencies
17-
run: npm i --legacy-peer-deps
18-
- name: Test Package bruno-query
19-
run: npm run test --workspace=packages/bruno-query
20-
- name: Build Package bruno-query
21-
run: npm run build --workspace=packages/bruno-query
22-
- name: Test Package bruno-lang
23-
run: npm run test --workspace=packages/bruno-lang
24-
- name: Test Package bruno-schema
25-
run: npm run test --workspace=packages/bruno-schema
26-
- name: Test Package bruno-app
27-
run: npm run test --workspace=packages/bruno-app
28-
- name: Test Package bruno-js
29-
run: npm run test --workspace=packages/bruno-js
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-node@v3
14+
with:
15+
node-version: 16
16+
- name: Install dependencies
17+
run: npm i --legacy-peer-deps
18+
- name: Test Package bruno-query
19+
run: npm run test --workspace=packages/bruno-query
20+
- name: Build Package bruno-query
21+
run: npm run build --workspace=packages/bruno-query
22+
- name: Test Package bruno-lang
23+
run: npm run test --workspace=packages/bruno-lang
24+
- name: Test Package bruno-schema
25+
run: npm run test --workspace=packages/bruno-schema
26+
- name: Test Package bruno-app
27+
run: npm run test --workspace=packages/bruno-app
28+
- name: Test Package bruno-js
29+
run: npm run test --workspace=packages/bruno-js
30+
- name: Test Package bruno-electron
31+
run: npm run test --workspace=packages/bruno-electron

Diff for: packages/bruno-electron/src/ipc/collection.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const {
1111
isDirectory,
1212
browseDirectory,
1313
createDirectory,
14-
searchForBruFiles
14+
searchForBruFiles,
15+
sanitizeDirectoryName
1516
} = require('../utils/filesystem');
1617
const { stringifyJson } = require('../utils/common');
1718
const { openCollectionDialog, openCollection } = require('../app/collections');
@@ -315,7 +316,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
315316

316317
ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => {
317318
try {
318-
let collectionName = collection.name;
319+
let collectionName = sanitizeDirectoryName(collection.name);
319320
let collectionPath = path.join(collectionLocation, collectionName);
320321

321322
if (fs.existsSync(collectionPath)) {
@@ -359,7 +360,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
359360
const uid = generateUidBasedOnHash(collectionPath);
360361
const brunoConfig = {
361362
version: '1',
362-
name: collection.name,
363+
name: collectionName,
363364
type: 'collection'
364365
};
365366
const content = await stringifyJson(brunoConfig);

Diff for: packages/bruno-electron/src/utils/filesystem.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ const searchForBruFiles = (dir) => {
114114
return searchForFiles(dir, '.bru');
115115
};
116116

117+
const sanitizeDirectoryName = (name) => {
118+
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
119+
};
120+
117121
module.exports = {
118122
isValidPathname,
119123
exists,
@@ -127,5 +131,6 @@ module.exports = {
127131
createDirectory,
128132
browseDirectory,
129133
searchForFiles,
130-
searchForBruFiles
134+
searchForBruFiles,
135+
sanitizeDirectoryName
131136
};

Diff for: packages/bruno-electron/src/utils/filesystem.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { sanitizeDirectoryName } = require('./filesystem.js');
2+
3+
describe('sanitizeDirectoryName', () => {
4+
it('should replace invalid characters with hyphens', () => {
5+
const input = '<>:"/\\|?*\x00-\x1F';
6+
const expectedOutput = '---';
7+
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
8+
});
9+
10+
it('should not modify valid directory names', () => {
11+
const input = 'my-directory';
12+
expect(sanitizeDirectoryName(input)).toEqual(input);
13+
});
14+
15+
it('should replace multiple invalid characters with a single hyphen', () => {
16+
const input = 'my<>invalid?directory';
17+
const expectedOutput = 'my-invalid-directory';
18+
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
19+
});
20+
21+
it('should handle names with slashes', () => {
22+
const input = 'my/invalid/directory';
23+
const expectedOutput = 'my-invalid-directory';
24+
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
25+
});
26+
});

0 commit comments

Comments
 (0)