Skip to content

Commit

Permalink
feat(nx-python): parameterize python version range, tox envlist and a…
Browse files Browse the repository at this point in the history
…dd .python-version file (#74)

feat(nx-python): parameterize python version range, tox envlist and add .python-versions file

re #67
  • Loading branch information
lucasvieirasilva authored Mar 22, 2023
1 parent f06396e commit 70d8a3b
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 11 deletions.
13 changes: 7 additions & 6 deletions e2e/nx-python-e2e/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
export default {
displayName: 'nx-python-e2e',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
globals: {},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/e2e/nx-python-e2e',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ build-backend = "poetry.core.masonry.api"
"
`;

exports[`nx-python migrate-shared-venv generator should migrate an isolate venv to shared venv 3`] = `
"3.8.11
"
`;

exports[`nx-python migrate-shared-venv generator should migrate an isolate venv to shared venv project without dev dependencies 1`] = `
"[tool.coverage.run]
branch = true
Expand Down Expand Up @@ -124,3 +129,8 @@ requires = [ "poetry-core==1.1.0" ]
build-backend = "poetry.core.masonry.api"
"
`;

exports[`nx-python migrate-shared-venv generator should migrate an isolate venv to shared venv project without dev dependencies 3`] = `
"3.8.11
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= pyenvPythonVersion %>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = 'Proprietary'
readme = 'README.md'

[tool.poetry.dependencies]
python = ">=3.8,<3.10"
python = "<%- pyprojectPythonDependency %>"

[build-system]
requires = ["poetry-core==1.1.0"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('nx-python migrate-shared-venv generator', () => {
expect(
generator(appTree, {
moveDevDependencies: true,
pyenvPythonVersion: '3.8.11',
pyprojectPythonDependency: '>=3.8,<3.10',
})
).rejects.toThrow('poetry not found');

Expand All @@ -44,10 +46,15 @@ describe('nx-python migrate-shared-venv generator', () => {
packageName: 'proj1',
buildLockedVersions: true,
buildBundleLocalDependencies: true,
pyenvPythonVersion: '3.8.11',
pyprojectPythonDependency: '>=3.8,<3.10',
toxEnvlist: 'py38',
});

const task = await generator(appTree, {
moveDevDependencies: true,
pyenvPythonVersion: '3.8.11',
pyprojectPythonDependency: '>=3.8,<3.10',
});
task();

Expand All @@ -56,6 +63,7 @@ describe('nx-python migrate-shared-venv generator', () => {
appTree.read('apps/proj1/pyproject.toml', 'utf-8')
).toMatchSnapshot();
expect(appTree.read('pyproject.toml', 'utf-8')).toMatchSnapshot();
expect(appTree.read('.python-version', 'utf-8')).toMatchSnapshot();
expect(spawnSyncMock).toHaveBeenNthCalledWith(
1,
'poetry',
Expand All @@ -79,10 +87,15 @@ describe('nx-python migrate-shared-venv generator', () => {
packageName: 'proj1',
buildLockedVersions: true,
buildBundleLocalDependencies: true,
pyenvPythonVersion: '3.8.11',
pyprojectPythonDependency: '>=3.8,<3.10',
toxEnvlist: 'py38',
});

const task = await generator(appTree, {
moveDevDependencies: true,
pyenvPythonVersion: '3.8.11',
pyprojectPythonDependency: '>=3.8,<3.10',
});
task();

Expand All @@ -91,6 +104,7 @@ describe('nx-python migrate-shared-venv generator', () => {
appTree.read('apps/proj1/pyproject.toml', 'utf-8')
).toMatchSnapshot();
expect(appTree.read('pyproject.toml', 'utf-8')).toMatchSnapshot();
expect(appTree.read('.python-version', 'utf-8')).toMatchSnapshot();
expect(spawnSyncMock).toHaveBeenNthCalledWith(
1,
'poetry',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { PyprojectToml } from '../../graph/dependency-graph';
import chalk from 'chalk';
import { checkPoetryExecutable, runPoetry } from '../../executors/utils/poetry';

async function addFiles(host: Tree) {
async function addFiles(host: Tree, options: Schema) {
const packageJson = await readJsonFile('package.json');

const templateOptions = {
...options,
template: '',
dot: '.',
packageName: packageJson.name,
Expand Down Expand Up @@ -111,7 +112,7 @@ function updateRootPoetryLock() {
async function generator(host: Tree, options: Schema) {
await checkPoetryExecutable();

await addFiles(host);
await addFiles(host, options);
const lockUpdateTasks = updatePyprojectRoot(host, options);
await formatFiles(host);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export interface Schema {
moveDevDependencies: boolean;
pyprojectPythonDependency: string;
pyenvPythonVersion: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"type": "boolean",
"description": "Specifies if migration moves the dev dependencies from the projects to the root pyproject.toml",
"default": true
},
"pyprojectPythonDependency": {
"type": "string",
"description": "Pyproject python dependency version range",
"default": ">=3.8,<3.11"
},
"pyenvPythonVersion": {
"type": "string",
"description": "Pyenv .python-version content",
"default": "3.8.11"
}
},
"required": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ def test_index():
"
`;

exports[`nx-python project generator should generate a python project into a different folder 9`] = `
"3.8.11
"
`;

exports[`nx-python project generator should generate a python project using custom source 1`] = `
"[tool.coverage.run]
branch = true
Expand Down Expand Up @@ -560,6 +565,11 @@ def test_index():
"
`;

exports[`nx-python project generator should successfully generate a python library project 9`] = `
"3.8.11
"
`;

exports[`nx-python project generator should successfully generate a python library project with dev dependencies 1`] = `
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
Expand Down Expand Up @@ -772,6 +782,11 @@ def test_index():
"
`;

exports[`nx-python project generator should successfully generate a python library project with dev dependencies 9`] = `
"3.8.11
"
`;

exports[`nx-python project generator should successfully generate a python library project with root pyproject.toml 1`] = `
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
Expand Down Expand Up @@ -1077,6 +1092,11 @@ def test_index():
"
`;

exports[`nx-python project generator should successfully generate a python project 9`] = `
"3.8.11
"
`;

exports[`nx-python project generator should successfully generate a python project with custom module name 1`] = `
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
Expand Down Expand Up @@ -1272,3 +1292,8 @@ def test_index():
assert index.hello() == "Hello test"
"
`;

exports[`nx-python project generator should successfully generate a python project with custom module name 9`] = `
"3.8.11
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= pyenvPythonVersion %>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ readme = 'README.md'
include = "<%= moduleName %>"

[tool.poetry.dependencies]
python = ">=3.8,<3.10"
python = "<%- pyprojectPythonDependency %>"

<%if (addDevDependencies) { %>
[tool.poetry.group.dev.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-python/src/generators/project/files/tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
isolated_build = True
envlist = py38
envlist = <%= toxEnvlist %>

[testenv]
whitelist_externals =
Expand Down
6 changes: 6 additions & 0 deletions packages/nx-python/src/generators/project/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe('nx-python project generator', () => {
addDevDependencies: false,
buildLockedVersions: true,
buildBundleLocalDependencies: true,
pyprojectPythonDependency: '>=3.8,<3.10',
pyenvPythonVersion: '3.8.11',
toxEnvlist: 'py38',
};

beforeEach(() => {
Expand Down Expand Up @@ -209,4 +212,7 @@ function assertGenerateFiles(
expect(
appTree.read(`${projectDirectory}/tests/test_index.py`).toString()
).toMatchSnapshot();
expect(
appTree.read(`${projectDirectory}/.python-version`).toString()
).toMatchSnapshot();
}
3 changes: 3 additions & 0 deletions packages/nx-python/src/generators/project/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export interface Schema {
sourceUrl?: string;
sourceSecondary?: boolean;
packageName: string;
pyprojectPythonDependency: string;
pyenvPythonVersion: string;
toxEnvlist: string;
moduleName: string;
publishable: boolean;
addDevDependencies: boolean;
Expand Down
15 changes: 15 additions & 0 deletions packages/nx-python/src/generators/project/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
"description": "Python package name",
"x-prompt": "What package name would you like to use?"
},
"pyprojectPythonDependency": {
"type": "string",
"description": "Pyproject python dependency version range",
"default": ">=3.8,<3.11"
},
"pyenvPythonVersion": {
"type": "string",
"description": "Pyenv .python-version content",
"default": "3.8.11"
},
"toxEnvlist": {
"type": "string",
"description": "Tox envlist",
"default": "py38"
},
"moduleName": {
"type": "string",
"description": "Python module name"
Expand Down

0 comments on commit 70d8a3b

Please sign in to comment.