diff --git a/docs/history/hatch.md b/docs/history/hatch.md index 04d4749a8..4408e3405 100644 --- a/docs/history/hatch.md +++ b/docs/history/hatch.md @@ -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 diff --git a/src/hatch/cli/env/show.py b/src/hatch/cli/env/show.py index a93834dc9..8ec89fec1 100644 --- a/src/hatch/cli/env/show.py +++ b/src/hatch/cli/env/show.py @@ -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 @@ -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') diff --git a/tests/cli/env/test_show.py b/tests/cli/env/test_show.py index eba80cac0..e7c3e0956 100644 --- a/tests/cli/env/test_show.py +++ b/tests/cli/env/test_show.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -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() @@ -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):