Skip to content

Commit

Permalink
Show internal environments when using the --json flag (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Apr 29, 2024
1 parent 20ca873 commit 6dcb140
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
1 change: 1 addition & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Read configuration from any `~/.pypirc` file for the `index` publisher
- Use the Git user as the default username for new project URL metadata
- Add `HATCH_DEBUG` environment variable that when enabled will show local variables in the case of unhandled tracebacks
- The `env show` command now outputs data about all internal environments when using the `--json` flag
- Upgrade default CPython distributions to 20240415
- Upgrade default PyPy distributions to 7.3.15
- Upgrade Ruff to 0.4.1
Expand Down
79 changes: 40 additions & 39 deletions src/hatch/cli/env/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,43 @@ def show(

from hatch.config.constants import AppEnvVars

if internal:
target_standalone_envs = app.project.config.internal_envs
target_matrices = app.project.config.internal_matrices
else:
target_standalone_envs = app.project.config.envs
target_matrices = app.project.config.matrices

if as_json:
import json

contextual_config = {}
for env_name, config in target_standalone_envs.items():
environment = app.get_environment(env_name)
new_config = contextual_config[env_name] = dict(config)

env_vars = dict(environment.env_vars)
env_vars.pop(AppEnvVars.ENV_ACTIVE)
if env_vars:
new_config['env-vars'] = env_vars

num_dependencies = len(config.get('dependencies', []))
dependencies = environment.environment_dependencies[:num_dependencies]
if dependencies:
new_config['dependencies'] = dependencies

extra_dependencies = environment.environment_dependencies[num_dependencies:]
if extra_dependencies:
new_config['extra-dependencies'] = extra_dependencies

if environment.pre_install_commands:
new_config['pre-install-commands'] = list(
environment.resolve_commands(environment.pre_install_commands)
)

if environment.post_install_commands:
new_config['post-install-commands'] = list(
environment.resolve_commands(environment.post_install_commands)
)

if environment.scripts:
new_config['scripts'] = {
script: list(environment.resolve_commands([script])) for script in environment.scripts
}
for environments in (app.project.config.envs, app.project.config.internal_envs):
for env_name, config in environments.items():
environment = app.get_environment(env_name)
new_config = contextual_config[env_name] = dict(config)

env_vars = dict(environment.env_vars)
env_vars.pop(AppEnvVars.ENV_ACTIVE)
if env_vars:
new_config['env-vars'] = env_vars

num_dependencies = len(config.get('dependencies', []))
dependencies = environment.environment_dependencies[:num_dependencies]
if dependencies:
new_config['dependencies'] = dependencies

extra_dependencies = environment.environment_dependencies[num_dependencies:]
if extra_dependencies:
new_config['extra-dependencies'] = extra_dependencies

if environment.pre_install_commands:
new_config['pre-install-commands'] = list(
environment.resolve_commands(environment.pre_install_commands)
)

if environment.post_install_commands:
new_config['post-install-commands'] = list(
environment.resolve_commands(environment.post_install_commands)
)

if environment.scripts:
new_config['scripts'] = {
script: list(environment.resolve_commands([script])) for script in environment.scripts
}

app.display(json.dumps(contextual_config, separators=(',', ':')))
return
Expand All @@ -80,6 +74,13 @@ def show(

from hatchling.metadata.utils import get_normalized_dependency, normalize_project_name

if internal:
target_standalone_envs = app.project.config.internal_envs
target_matrices = app.project.config.internal_matrices
else:
target_standalone_envs = app.project.config.envs
target_matrices = app.project.config.matrices

for env_name in envs:
if env_name not in target_standalone_envs and env_name not in target_matrices:
app.abort(f'Environment `{env_name}` is not defined by project config')
Expand Down
22 changes: 16 additions & 6 deletions tests/cli/env/test_show.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

import pytest
Expand Down Expand Up @@ -45,7 +46,7 @@ def test_default(hatch, helpers, temp_dir, config_file):
)


def test_default_as_json(hatch, helpers, temp_dir, config_file):
def test_default_as_json(hatch, temp_dir, config_file):
config_file.model.template.plugins['default']['tests'] = False
config_file.save()

Expand All @@ -64,11 +65,20 @@ def test_default_as_json(hatch, helpers, temp_dir, config_file):
result = hatch('env', 'show', '--json')

assert result.exit_code == 0, result.output
assert helpers.remove_trailing_spaces(result.output) == helpers.dedent(
"""
{"default":{"type":"virtual"}}
"""
)

environments = json.loads(result.output)
assert list(environments) == [
'default',
'hatch-build',
'hatch-static-analysis',
'hatch-test.py3.12',
'hatch-test.py3.11',
'hatch-test.py3.10',
'hatch-test.py3.9',
'hatch-test.py3.8',
'hatch-uv',
]
assert environments['default'] == {'type': 'virtual'}


def test_single_only(hatch, helpers, temp_dir, config_file):
Expand Down

0 comments on commit 6dcb140

Please sign in to comment.