Skip to content

Commit

Permalink
refactor: fix upload config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Nov 22, 2024
1 parent dee46b3 commit 8be5833
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
7 changes: 0 additions & 7 deletions e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ async function addTargetToWorkspace(
codeStrings: 'customPlugin()',
},
],
// The upload test is skipped as it requires the @code-pushup/portal-client dependency
upload: {
server: 'https://dummy-server.dev',
organization: 'dummy-organization',
apiKey: 'dummy-api-key',
project: 'dummy-project',
},
});
await materializeTree(tree, cwd);
}
Expand Down
11 changes: 9 additions & 2 deletions packages/nx-plugin/src/executors/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ export function parseAutorunExecutorOptions(
options: Partial<AutorunCommandExecutorOptions>,
normalizedContext: NormalizedExecutorContext,
): AutorunCommandExecutorOptions {
const { projectPrefix, persist, upload } = options;
const { projectPrefix, persist, upload, command } = options;
const needsUploadParams =
command === 'upload' || command === 'autorun' || command === undefined;
return {
...parseAutorunExecutorOnlyOptions(options),
...globalConfig(options, normalizedContext),
persist: persistConfig({ projectPrefix, ...persist }, normalizedContext),
upload: uploadConfig({ projectPrefix, ...upload }, normalizedContext),
// @TODO This is a hack to avoid validation errors of upload config for commands that dont need it.
// Fix: use utils and execute the core logic directly
// Blocked by Nx plugins can't compile to es6
upload: needsUploadParams
? uploadConfig({ projectPrefix, ...upload }, normalizedContext)
: undefined,
};
}
59 changes: 59 additions & 0 deletions packages/nx-plugin/src/executors/cli/utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type MockInstance, expect, vi } from 'vitest';
import { osAgnosticPath } from '@code-pushup/test-utils';
import type { Command } from '../internal/types';
import {
parseAutorunExecutorOnlyOptions,
parseAutorunExecutorOptions,
Expand Down Expand Up @@ -93,4 +94,62 @@ describe('parseAutorunExecutorOptions', () => {
osAgnosticPath('workspaceRoot/.code-pushup/my-app'),
);
});

it.each<Command | undefined>(['upload', 'autorun', undefined])(
'should include upload config for command %s',
command => {
const projectName = 'my-app';
const executorOptions = parseAutorunExecutorOptions(
{
command,
upload: {
organization: 'code-pushup',
},
},
{
projectName,
workspaceRoot: 'workspaceRoot',
projectConfig: {
name: 'my-app',
root: 'root',
},
},
);

expect(executorOptions).toEqual(
expect.objectContaining({
upload: expect.any(Object),
}),
);
},
);

it.each<Command>(['collect'])(
'should not include upload config for command %s',
command => {
const projectName = 'my-app';
const executorOptions = parseAutorunExecutorOptions(
{
command,
upload: {
organization: 'code-pushup',
},
},
{
projectName,
workspaceRoot: 'workspaceRoot',
projectConfig: {
name: 'my-app',
root: 'root',
},
},
);

expect(executorOptions).toEqual(
expect.not.objectContaining({
upload: expect.any(Object),
}),
);
},
);
});
17 changes: 9 additions & 8 deletions packages/nx-plugin/src/executors/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ export type ProjectExecutorOnlyOptions = {
/**
* CLI types that apply globally for all commands.
*/
export type Command =
| 'collect'
| 'upload'
| 'autorun'
| 'print-config'
| 'compare'
| 'merge-diffs'
| 'history';
export type GlobalExecutorOptions = {
command?:
| 'collect'
| 'upload'
| 'autorun'
| 'print-config'
| 'compare'
| 'merge-diffs'
| 'history';
command?: Command;
bin?: string;
verbose?: boolean;
progress?: boolean;
Expand Down

0 comments on commit 8be5833

Please sign in to comment.