Skip to content

Commit

Permalink
feat: Introduce requirePoetryLockFile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
FinchPowers authored Oct 18, 2022
1 parent 6fbdde1 commit e81d9e1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ custom:
usePoetry: false
```

Be aware that if no `poetry.lock` file is present, a new one will be generated on the fly. To help having predictable builds,
you can set the `requirePoetryLockFile` flag to true to throw an error when `poetry.lock` is missing.

```yaml
custom:
pythonRequirements:
requirePoetryLockFile: false
```

### Poetry with git dependencies

Poetry by default generates the exported requirements.txt file with `-e` and that breaks pip with `-t` parameter
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ServerlessPythonRequirements {
pipCmdExtraArgs: [],
noDeploy: [],
vendor: '',
requirePoetryLockFile: false,
},
(this.serverless.service.custom &&
this.serverless.service.custom.pythonRequirements) ||
Expand Down
26 changes: 21 additions & 5 deletions lib/poetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,28 @@ async function pyprojectTomlToRequirements(modulePath, pluginInstance) {
generateRequirementsProgress = progress.get(
'python-generate-requirements-toml'
);
generateRequirementsProgress.update(
'Generating requirements.txt from "pyproject.toml"'
);
log.info('Generating requirements.txt from "pyproject.toml"');
}

const emitMsg = (msg) => {
if (generateRequirementsProgress) {
generateRequirementsProgress.update(msg);
log.info(msg);
} else {
serverless.cli.log(msg);
}
};

if (fs.existsSync('poetry.lock')) {
emitMsg('Generating requirements.txt from poetry.lock');
} else {
serverless.cli.log('Generating requirements.txt from pyproject.toml...');
if (options.requirePoetryLockFile) {
throw new serverless.classes.Error(
'poetry.lock file not found - set requirePoetryLockFile to false to ' +
'disable this error',
'MISSING_REQUIRED_POETRY_LOCK'
);
}
emitMsg('Generating poetry.lock and requirements.txt from pyproject.toml');
}

try {
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,23 @@ test('py3.7 can ignore functions defined with `image`', async (t) => {

t.end();
});

test('poetry py3.7 fails packaging if poetry.lock is missing and flag requirePoetryLockFile is set to true', async (t) => {
copySync('tests/poetry', 'tests/base with a space');
process.chdir('tests/base with a space');
removeSync('poetry.lock');

const path = npm(['pack', '../..']);
npm(['i', path]);
const stdout = sls(['package'], {
env: { requirePoetryLockFile: 'true', slim: 'true' },
noThrow: true,
});
t.true(
stdout.includes(
'poetry.lock file not found - set requirePoetryLockFile to false to disable this error'
),
'flag works and error is properly reported'
);
t.end();
});
1 change: 1 addition & 0 deletions tests/poetry/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ custom:
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
slimPatternsAppendDefaults: ${env:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
dockerizePip: ${env:dockerizePip, self:custom.defaults.dockerizePip}
requirePoetryLockFile: ${env:requirePoetryLockFile, false}
defaults:
zip: false
slimPatterns: false
Expand Down

0 comments on commit e81d9e1

Please sign in to comment.