Skip to content

Commit

Permalink
feat(nx-python): add uv support for add, update, remove, install, loc…
Browse files Browse the repository at this point in the history
…k and release
  • Loading branch information
lucasvieirasilva committed Dec 12, 2024
1 parent 5a61487 commit e045334
Show file tree
Hide file tree
Showing 25 changed files with 5,518 additions and 174 deletions.
231 changes: 231 additions & 0 deletions packages/nx-python/src/executors/add/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { vol } from 'memfs';
import '../../utils/mocks/cross-spawn.mock';
import '../../utils/mocks/fs.mock';
import * as poetryUtils from '../../provider/poetry/utils';
import { UVProvider } from '../../provider/uv/provider';
import executor from './executor';
import chalk from 'chalk';
import { parseToml } from '../../provider/poetry/utils';
Expand Down Expand Up @@ -1303,4 +1304,234 @@ describe('Add Executor', () => {
expect(output.success).toBe(true);
});
});

describe('uv', () => {
let checkPrerequisites: MockInstance;

beforeEach(() => {
checkPrerequisites = vi
.spyOn(UVProvider.prototype, 'checkPrerequisites')
.mockResolvedValue(undefined);
vi.mocked(spawn.sync).mockReturnValue({
status: 0,
output: [''],
pid: 0,
signal: null,
stderr: null,
stdout: null,
});
vi.spyOn(process, 'chdir').mockReturnValue(undefined);
});

beforeEach(() => {
vol.fromJSON({
'uv.lock': '',
});
});

it('should return success false when the uv is not installed', async () => {
checkPrerequisites.mockRejectedValue(new Error('uv not found'));

const options = {
name: 'numpy',
local: false,
};

const context: ExecutorContext = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
};

const output = await executor(options, context);
expect(checkPrerequisites).toHaveBeenCalled();
expect(spawn.sync).not.toHaveBeenCalled();
expect(output.success).toBe(false);
});

it('run add target and should add the dependency to the project', async () => {
const options = {
name: 'numpy',
local: false,
};

const context: ExecutorContext = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
};

const output = await executor(options, context);
expect(checkPrerequisites).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith(
'uv',
['add', 'numpy', '--project', 'apps/app'],
{
cwd: '.',
shell: false,
stdio: 'inherit',
},
);
expect(output.success).toBe(true);
});

it('run add target and should add the dependency to the project group dev', async () => {
const options = {
name: 'numpy',
local: false,
group: 'dev',
};

const context: ExecutorContext = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
};

const output = await executor(options, context);
expect(checkPrerequisites).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith(
'uv',
['add', 'numpy', '--project', 'apps/app', '--group', 'dev'],
{
cwd: '.',
shell: false,
stdio: 'inherit',
},
);
expect(output.success).toBe(true);
});

it('run add target and should add the dependency to the project extras', async () => {
const options = {
name: 'numpy',
local: false,
extras: ['dev'],
};

const context: ExecutorContext = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
};

const output = await executor(options, context);
expect(checkPrerequisites).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith(
'uv',
['add', 'numpy', '--project', 'apps/app', '--extra', 'dev'],
{
cwd: '.',
shell: false,
stdio: 'inherit',
},
);
expect(output.success).toBe(true);
});

it('run add target and should throw an exception', async () => {
vi.mocked(spawn.sync).mockImplementation(() => {
throw new Error('fake error');
});

const options = {
name: 'numpy',
local: false,
};

const context: ExecutorContext = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
};

const output = await executor(options, context);
expect(checkPrerequisites).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith(
'uv',
['add', 'numpy', '--project', 'apps/app'],
{
cwd: '.',
shell: false,
stdio: 'inherit',
},
);
expect(output.success).toBe(false);
});
});
});
Loading

0 comments on commit e045334

Please sign in to comment.