Skip to content

Commit

Permalink
fix: updated dependency hash (#1169)
Browse files Browse the repository at this point in the history
* fix: updated dependency hash

* adjust tests

* fix

* .

---------

Co-authored-by: Ofek Lev <ofekmeister@gmail.com>
  • Loading branch information
juftin and ofek authored Dec 24, 2023
1 parent 482dead commit 9aa2c70
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

***Fixed:***

- Ensure that the `dependency_hash` method of the `environment` interface is called after `sync_dependencies` for cases where the hash is only known at that point, such as for dependency lockers

## [1.9.0](https://github.com/pypa/hatch/releases/tag/hatch-v1.9.0) - 2023-12-19 ## {: #hatch-v1.9.0 }

***Changed:***
Expand Down
7 changes: 4 additions & 3 deletions src/hatch/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,18 @@ def prepare_environment(self, environment: EnvironmentInterface):
with self.status('Running post-installation commands'):
self.run_shell_commands(environment, environment.post_install_commands, source='post-install')

dep_hash = environment.dependency_hash()
new_dep_hash = environment.dependency_hash()
current_dep_hash = self.env_metadata.dependency_hash(environment)
if dep_hash != current_dep_hash:
if new_dep_hash != current_dep_hash:
with self.status('Checking dependencies'):
dependencies_in_sync = environment.dependencies_in_sync()

if not dependencies_in_sync:
with self.status('Syncing dependencies'):
environment.sync_dependencies()
new_dep_hash = environment.dependency_hash()

self.env_metadata.update_dependency_hash(environment, dep_hash)
self.env_metadata.update_dependency_hash(environment, new_dep_hash)

def run_shell_commands(
self,
Expand Down
46 changes: 45 additions & 1 deletion tests/cli/run/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def test_sync_project_features(hatch, helpers, temp_dir, config_file):


@pytest.mark.requires_internet
def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file):
def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file, mocker):
config_file.model.template.plugins['default']['tests'] = False
config_file.save()

Expand Down Expand Up @@ -507,6 +507,7 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file):
assert output_file.is_file()

assert str(output_file.read_text()) == "(1.0, 'KiB')"
output_file.unlink()

# Now there should be no output because there is no dependency checking
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
Expand All @@ -524,6 +525,49 @@ def test_dependency_hash_checking(hatch, helpers, temp_dir, config_file):
assert output_file.is_file()

assert str(output_file.read_text()) == "(1.0, 'KiB')"
output_file.unlink()

mocker.patch('hatch.env.virtual.VirtualEnvironment.dependencies_in_sync', return_value=False)
mocker.patch('hatch.env.virtual.VirtualEnvironment.dependency_hash', side_effect=['foo', 'bar', 'bar'])

# Ensure that the saved value is the second value
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
result = hatch(
'run',
'python',
'-c',
"import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))",
)

assert result.exit_code == 0, result.output
assert result.output == helpers.dedent(
"""
Checking dependencies
Syncing dependencies
"""
)

output_file = project_path / 'test.txt'
assert output_file.is_file()

assert str(output_file.read_text()) == "(1.0, 'KiB')"
output_file.unlink()

with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
result = hatch(
'run',
'python',
'-c',
"import binary,pathlib,sys;pathlib.Path('test.txt').write_text(str(binary.convert_units(1024)))",
)

assert result.exit_code == 0, result.output
assert not result.output

output_file = project_path / 'test.txt'
assert output_file.is_file()

assert str(output_file.read_text()) == "(1.0, 'KiB')"


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

0 comments on commit 9aa2c70

Please sign in to comment.