Skip to content

Commit

Permalink
fix: expand glob before checking API count in join (#1757)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeelChotai authored Dec 6, 2024
1 parent b151753 commit 7ba0d13
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-donkeys-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redocly/cli": patch
---

Fixed an issue where `join` would throw an error when a glob pattern was provided.
37 changes: 35 additions & 2 deletions packages/cli/src/__tests__/commands/join.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { yellow } from 'colorette';
import { detectSpec } from '@redocly/openapi-core';
import { handleJoin } from '../../commands/join';
import { exitWithError, writeToFileByExtension } from '../../utils/miscellaneous';
import {
exitWithError,
getFallbackApisOrExit,
writeToFileByExtension,
} from '../../utils/miscellaneous';
import { loadConfig } from '../../__mocks__/@redocly/openapi-core';
import { ConfigFixture } from '../fixtures/config';

Expand All @@ -15,7 +19,35 @@ describe('handleJoin', () => {

it('should call exitWithError because only one entrypoint', async () => {
await handleJoin({ argv: { apis: ['first.yaml'] }, config: {} as any, version: 'cli-version' });
expect(exitWithError).toHaveBeenCalledWith(`At least 2 apis should be provided.`);
expect(exitWithError).toHaveBeenCalledWith(`At least 2 APIs should be provided.`);
});

it('should call exitWithError if glob expands to less than 2 APIs', async () => {
(getFallbackApisOrExit as jest.Mock).mockResolvedValueOnce([{ path: 'first.yaml' }]);

await handleJoin({
argv: { apis: ['*.yaml'] },
config: {} as any,
version: 'cli-version',
});

expect(exitWithError).toHaveBeenCalledWith(`At least 2 APIs should be provided.`);
});

it('should proceed if glob expands to 2 or more APIs', async () => {
(detectSpec as jest.Mock).mockReturnValue('oas3_1');
(getFallbackApisOrExit as jest.Mock).mockResolvedValueOnce([
{ path: 'first.yaml' },
{ path: 'second.yaml' },
]);

await handleJoin({
argv: { apis: ['*.yaml'] },
config: ConfigFixture as any,
version: 'cli-version',
});

expect(exitWithError).not.toHaveBeenCalled();
});

it('should call exitWithError because passed all 3 options for tags', async () => {
Expand Down Expand Up @@ -52,6 +84,7 @@ describe('handleJoin', () => {
});

it('should call exitWithError because Only OpenAPI 3.0 and OpenAPI 3.1 are supported', async () => {
(detectSpec as jest.Mock).mockReturnValueOnce('oas2_0');
await handleJoin({
argv: {
apis: ['first.yaml', 'second.yaml'],
Expand Down
15 changes: 8 additions & 7 deletions packages/cli/src/commands/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,12 @@ export async function handleJoin({
}: CommandArgs<JoinOptions>) {
const startedAt = performance.now();

if (argv.apis.length < 2) {
return exitWithError(`At least 2 apis should be provided.`);
}

const fileExtension = getAndValidateFileExtension(argv.output || argv.apis[0]);

const {
'prefix-components-with-info-prop': prefixComponentsWithInfoProp,
'prefix-tags-with-filename': prefixTagsWithFilename,
'prefix-tags-with-info-prop': prefixTagsWithInfoProp,
'without-x-tag-groups': withoutXTagGroups,
output: specFilename = `openapi.${fileExtension}`,
output,
} = argv;

const usedTagsOptions = [
Expand All @@ -91,6 +85,13 @@ export async function handleJoin({
}

const apis = await getFallbackApisOrExit(argv.apis, config);
if (apis.length < 2) {
return exitWithError(`At least 2 APIs should be provided.`);
}

const fileExtension = getAndValidateFileExtension(output || apis[0].path);
const specFilename = output || `openapi.${fileExtension}`;

const externalRefResolver = new BaseResolver(config.resolve);
const documents = await Promise.all(
apis.map(
Expand Down

1 comment on commit 7ba0d13

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 78.63% 5040/6410
🟡 Branches 67.27% 2053/3052
🟡 Functions 73.16% 834/1140
🟡 Lines 78.9% 4752/6023

Test suite run success

834 tests passing in 121 suites.

Report generated by 🧪jest coverage report action from 7ba0d13

Please sign in to comment.