Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to install only dev dependencies (#2572) #2917

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ the `--no-dev` option.
poetry install --no-dev
```

Conversely, you can specify to the command that you only want to install the development dependencies
by passing the `--dev-only` option. Note that `--no-dev` takes priority if both options are passed.

```bash
poetry install --dev-only
```

If you want to remove old dependencies no longer present in the lock file, use the
`--remove-untracked` option.

Expand Down Expand Up @@ -142,9 +149,13 @@ If you want to skip this installation, use the `--no-root` option.
poetry install --no-root
```

Installation of your project's package is also skipped when the `--dev-only`
option is passed.

### Options

* `--no-dev`: Do not install dev dependencies.
* `--dev-only`: Only install dev dependencies.
* `--no-root`: Do not install the root package (your project).
* `--dry-run`: Output the operations but do not execute anything (implicitly enables --verbose).
* `--remove-untracked`: Remove dependencies not presented in the lock file
Expand Down
4 changes: 3 additions & 1 deletion poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class InstallCommand(InstallerCommand):

options = [
option("no-dev", None, "Do not install the development dependencies."),
option("dev-only", None, "Only install the development dependencies."),
option(
"no-root", None, "Do not install the root package (the current project)."
),
Expand Down Expand Up @@ -64,6 +65,7 @@ def handle(self):

self._installer.extras(extras)
self._installer.dev_mode(not self.option("no-dev"))
self._installer.dev_only(self.option("dev-only"))
self._installer.dry_run(self.option("dry-run"))
self._installer.remove_untracked(self.option("remove-untracked"))
self._installer.verbose(self._io.is_verbose())
Expand All @@ -73,7 +75,7 @@ def handle(self):
if return_code != 0:
return return_code

if self.option("no-root"):
if self.option("no-root") or self.option("dev-only"):
return 0

try:
Expand Down
12 changes: 12 additions & 0 deletions poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
self._verbose = False
self._write_lock = True
self._dev_mode = True
self._dev_only = False
self._execute_operations = True
self._lock = False

Expand Down Expand Up @@ -136,6 +137,14 @@ def dev_mode(self, dev_mode=True): # type: (bool) -> Installer
def is_dev_mode(self): # type: () -> bool
return self._dev_mode

def dev_only(self, dev_only=False): # type: (bool) -> Installer
self._dev_only = dev_only

return self

def is_dev_only(self): # type: () -> bool
return self._dev_only

def update(self, update=True): # type: (bool) -> Installer
self._update = update

Expand Down Expand Up @@ -270,6 +279,9 @@ def _do_install(self, local_repo):
if not self.is_dev_mode():
root = root.clone()
del root.dev_requires[:]
elif self.is_dev_only():
root = root.clone()
del root.requires[:]

if self._io.is_verbose():
self._io.write_line("")
Expand Down
32 changes: 31 additions & 1 deletion tests/installation/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ def test_run_update_after_removing_dependencies(
assert 1 == installer.executor.removals_count


def test_run_install_no_dev(installer, locker, repo, package, installed):
def _configure_run_install_dev(locker, repo, package, installed):
"""
Perform common test setup for `test_run_install_*dev*()` methods.
"""
locker.locked(True)
locker.mock_lock_data(
{
Expand Down Expand Up @@ -323,7 +326,34 @@ def test_run_install_no_dev(installer, locker, repo, package, installed):
package.add_dependency(Factory.create_dependency("B", "~1.1"))
package.add_dependency(Factory.create_dependency("C", "~1.2", category="dev"))


def test_run_install_no_dev(installer, locker, repo, package, installed):
_configure_run_install_dev(locker, repo, package, installed)

installer.dev_mode(False)
installer.run()

assert 0 == installer.executor.installations_count
assert 0 == installer.executor.updates_count
assert 1 == installer.executor.removals_count


def test_run_install_dev_only(installer, locker, repo, package, installed):
_configure_run_install_dev(locker, repo, package, installed)

installer.dev_only(True)
installer.run()

assert 0 == installer.executor.installations_count
assert 0 == installer.executor.updates_count
assert 2 == installer.executor.removals_count


def test_run_install_no_dev_and_dev_only(installer, locker, repo, package, installed):
_configure_run_install_dev(locker, repo, package, installed)

installer.dev_mode(False)
installer.dev_only(True)
installer.run()

assert 0 == installer.executor.installations_count
Expand Down