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

'hatch version' now displays error message when executed outside proj… #927

Merged
merged 3 commits into from
Nov 26, 2023
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
1 change: 1 addition & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Fix nushell activation
- Better handling of flat storage directory hierarchies for the `virtual` environment type
- Display useful information when running the `version` command outside of a project rather than erroring

## [1.7.0](https://github.com/pypa/hatch/releases/tag/hatch-v1.7.0) - 2023-04-03 ## {: #hatch-v1.7.0 }

Expand Down
15 changes: 14 additions & 1 deletion src/hatch/cli/version/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import click

if TYPE_CHECKING:
from hatch.cli.application import Application


@click.command(short_help="View or set a project's version")
@click.argument('desired_version', required=False)
@click.pass_obj
def version(app, desired_version):
def version(app: Application, desired_version: str | None):
"""View or set a project's version."""
if app.project.root is None:
if app.project.chosen_name is None:
app.abort('No project detected')
else:
app.abort(f'Project {app.project.chosen_name} (not a project)')

if 'version' in app.project.metadata.config.get('project', {}):
if desired_version:
app.abort('Cannot set version when it is statically defined by the `project.version` field')
Expand Down
30 changes: 30 additions & 0 deletions tests/cli/version/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@
from hatchling.utils.constants import DEFAULT_BUILD_SCRIPT, DEFAULT_CONFIG_FILE


class TestNoProject:
def test_random_directory(self, hatch, temp_dir, helpers):
with temp_dir.as_cwd():
result = hatch('version')

assert result.exit_code == 1, result.output
assert result.output == helpers.dedent(
"""
No project detected
"""
)

def test_configured_project(self, hatch, temp_dir, helpers, config_file):
project = 'foo'
config_file.model.mode = 'project'
config_file.model.project = project
config_file.model.projects = {project: str(temp_dir)}
config_file.save()

with temp_dir.as_cwd():
result = hatch('version')

assert result.exit_code == 1, result.output
assert result.output == helpers.dedent(
"""
Project foo (not a project)
"""
)


def test_incompatible_environment(hatch, temp_dir, helpers):
project_name = 'My.App'

Expand Down
Loading