Skip to content

Commit

Permalink
feat: add support for poetry 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasvieirasilva committed Jan 29, 2025
1 parent 6bcd8d5 commit 6743d9b
Show file tree
Hide file tree
Showing 23 changed files with 1,522 additions and 284 deletions.
315 changes: 264 additions & 51 deletions packages/nx-python/src/executors/add/executor.spec.ts

Large diffs are not rendered by default.

175 changes: 174 additions & 1 deletion packages/nx-python/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,170 @@ describe('Build Executor', () => {
});

describe('locked resolver', () => {
it('should install poetry-plugin-export plugin before build python project', async () => {
vol.fromJSON({
'apps/app/.venv/pyvenv.cfg': 'fake',
'apps/app/app/index.py': 'print("Hello from app")',
'apps/app/poetry.lock': dedent`
[[package]]
name = "click"
version = "7.1.2"
description = "Composable command line interface toolkit"
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
`,

'apps/app/pyproject.toml': dedent`
[tool.poetry]
name = "app"
version = "1.0.0"
[[tool.poetry.packages]]
include = "app"
[tool.poetry.dependencies]
python = "^3.8"
click = "7.1.2"
[tool.poetry.group.dev.dependencies]
pytest = "6.2.4"
`,
});

const spawnMock = vi.mocked(spawn.sync);

spawnMock
.mockReturnValueOnce({
status: 1,
output: [''],
pid: 0,
signal: null,
stderr: 'The command "export" does not exist',
stdout: null,
})
.mockImplementation((_, args, opts) => {
if (args[0] == 'build') {
spawnBuildMockImpl(opts);
} else if (args[0] == 'export' && opts.cwd === 'apps/app') {
writeFileSync(
join(buildPath, 'requirements.txt'),
dedent`
click==7.1.2
`,
);
}
return {
status: 0,
output: [''],
pid: 0,
signal: null,
stderr: null,
stdout: null,
};
});

const options: BuildExecutorSchema = {
ignorePaths: ['.venv', '.tox', 'tests/'],
silent: false,
outputPath: 'dist/apps/app',
keepBuildFolder: true,
devDependencies: false,
lockedVersions: true,
bundleLocalDependencies: true,
};

const output = await executor(options, {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
projectsConfigurations: {
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
nxJsonConfiguration: {},
projectGraph: {
dependencies: {},
nodes: {},
},
});

expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(existsSync(buildPath)).toBeTruthy();
expect(existsSync(`${buildPath}/app`)).toBeTruthy();
expect(existsSync(`${buildPath}/dist/app.fake`)).toBeTruthy();
expect(spawn.sync).toHaveBeenCalledTimes(4);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['export', '--help'],
{
cwd: 'apps/app',
stdio: 'pipe',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['self', 'add', 'poetry-plugin-export'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(
3,
'poetry',
[
'export',
'--format',
'requirements.txt',
'--without-hashes',
'--without-urls',
'--output',
`${buildPath}/requirements.txt`,
],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(4, 'poetry', ['build'], {
cwd: buildPath,
shell: false,
stdio: 'inherit',
});

const projectTomlData = parse(
readFileSync(`${buildPath}/pyproject.toml`).toString('utf-8'),
) as PoetryPyprojectToml;

expect(projectTomlData.tool.poetry.packages).toStrictEqual([
{
include: 'app',
},
]);

expect(projectTomlData.tool.poetry.dependencies).toStrictEqual({
python: '^3.8',
click: '7.1.2',
});
expect(
projectTomlData.tool.poetry.group.dev.dependencies,
).toStrictEqual({});

expect(output.success).toBe(true);
});

it('should build python project with local dependencies and keep the build folder', async () => {
vol.fromJSON({
'apps/app/.venv/pyvenv.cfg': 'fake',
Expand Down Expand Up @@ -1803,6 +1967,15 @@ describe('Build Executor', () => {
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['export', '--help'],
{
cwd: 'apps/app',
stdio: 'pipe',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
[
'export',
'--format',
Expand All @@ -1820,7 +1993,7 @@ describe('Build Executor', () => {
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['build'], {
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['build'], {
cwd: buildPath,
shell: false,
stdio: 'inherit',
Expand Down
9 changes: 7 additions & 2 deletions packages/nx-python/src/executors/install/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Install Executor', () => {

afterEach(() => {
vol.reset();
vi.resetAllMocks();
});

describe('poetry', () => {
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('Install Executor', () => {
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith(
'poetry',
['install', '-v', '--no-dev'],
['install', '--no-dev', '-v'],
{
stdio: 'inherit',
shell: false,
Expand Down Expand Up @@ -158,7 +159,7 @@ describe('Install Executor', () => {

const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(spawn.sync).toHaveBeenCalledWith('poetry', ['install', '-vv'], {
expect(spawn.sync).toHaveBeenCalledWith('poetry', ['install', '-vvv'], {
stdio: 'inherit',
shell: false,
cwd: 'apps/app',
Expand Down Expand Up @@ -206,6 +207,10 @@ describe('Install Executor', () => {
stdio: 'inherit',
shell: false,
cwd: 'apps/app',
env: {
...process.env,
POETRY_CACHE_DIR: path.resolve('apps/app/.cache/pypoetry'),
},
});
expect(output.success).toBe(false);
});
Expand Down
Loading

0 comments on commit 6743d9b

Please sign in to comment.