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

CLI: Optional sorting for verdi code export #6345

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/aiida/cmdline/commands/cmd_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,14 @@ def show(code):
@verdi_code.command()
@arguments.CODE()
@arguments.OUTPUT_FILE(type=click.Path(exists=False))
@click.option(
'--sort/--no-sort',
is_flag=True,
default=True,
help='Sort the keys of the output YAML.',
)
@with_dbenv()
def export(code, output_file):
def export(code, output_file, sort):
"""Export code to a yaml file."""
import yaml

Expand All @@ -255,7 +261,7 @@ def export(code, output_file):
code_data[key] = str(value)

with open(output_file, 'w', encoding='utf-8') as yfhandle:
yaml.dump(code_data, yfhandle)
yaml.dump(code_data, yfhandle, sort_keys=sort)


@verdi_code.command()
Expand Down
6 changes: 5 additions & 1 deletion tests/cmdline/commands/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ def test_code_duplicate_ignore(run_cli_command, aiida_code_installed, non_intera


@pytest.mark.usefixtures('aiida_profile_clean')
def test_code_export(run_cli_command, aiida_code_installed, tmp_path, file_regression):
@pytest.mark.parametrize('sort', (True, False))
def test_code_export(run_cli_command, aiida_code_installed, tmp_path, file_regression, sort):
"""Test export the code setup to str."""
prepend_text = 'module load something\n some command'
code = aiida_code_installed(
Expand All @@ -254,7 +255,10 @@ def test_code_export(run_cli_command, aiida_code_installed, tmp_path, file_regre
prepend_text=prepend_text,
)
filepath = tmp_path / 'code.yml'

options = [str(code.pk), str(filepath)]
options.append('--sort' if sort else '--no-sort')

run_cli_command(cmd_code.export, options)

# file regression check
Expand Down
8 changes: 8 additions & 0 deletions tests/cmdline/commands/test_code/test_code_export_False_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
label: code
description: ''
default_calc_job_plugin: core.arithmetic.add
use_double_quotes: 'False'
prepend_text: "module load something\n some command"
append_text: ''
computer: localhost
filepath_executable: /bin/cat
8 changes: 8 additions & 0 deletions tests/cmdline/commands/test_code/test_code_export_True_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
append_text: ''
computer: localhost
default_calc_job_plugin: core.arithmetic.add
description: ''
filepath_executable: /bin/cat
label: code
prepend_text: "module load something\n some command"
use_double_quotes: 'False'
Loading