diff --git a/src/aiida/cmdline/commands/cmd_code.py b/src/aiida/cmdline/commands/cmd_code.py index e4ff613159..7ed0107fe3 100644 --- a/src/aiida/cmdline/commands/cmd_code.py +++ b/src/aiida/cmdline/commands/cmd_code.py @@ -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 @@ -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() diff --git a/tests/cmdline/commands/test_code.py b/tests/cmdline/commands/test_code.py index e05004a233..a36a7bb28a 100644 --- a/tests/cmdline/commands/test_code.py +++ b/tests/cmdline/commands/test_code.py @@ -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( @@ -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 diff --git a/tests/cmdline/commands/test_code/test_code_export_False_.yml b/tests/cmdline/commands/test_code/test_code_export_False_.yml new file mode 100644 index 0000000000..149df704b0 --- /dev/null +++ b/tests/cmdline/commands/test_code/test_code_export_False_.yml @@ -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 diff --git a/tests/cmdline/commands/test_code/test_code_export_True_.yml b/tests/cmdline/commands/test_code/test_code_export_True_.yml new file mode 100644 index 0000000000..640717a1d2 --- /dev/null +++ b/tests/cmdline/commands/test_code/test_code_export_True_.yml @@ -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'