Skip to content

Commit

Permalink
CLI: Add --sort/--no-sort to verdi code export (#6345)
Browse files Browse the repository at this point in the history
When running `verdi code export`, the generated yaml file is
automatically sorted. However, the resulting alphabetical ordering is
not what is typically found in the code yaml files in the
`aiida-code-registry` and `aiida-resource-registry`.

The `--sort/--no-sort` switch is added to allow toggling the
sorting of the keys in the output YAML.
  • Loading branch information
GeigerJ2 committed May 13, 2024
1 parent fb9b6cc commit 80c6068
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
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'

0 comments on commit 80c6068

Please sign in to comment.