Skip to content

Commit

Permalink
feat: Extract pipenv contraint from Pipfile first (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Aug 5, 2024
1 parent aacb7c7 commit aeaade8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/manager/pipenv.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,27 @@ describe('pipenv', () => {
const res = await pipenv.getPipenvConstraint('/path/to/folder');
expect(res).toBe('');
});

it('gets pipenv constraint from Pipenv `packages` section', async () => {
mockFs(fs, {
'/path/to/folder/Pipfile': codeBlock`
[packages]
pipenv = "==2020.8.13"
`,
});
const res = await pipenv.getPipenvConstraint('/path/to/folder');
expect(res).toBe('==2020.8.13');
});

it('gets pipenv constraint from Pipenv `dev-packages` section', async () => {
mockFs(fs, {
'/path/to/folder/Pipfile': codeBlock`
[dev-packages]
pipenv = "==2020.8.13"
`,
});
const res = await pipenv.getPipenvConstraint('/path/to/folder');
expect(res).toBe('==2020.8.13');
});
});
});
62 changes: 52 additions & 10 deletions src/manager/pipenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,47 @@ import { readFile } from '../fs';

const PythonConstraintObject = z
.object({
requires: z.object({
python_version: z.string().nullable().catch(null),
python_full_version: z.string().nullable().catch(null),
}),
requires: z
.object({
python_version: z.string().nullable().catch(null),
python_full_version: z.string().nullable().catch(null),
})
.catch({
python_version: null,
python_full_version: null,
}),
packages: z
.object({
pipenv: z.string().nullable(),
})
.catch({ pipenv: null }),
'dev-packages': z
.object({
pipenv: z.string().nullable(),
})
.catch({ pipenv: null }),
})
.transform(
({
requires: {
python_version: pythonVersion,
python_full_version: pythonFullVersion,
},
}) => ({ pythonVersion, pythonFullVersion }),
packages: { pipenv: pipenvDefault },
'dev-packages': { pipenv: pipenvDevelop },
}) => ({
pythonVersion,
pythonFullVersion,
pipenvDefault,
pipenvDevelop,
}),
)
.catch({ pythonVersion: null, pythonFullVersion: null });
.catch({
pythonVersion: null,
pythonFullVersion: null,
pipenvDefault: null,
pipenvDevelop: null,
});

const PythonConstraint = z
.object({
Expand Down Expand Up @@ -118,10 +145,15 @@ const PipenvConstraint = z
}
}

return '';
return null;
},
)
.catch('');
.nullable()
.catch(null);

const PipfilePipenvConstraint = Toml.pipe(PythonConstraintObject)
.pipe(PipenvConstraint)
.catch(null);

export async function getPythonConstraint(
path: string,
Expand Down Expand Up @@ -152,8 +184,18 @@ export async function getPythonConstraint(
}

export async function getPipenvConstraint(path: string): Promise<string> {
const pipfileContent = await readFile(path, 'Pipfile');
const pipenvConstraint = PipfilePipenvConstraint.parse(pipfileContent);
if (pipenvConstraint) {
return pipenvConstraint;
}

const lockfileContent = await readFile(path, 'Pipfile.lock');
const pipenvConstraint =
const pipenvLockfileConstraint =
PipfileLock.pipe(PipenvConstraint).parse(lockfileContent);
return pipenvConstraint;
if (pipenvLockfileConstraint) {
return pipenvLockfileConstraint;
}

return '';
}

0 comments on commit aeaade8

Please sign in to comment.