Merge pull request #242 from tpyle/bug/handle-collection-names-with-slashes

Corrects issue when collection names in imports have slashes
This commit is contained in:
Anoop M D 2023-09-29 12:20:11 +05:30 committed by GitHub
commit 626d925ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 24 deletions

View File

@ -1,9 +1,9 @@
name: Unit Tests
on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
jobs:
test:
timeout-minutes: 60
@ -27,3 +27,5 @@ jobs:
run: npm run test --workspace=packages/bruno-app
- name: Test Package bruno-js
run: npm run test --workspace=packages/bruno-js
- name: Test Package bruno-electron
run: npm run test --workspace=packages/bruno-electron

View File

@ -11,7 +11,8 @@ const {
isDirectory,
browseDirectory,
createDirectory,
searchForBruFiles
searchForBruFiles,
sanitizeDirectoryName
} = require('../utils/filesystem');
const { stringifyJson } = require('../utils/common');
const { openCollectionDialog, openCollection } = require('../app/collections');
@ -315,7 +316,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => {
try {
let collectionName = collection.name;
let collectionName = sanitizeDirectoryName(collection.name);
let collectionPath = path.join(collectionLocation, collectionName);
if (fs.existsSync(collectionPath)) {
@ -359,7 +360,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
const uid = generateUidBasedOnHash(collectionPath);
const brunoConfig = {
version: '1',
name: collection.name,
name: collectionName,
type: 'collection'
};
const content = await stringifyJson(brunoConfig);

View File

@ -114,6 +114,10 @@ const searchForBruFiles = (dir) => {
return searchForFiles(dir, '.bru');
};
const sanitizeDirectoryName = (name) => {
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
};
module.exports = {
isValidPathname,
exists,
@ -127,5 +131,6 @@ module.exports = {
createDirectory,
browseDirectory,
searchForFiles,
searchForBruFiles
searchForBruFiles,
sanitizeDirectoryName
};

View File

@ -0,0 +1,26 @@
const { sanitizeDirectoryName } = require('./filesystem.js');
describe('sanitizeDirectoryName', () => {
it('should replace invalid characters with hyphens', () => {
const input = '<>:"/\\|?*\x00-\x1F';
const expectedOutput = '---';
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
});
it('should not modify valid directory names', () => {
const input = 'my-directory';
expect(sanitizeDirectoryName(input)).toEqual(input);
});
it('should replace multiple invalid characters with a single hyphen', () => {
const input = 'my<>invalid?directory';
const expectedOutput = 'my-invalid-directory';
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
});
it('should handle names with slashes', () => {
const input = 'my/invalid/directory';
const expectedOutput = 'my-invalid-directory';
expect(sanitizeDirectoryName(input)).toEqual(expectedOutput);
});
});