Corrects issue when collection names in imports have slashes

This commit is contained in:
Thomas Pyle 2023-09-28 21:39:33 -04:00
parent 516411b9a2
commit 2c0ccf769c
4 changed files with 58 additions and 24 deletions

View File

@ -1,9 +1,9 @@
name: Unit Tests name: Unit Tests
on: on:
push: push:
branches: [ main ] branches: [main]
pull_request: pull_request:
branches: [ main ] branches: [main]
jobs: jobs:
test: test:
timeout-minutes: 60 timeout-minutes: 60
@ -27,3 +27,5 @@ jobs:
run: npm run test --workspace=packages/bruno-app run: npm run test --workspace=packages/bruno-app
- name: Test Package bruno-js - name: Test Package bruno-js
run: npm run test --workspace=packages/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, isDirectory,
browseDirectory, browseDirectory,
createDirectory, createDirectory,
searchForBruFiles searchForBruFiles,
sanitizeDirectoryName
} = require('../utils/filesystem'); } = require('../utils/filesystem');
const { stringifyJson } = require('../utils/common'); const { stringifyJson } = require('../utils/common');
const { openCollectionDialog, openCollection } = require('../app/collections'); const { openCollectionDialog, openCollection } = require('../app/collections');
@ -315,7 +316,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => { ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => {
try { try {
let collectionName = collection.name; let collectionName = sanitizeDirectoryName(collection.name);
let collectionPath = path.join(collectionLocation, collectionName); let collectionPath = path.join(collectionLocation, collectionName);
if (fs.existsSync(collectionPath)) { if (fs.existsSync(collectionPath)) {
@ -359,7 +360,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
const uid = generateUidBasedOnHash(collectionPath); const uid = generateUidBasedOnHash(collectionPath);
const brunoConfig = { const brunoConfig = {
version: '1', version: '1',
name: collection.name, name: collectionName,
type: 'collection' type: 'collection'
}; };
const content = await stringifyJson(brunoConfig); const content = await stringifyJson(brunoConfig);

View File

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