Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/extension/commands/sidebar-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,19 @@ async function handlePaste(item: Entity | undefined, getTarget: GetTargetFn, set

const dest = await getUniqueUri(parent.uri, sourceName, '');
await vscode.workspace.fs.copy(copied.uri, dest, { overwrite: false });

// As for a request, the folder's own name picks up the " copy" suffix its directory did.
// Without a name of its own the folder shows its directory's, which already carries it.
const suffix = basename(dest).slice(sourceName.length);
const folderFile = vscode.Uri.joinPath(dest, `folder.${format}`);
if (suffix && await doesItExist(folderFile)) {
const { meta } = parseFolder(await readText(folderFile), { format }) ?? {};
// The parser substitutes "Untitled Folder" when the file carries no name.
if (meta?.name && meta.name !== 'Untitled Folder') {
await updateNameInFile(folderFile, 'folder', `${meta.name}${suffix}`, format);
}
}

return;
}

Expand Down
12 changes: 10 additions & 2 deletions src/extension/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,9 @@ const HTTP_METHOD_BLOCK_REGEX = /^\s*(get|post|put|delete|patch|options|head|tra

const parseBruFileMeta = (data: string): Record<string, unknown> | null => {
try {
const metaRegex = /meta\s*{\s*([\s\S]*?)\s*}/;
// The block ends at a `}` on its own line, so a `}` inside a value
// (e.g. `name: user/{id}/path`) does not cut the block short.
const metaRegex = /meta\s*{([\s\S]*?)\n\s*}/;
const match = data?.match?.(metaRegex);
if (match) {
const metaContent = match[1].trim();
Expand All @@ -619,7 +621,13 @@ const parseBruFileMeta = (data: string): Record<string, unknown> | null => {
}
return;
}
const [key, value] = trimmed.split(':').map((str) => str.trim());
// Split on the first colon only: values such as `user/:id` contain more.
const colonIndex = trimmed.indexOf(':');
if (colonIndex === -1) {
return;
}
const key = trimmed.slice(0, colonIndex).trim();
const value = trimmed.slice(colonIndex + 1).trim();
if (key && value) {
if (value === '[' || value.startsWith('[')) {
insideArray = !value.includes(']');
Expand Down
57 changes: 57 additions & 0 deletions src/extension/utils/parse-file-meta.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, test, expect, vi } from 'vitest';

vi.mock('vscode', () => ({}));

const { parseBruFileMeta, parseFileMeta } = await import('./collection');

const bru = (metaLines: string[], eol = '\n') =>
['meta {', ...metaLines.map((line) => ` ${line}`), '}', '', 'get {', ' url: https://example.com/{id}', '}', ''].join(eol);

const yml = (name: string) =>
['info:', ` name: ${name}`, ' type: http', ' seq: 4', 'http:', ' method: GET', ' url: https://example.com', ''].join('\n');

describe('parseBruFileMeta reads request names in full (#215)', () => {
test('keeps a plain name, numeric seq, type and method', () => {
const meta = parseBruFileMeta(bru(['name: Get users', 'type: http', 'seq: 3']));
expect(meta).toMatchObject({ name: 'Get users', type: 'http-request', seq: 3, tags: [] });
expect((meta?.request as { method: string }).method).toBe('get');
});

test('keeps colons after the first one in the name', () => {
const meta = parseBruFileMeta(bru(['name: user/:username/sub/path', 'type: http', 'seq: 1']));
expect(meta?.name).toBe('user/:username/sub/path');
});

test('keeps a closing brace inside the name', () => {
const meta = parseBruFileMeta(bru(['name: user/{username}/sub/path', 'type: http', 'seq: 2']));
expect(meta?.name).toBe('user/{username}/sub/path');
expect(meta?.seq).toBe(2);
});

test('handles CRLF line endings', () => {
const meta = parseBruFileMeta(bru(['name: user/{username}/:id', 'type: graphql', 'seq: 7'], '\r\n'));
expect(meta).toMatchObject({ name: 'user/{username}/:id', type: 'graphql-request', seq: 7 });
});

test('skips a tags array without losing the keys after it', () => {
const meta = parseBruFileMeta(bru(['name: Tagged', 'type: http', 'tags: [', ' smoke', ' regression', ']', 'seq: 5']));
expect(meta).toMatchObject({ name: 'Tagged', type: 'http-request', seq: 5, tags: [] });
});

test('skips an inline tags array', () => {
const meta = parseBruFileMeta(bru(['name: Tagged', 'tags: [smoke]', 'seq: 6']));
expect(meta).toMatchObject({ name: 'Tagged', seq: 6 });
});

test('returns null when there is no meta block', () => {
expect(parseBruFileMeta('get {\n url: https://example.com\n}\n')).toBeNull();
});
});

describe('parseFileMeta for yml reads the same names in full', () => {
test.each(['Get users', 'user/:username/sub/path', 'user/{username}/sub/path'])('%s', (name) => {
const meta = parseFileMeta(yml(name), 'yml');
expect(meta).toMatchObject({ name, type: 'http-request', seq: 4 });
expect((meta?.request as { method: string }).method).toBe('get');
});
});
1 change: 1 addition & 0 deletions tests/e2e/collection/clone/clone-collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { buildCommonLocators } from '../../utils/page/locators';

test.describe('Clone collection', () => {

// @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyCollectionFunctionalities: Clone)
test('TC-3647: Verify the Cloning the collection from collection ellipse clone option', { tag: '@sanity' }, async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Source Collection';
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/collection/create/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { buildCommonLocators } from '../../utils/page/locators';

test.describe('Collection management', () => {

// @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyWelcomeView and the setup, which creates every collection from the sidebar)
test('TC-3504: Create a collection from the sidebar', { tag: '@sanity' }, async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'My Test Collection';
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/collection/delete/collection-removal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test.describe('Collection removal', () => {
await expect(row).toHaveCount(1);
});

// @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyFolderFunctionalities: Delete)
test('Deleted folder disappears from the sidebar', async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Folder Delete Test';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
const TEST_SERVER = 'http://127.0.0.1:8081';

test.describe('Paste on a request', () => {
// @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyRequestFunctionalities: Copy, then Paste onto a request)
test('pasting with a request row focused puts the copy beside it', async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Paste Target';
Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/collection/moving-requests/reorder-requests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
closeEditorTabIfOpen,
createFolder,
expandCollection,
dragItemAboveOrBelow,
moveItemAboveOrBelow,
findFilesWithName
} from '../../utils/page/actions';
import { buildCommonLocators } from '../../utils/page/locators';

test.describe('Reorder requests by drag and drop', () => {
// @deprecated Moved to tests/e2e/sidebar2/suites/reorder.ts (reorderRequests)
// @deprecated Moved to tests/e2e/sidebar/sidebar-actions.test.ts (verifyReorder)
test('Dropping a request above another and below the last row saves the new order', async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Reorder Collection';
Expand All @@ -36,19 +36,19 @@ test.describe('Reorder requests by drag and drop', () => {
});

await test.step('Drop r3 above r1 and verify the new order in the sidebar and on disk', async () => {
await dragItemAboveOrBelow(sidebar, 'r3', 'r1', 'above');
await moveItemAboveOrBelow(sidebar, 'r3', 'r1', 'above');
Comment thread
Gibin-bruno marked this conversation as resolved.
Resolved
await expect(rows()).toHaveText([/r3/, /r1/, /r2/]);
await expect.poll(() => [seqOf('r3'), seqOf('r1'), seqOf('r2')]).toEqual([1, 2, 3]);
});

await test.step('Drop r3 below the last row (below r2) and verify the new order in the sidebar and on disk', async () => {
await dragItemAboveOrBelow(sidebar, 'r3', 'r2', 'below');
await moveItemAboveOrBelow(sidebar, 'r3', 'r2', 'below');
await expect(rows()).toHaveText([/r1/, /r2/, /r3/]);
await expect.poll(() => [seqOf('r1'), seqOf('r2'), seqOf('r3')]).toEqual([1, 2, 3]);
});
});

// @deprecated Moved to tests/e2e/sidebar2/suites/reorder.ts (reorderFolders)
// @deprecated Moved to tests/e2e/sidebar/sidebar-actions.test.ts (verifyReorder)
test('Dropping a folder above another and below the last row saves the new order', async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Reorder Folders Collection';
Expand All @@ -69,13 +69,13 @@ test.describe('Reorder requests by drag and drop', () => {
});

await test.step('Drop f3 above f1 and verify the new order in the sidebar and on disk', async () => {
await dragItemAboveOrBelow(sidebar, 'f3', 'f1', 'above');
await moveItemAboveOrBelow(sidebar, 'f3', 'f1', 'above');
await expect(rows()).toHaveText([/f3/, /f1/, /f2/]);
await expect.poll(() => [seqOf('f3'), seqOf('f1'), seqOf('f2')]).toEqual([1, 2, 3]);
});

await test.step('Drop f3 below the last row (below f2) and verify the new order in the sidebar and on disk', async () => {
await dragItemAboveOrBelow(sidebar, 'f3', 'f2', 'below');
await moveItemAboveOrBelow(sidebar, 'f3', 'f2', 'below');
await expect(rows()).toHaveText([/f1/, /f2/, /f3/]);
await expect.poll(() => [seqOf('f1'), seqOf('f2'), seqOf('f3')]).toEqual([1, 2, 3]);
});
Expand Down
17 changes: 12 additions & 5 deletions tests/e2e/context.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import type { FileFormat } from './utils/page/sidebar';

type CollectionInfo = {
name: string,
location: string
location: string,
format: FileFormat,
}

// One per test: every collection the test creates, by name.
export class Context {
private collections = new Map<string, CollectionInfo>();
private _collections = new Map<string, CollectionInfo>();

get collections(): string[] {
return Array.from(this._collections.keys());
}

addCollection(name: string, location: string) {
this.collections.set(name, { name, location });
addCollection(name: string, location: string, format: FileFormat) {
this._collections.set(name, { name, location, format });
}

getCollection(name: string): CollectionInfo {
const collection = this.collections.get(name);
const collection = this._collections.get(name);

if (!collection) {
throw new Error(`Collection "${name}" was not created in this test`);
Expand Down
16 changes: 8 additions & 8 deletions tests/e2e/e2e-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ async function addRequest(page: Page, sidebar: Sidebar, collection: string, { re

export type Setup = {
sidebar: Sidebar,
// Every collection created, by name
ctx: Context,
// Names of the collections it created
collections: string[],
}

// Builds the workspace. Collections go in `location`, by default a fresh temp folder outside the
Expand All @@ -149,11 +146,15 @@ export async function createSetup<const EnvName extends string = never>(
): Promise<Setup> {
const sidebar = await openBrunoSidebar(page);
const ctx = new Context();
const collections: string[] = [];

for (const collection of config.collections) {
if (typeof collection === 'string') {
// import collection
const name = await sidebar.importFile(
collection,
location,
collection.endsWith('.zip') ? undefined : 'yml'
);
ctx.addCollection(name, location, fs.existsSync(path.join(location, name, 'bruno.json')) ? 'bru' : 'yml');
} else {
const folders = flattenFolders(collection.folders)
const requests: RequestEntry[] = [
Expand All @@ -162,7 +163,6 @@ export async function createSetup<const EnvName extends string = never>(
]

await createCollectionV2(sidebar, ctx, collection.name, location, collection.format)
collections.push(collection.name)

verifyGitignore(ctx, collection.name)

Expand Down Expand Up @@ -215,7 +215,7 @@ export async function createSetup<const EnvName extends string = never>(
}
}

return { sidebar, ctx, collections }
return { sidebar, ctx }
}

export type Workspace = Parameters<typeof createSetup>[0];
Expand All @@ -237,7 +237,7 @@ export function runSuites<const S extends LifecycleSuite[]>(title: string, suite
setup?.(test);

test.beforeEach(async ({ workbench }) => {
({ collections } = await createSetup(workspace, workbench));
({ ctx: { collections } } = await createSetup(workspace, workbench));
});

test.afterEach(async ({ workbench }) => {
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/folder/create/folder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { buildCommonLocators } from '../../utils/page/locators';

test.describe('Folder management', () => {

// @deprecated Covered by tests/e2e/sidebar/sidebar-actions.test.ts (verifyCollectionFunctionalities: New Folder)
test('TC-3648:Verify Adding a new folder under collection', { tag: '@sanity' }, async ({ page, tmpDir }) => {
const sidebar = await openBrunoSidebar(page);
const collectionName = 'Folder Collection';
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/response2/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test.describe('response lifecycle', () => {
let collections: string[] = [];

test.beforeEach(async ({ workbench }) => {
({ collections } = await createSetup(workspace, workbench));
({ ctx: { collections } } = await createSetup(workspace, workbench));
});

test.afterEach(async ({ workbench }) => {
Expand Down
86 changes: 86 additions & 0 deletions tests/e2e/sidebar/fixtures/bruno-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"name": "br-collection",
"version": "1",
"items": [
{
"type": "folder",
"name": "br-f1",
"items": [
{
"type": "http",
"name": "br-f1-r1",
"seq": 1,
"request": {
"url": "http://127.0.0.1:8081/api/echo/json",
"method": "POST",
"headers": [],
"params": [],
"body": {
"mode": "none"
},
"auth": {
"mode": "none"
}
}
}
]
},
{
"type": "folder",
"name": "br-f2",
"items": [
{
"type": "http",
"name": "br-f2-r1",
"seq": 1,
"request": {
"url": "http://127.0.0.1:8081/api/echo/json",
"method": "POST",
"headers": [],
"params": [],
"body": {
"mode": "none"
},
"auth": {
"mode": "none"
}
}
}
]
},
{
"type": "http",
"name": "br-r1",
"seq": 1,
"request": {
"url": "http://127.0.0.1:8081/ping",
"method": "GET",
"headers": [],
"params": [],
"body": {
"mode": "none"
},
"auth": {
"mode": "none"
}
}
},
{
"type": "http",
"name": "br-r2",
"seq": 2,
"request": {
"url": "http://127.0.0.1:8081/api/echo/json",
"method": "POST",
"headers": [],
"params": [],
"body": {
"mode": "none"
},
"auth": {
"mode": "none"
}
}
}
]
}
Loading
Loading