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,29 +1,31 @@
name: Unit Tests
on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies
run: npm i --legacy-peer-deps
- name: Test Package bruno-query
run: npm run test --workspace=packages/bruno-query
- name: Build Package bruno-query
run: npm run build --workspace=packages/bruno-query
- name: Test Package bruno-lang
run: npm run test --workspace=packages/bruno-lang
- name: Test Package bruno-schema
run: npm run test --workspace=packages/bruno-schema
- name: Test Package bruno-app
run: npm run test --workspace=packages/bruno-app
- name: Test Package bruno-js
run: npm run test --workspace=packages/bruno-js
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Install dependencies
run: npm i --legacy-peer-deps
- name: Test Package bruno-query
run: npm run test --workspace=packages/bruno-query
- name: Build Package bruno-query
run: npm run build --workspace=packages/bruno-query
- name: Test Package bruno-lang
run: npm run test --workspace=packages/bruno-lang
- name: Test Package bruno-schema
run: npm run test --workspace=packages/bruno-schema
- name: Test Package bruno-app
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);
});
});