From f6c371c338750a6d422ad0c1190b4812db4ff37a Mon Sep 17 00:00:00 2001 From: Adrian Dimech Date: Fri, 21 Feb 2025 11:58:41 +1100 Subject: [PATCH] feat(nx-python): add an optional fix option for the ruff-check executor allows a user to configure whether they want to fix lint errors via nx configurations by setting the fix property to true. --- .../src/executors/ruff-check/executor.spec.ts | 51 +++++++++++++++++++ .../src/executors/ruff-check/executor.ts | 4 ++ .../src/executors/ruff-check/schema.d.ts | 1 + .../src/executors/ruff-check/schema.json | 6 +++ 4 files changed, 62 insertions(+) diff --git a/packages/nx-python/src/executors/ruff-check/executor.spec.ts b/packages/nx-python/src/executors/ruff-check/executor.spec.ts index 35a486b..8468a1d 100644 --- a/packages/nx-python/src/executors/ruff-check/executor.spec.ts +++ b/packages/nx-python/src/executors/ruff-check/executor.spec.ts @@ -294,6 +294,57 @@ describe('Ruff Check Executor', () => { expect(output.success).toBe(true); }); + it('should execute ruff check linting', async () => { + vi.mocked(spawn.sync).mockReturnValueOnce({ + status: 0, + output: [''], + pid: 0, + signal: null, + stderr: null, + stdout: null, + }); + + const output = await executor( + { + lintFilePatterns: ['app'], + fix: true, + __unparsed__: [], + }, + { + cwd: '', + root: '.', + isVerbose: false, + projectName: 'app', + projectsConfigurations: { + version: 2, + projects: { + app: { + root: 'apps/app', + targets: {}, + }, + }, + }, + nxJsonConfiguration: {}, + projectGraph: { + dependencies: {}, + nodes: {}, + }, + }, + ); + expect(checkPrerequisites).toHaveBeenCalled(); + expect(spawn.sync).toHaveBeenCalledTimes(1); + expect(spawn.sync).toHaveBeenCalledWith( + 'uv', + ['run', 'ruff', 'check', 'app', '--fix'], + { + cwd: 'apps/app', + shell: true, + stdio: 'inherit', + }, + ); + expect(output.success).toBe(true); + }); + it('should fail to execute ruff check linting ', async () => { vi.mocked(spawn.sync).mockReturnValueOnce({ status: 1, diff --git a/packages/nx-python/src/executors/ruff-check/executor.ts b/packages/nx-python/src/executors/ruff-check/executor.ts index 94daee1..6ec2c6b 100644 --- a/packages/nx-python/src/executors/ruff-check/executor.ts +++ b/packages/nx-python/src/executors/ruff-check/executor.ts @@ -24,6 +24,10 @@ export default async function executor( .concat(options.lintFilePatterns) .concat(options.__unparsed__); + if (options.fix) { + commandArgs.push('--fix'); + } + const provider = await getProvider( workspaceRoot, undefined, diff --git a/packages/nx-python/src/executors/ruff-check/schema.d.ts b/packages/nx-python/src/executors/ruff-check/schema.d.ts index c937501..d9c8565 100644 --- a/packages/nx-python/src/executors/ruff-check/schema.d.ts +++ b/packages/nx-python/src/executors/ruff-check/schema.d.ts @@ -1,4 +1,5 @@ export interface RuffCheckExecutorSchema { lintFilePatterns: string[]; + fix?: boolean; __unparsed__: string[]; } diff --git a/packages/nx-python/src/executors/ruff-check/schema.json b/packages/nx-python/src/executors/ruff-check/schema.json index 107c386..aa3afc3 100644 --- a/packages/nx-python/src/executors/ruff-check/schema.json +++ b/packages/nx-python/src/executors/ruff-check/schema.json @@ -11,6 +11,12 @@ "type": "string" } }, + "fix": { + "type": "boolean", + "description": "Fixes linting errors (may overwrite linted files).", + "default": false, + "x-priority": "important" + }, "__unparsed__": { "hidden": true, "type": "array",