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

Fixtures: Fix bug in reset of empty_config fixture #5717

Merged
merged 1 commit into from
Oct 24, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Oct 23, 2022

The empty_config fixture creates a completely independent configuration directory which allows test to manipulate the config and its contents, such as the profiles, for the duration of the test without affecting pre-existing config directories.

To accomplish this, the AIIDA_PATH environment variable, which indicates the location of the configuration directory, is set to a temporary directory. The fixture then calls the method aiida.manage.configuration.settings.set_configuration_directory which will set a number of global variables that define important directory and file locations within the configuration folder, for example the location of important daemon files.

The bug was that these global variables weren't reset after the end of the fixture. This means that operations performed by the daemon client, such as checking whether the daemon is running, which rely on these filepaths being set correctly, would fail. The solution therefore is to call set_configuration_directory once again, to revert the global variables to their previous values.

This same problem also occurred in the cache_aiida_path_variable fixture defined and used in tests/manage/configuration/test_config.py. An explicit call after the finally in the fixture is necessary to reset the global variables.

Finally, test_environment_variable_not_set set the value of the DEFAULT_AIIDA_PATH variable to a temporary directory, however, it never changed it back to the default ~. This would essentially cause the set_configuration_directory be ineffective if the AIIDA_PATH variable was never set before running the tests, as then the value of DEFAULT_AIIDA_PATH would be used, which was still the temporary directory of the test. This would break any following tests that rely on the global variables in the settings module to be properly set.

Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @sphuber !

great that you managed to track this down to make the tests more reproducible.

I have a few minor suggestions for further improvement

@@ -229,6 +229,12 @@ def empty_config(tmp_path) -> Config:
configuration.CONFIG = current_config
manager.load_profile(current_profile_name)
Copy link
Member

@ltalirz ltalirz Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by the way, if the goal is strictly to reconstruct the state before this fixture was called, then I guess in principle one should check whether the "current profile" was loaded before or not.

I see that the current Manager implementation does not provide information on whether that was actually the case.
It may make sense to add an

@property
def is_profile_loaded(self):
  return self._profile is not None

property in the Manager class (could also be used inside the manager implementation in some places)

tests/conftest.py Outdated Show resolved Hide resolved
tests/conftest.py Outdated Show resolved Hide resolved
@sphuber sphuber force-pushed the fix/empty-config-fixture-reset branch from bedd77b to 7793c76 Compare October 24, 2022 17:39
@sphuber sphuber requested a review from ltalirz October 24, 2022 17:50
The `empty_config` fixture creates a completely independent
configuration directory which allows test to manipulate the config and
its contents, such as the profiles, for the duration of the test without
affecting pre-existing config directories.

To accomplish this, the `AIIDA_PATH` environment variable, which
indicates the location of the configuration directory, is set to a
temporary directory. The fixture then calls the method
`aiida.manage.configuration.settings.set_configuration_directory` which
will set a number of global variables that define important directory
and file locations within the configuration folder, for example the
location of important daemon files.

The bug was that these global variables weren't reset after the end of
the fixture. This means that operations performed by the daemon client,
such as checking whether the daemon is running, which rely on these
filepaths being set correctly, would fail. The solution therefore is to
call `set_configuration_directory` once again, to revert the global
variables to their previous values.

This same problem also occurred in the `cache_aiida_path_variable`
fixture defined and used in `tests/manage/configuration/test_config.py`.
An explicit call after the `finally` in the fixture is necessary to
reset the global variables.

Finally, `test_environment_variable_not_set` set the value of the
`DEFAULT_AIIDA_PATH` variable to a temporary directory, however, it
never changed it back to the default `~`. This would essentially cause
the `set_configuration_directory` be ineffective if the `AIIDA_PATH`
variable was never set before running the tests, as then the value of
`DEFAULT_AIIDA_PATH` would be used, which was still the temporary
directory of the test. This would break any following tests that rely on
the global variables in the `settings` module to be properly set.
@sphuber sphuber force-pushed the fix/empty-config-fixture-reset branch from 7793c76 to 7fc2338 Compare October 24, 2022 18:00
Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot @sphuber !

looks great to me, as well as easier to follow

aiida/manage/configuration/settings.py Show resolved Hide resolved
@sphuber sphuber merged commit 0258522 into aiidateam:main Oct 24, 2022
@sphuber sphuber deleted the fix/empty-config-fixture-reset branch October 24, 2022 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants