Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 14, 2024
1 parent b56fd08 commit 92fe8e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
12 changes: 7 additions & 5 deletions src/aiida/cmdline/commands/cmd_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ def computer_export():
def computer_export_setup(computer, output_file):
"""Export computer setup to a yaml file."""
import yaml

computer_setup_data = {
'label': computer.label,
'description': computer.description,
Expand All @@ -757,7 +758,7 @@ def computer_export_setup(computer, output_file):
'default_memory_per_machine': computer.get_default_memory_per_machine(),
'prepend_text': computer.get_prepend_text(),
'append_text': computer.get_append_text(),
'use_double_quotes': computer.get_use_double_quotes()
'use_double_quotes': computer.get_use_double_quotes(),
}
try:
with open(output_file, 'w', encoding='utf-8') as yfhandle:
Expand All @@ -774,16 +775,17 @@ def computer_export_setup(computer, output_file):
def computer_export_config(computer, output_file):
"""Export computer configuration to a yaml file."""
import yaml
from aiida import orm
from aiida.transports import cli as transport_cli


if not computer.is_configured:
echo.echo_error(f"Computer<{computer.pk}> {computer.label} config cannot be exported, because computer has not been configured yet.")
echo.echo_error(
f'Computer<{computer.pk}> {computer.label} config cannot be exported, because computer has not been configured yet.'
)
else:
computer_config_data = computer.get_configuration()
try:
with open(output_file, 'w', encoding='utf-8') as yfhandle:
yaml.dump(computer_config_data, yfhandle)
echo.echo_success(f"Computer<{computer.pk}> {computer.label} config exported to file '{output_file}'.")
except Exception:
raise
raise
43 changes: 28 additions & 15 deletions tests/cmdline/commands/test_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
import tempfile
import textwrap
from collections import OrderedDict
import yaml

import pytest
import yaml
from aiida import orm
from aiida.cmdline.commands.cmd_computer import (
computer_configure,
computer_delete,
computer_duplicate,
computer_export_config,
computer_export_setup,
computer_list,
computer_relabel,
computer_setup,
computer_show,
computer_export_setup,
computer_export_config,
computer_test,
)

Expand Down Expand Up @@ -521,17 +521,23 @@ def test_computer_export_setup(self):
comp = self.comp_builder.new()
comp.store()

exported_setup_filename = "computer-setup.yml"
exported_setup_filename = 'computer-setup.yml'
try:
result = self.cli_runner(computer_export_setup, [comp.label, exported_setup_filename])
assert f"Success" in result.output, "Command should have run successfull."
assert f"{exported_setup_filename}" in result.output, "Filename should be in terminal output but was not found."
assert os.path.exists(exported_setup_filename), f"'{exported_setup_filename}' was not created during export."
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_setup_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_setup_filename
), f"'{exported_setup_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_setup_filename, 'r', encoding='utf-8') as yfhandle:
configure_setup_data = yaml.safe_load(yfhandle)
assert configure_setup_data == self.comp_builder.get_computer_spec(comp), "Internal computer configuration does not agree with exported one."
assert configure_setup_data == self.comp_builder.get_computer_spec(
comp
), 'Internal computer configuration does not agree with exported one.'
except Exception:
raise
finally:
Expand All @@ -545,27 +551,34 @@ def test_computer_export_config(self):
comp = self.comp_builder.new()
comp.store()

exported_config_filename = "computer-configure.yml"
exported_config_filename = 'computer-configure.yml'
try:
result = self.cli_runner(computer_export_config, [comp.label, exported_config_filename])
assert f"Error" in result.output, "Command should have raised an error."
assert 'Error' in result.output, 'Command should have raised an error.'

comp.configure(safe_interval=0.)
comp.configure(safe_interval=0.0)
result = self.cli_runner(computer_export_config, [comp.label, exported_config_filename])
assert f"Success" in result.output, "Command should have run successfull."
assert f"{exported_config_filename}" in result.output, "Filename should be in terminal output but was not found."
assert os.path.exists(exported_config_filename), f"'{exported_config_filename}' was not created during export."
assert 'Success' in result.output, 'Command should have run successfull.'
assert (
f'{exported_config_filename}' in result.output
), 'Filename should be in terminal output but was not found.'
assert os.path.exists(
exported_config_filename
), f"'{exported_config_filename}' was not created during export."

# verifying correctness by comparing internal and loaded yml object
with open(exported_config_filename, 'r', encoding='utf-8') as yfhandle:
configure_config_data = yaml.safe_load(yfhandle)
assert configure_config_data == comp.get_configuration(), "Internal computer configuration does not agree with exported one."
assert (
configure_config_data == comp.get_configuration()
), 'Internal computer configuration does not agree with exported one.'
except Exception:
raise
finally:
if os.path.exists(exported_config_filename):
os.remove(exported_config_filename)


class TestVerdiComputerCommands:
"""Testing verdi computer commands.
Expand Down

0 comments on commit 92fe8e3

Please sign in to comment.