From 4a805f3744b74b60ee1d77fd3728557e499efe8e Mon Sep 17 00:00:00 2001 From: Rina Fujino Date: Fri, 12 Jul 2024 14:40:25 +0000 Subject: [PATCH] fix(tedge): `tedge config get` returns 1 when a key is not set Signed-off-by: Rina Fujino --- .../core/tedge/src/cli/config/commands/get.rs | 2 ++ crates/core/tedge/tests/main.rs | 30 ++++++++++++------- .../tests/tedge/tedge_config_get.robot | 4 +-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/core/tedge/src/cli/config/commands/get.rs b/crates/core/tedge/src/cli/config/commands/get.rs index bf6013b32b6..7fca19aedec 100644 --- a/crates/core/tedge/src/cli/config/commands/get.rs +++ b/crates/core/tedge/src/cli/config/commands/get.rs @@ -19,9 +19,11 @@ impl Command for GetConfigCommand { } Err(tedge_config::ReadError::ConfigNotSet { .. }) => { eprintln!("The provided config key: '{}' is not set", self.key); + std::process::exit(1) } Err(tedge_config::ReadError::ReadOnlyNotFound { message, key }) => { eprintln!("The provided config key: '{key}' is not configured: {message}",); + std::process::exit(1) } Err(err) => return Err(err.into()), } diff --git a/crates/core/tedge/tests/main.rs b/crates/core/tedge/tests/main.rs index d78bd53169d..04773a3c8ed 100644 --- a/crates/core/tedge/tests/main.rs +++ b/crates/core/tedge/tests/main.rs @@ -88,7 +88,7 @@ mod tests { // We start we no certificate, hence no device id get_device_id_cmd .assert() - .success() + .failure() .stderr(predicate::str::contains("'device.id' is not configured")); // The create command created a certificate @@ -124,7 +124,7 @@ mod tests { // The remove command also removed the device id from the config get_device_id_cmd .assert() - .success() + .failure() .stderr(predicate::str::contains("device.id")); // The a new certificate can then be created. @@ -152,7 +152,7 @@ mod tests { get_device_id_cmd .assert() - .success() + .failure() .stderr(predicate::str::contains("device.id")); // forbidden to set read-only key by CLI @@ -207,12 +207,16 @@ mod tests { config_key, ])?; - let get_config_command = get_config_command.assert().success(); + let get_config_command = get_config_command.assert(); if is_default { - get_config_command.stdout(predicate::str::contains(default_value_or_error_message)); + get_config_command + .stdout(predicate::str::contains(default_value_or_error_message)) + .success(); } else { - get_config_command.stderr(predicate::str::contains(default_value_or_error_message)); + get_config_command + .stderr(predicate::str::contains(default_value_or_error_message)) + .failure(); } let mut set_config_command = tedge_command_with_test_home([ @@ -257,12 +261,16 @@ mod tests { config_key, ])?; - let get_config_command = get_config_command.assert().success(); + let get_config_command = get_config_command.assert(); if is_default { - get_config_command.stdout(predicate::str::contains(default_value_or_error_message)); + get_config_command + .stdout(predicate::str::contains(default_value_or_error_message)) + .success(); } else { - get_config_command.stderr(predicate::str::contains(default_value_or_error_message)); + get_config_command + .stderr(predicate::str::contains(default_value_or_error_message)) + .failure(); } Ok(()) @@ -287,7 +295,7 @@ mod tests { get_device_id_cmd .assert() - .success() + .failure() .stderr(predicate::str::contains("device.id")); let mut get_cert_path_cmd = tedge_command_with_test_home([ @@ -326,7 +334,7 @@ mod tests { get_c8y_url_cmd .assert() - .success() + .failure() .stderr(predicate::str::contains( "The provided config key: 'c8y.url' is not set", )); diff --git a/tests/RobotFramework/tests/tedge/tedge_config_get.robot b/tests/RobotFramework/tests/tedge/tedge_config_get.robot index f6aea040f4c..015ddda8038 100644 --- a/tests/RobotFramework/tests/tedge/tedge_config_get.robot +++ b/tests/RobotFramework/tests/tedge/tedge_config_get.robot @@ -14,10 +14,10 @@ Set keys should return value on stdout Should Be Equal ${output} ${DEVICE_SN} Unset keys should not return anything on stdout and warnings on stderr - ${output}= Execute Command tedge config get az.url 2>/dev/null exp_exit_code=0 + ${output}= Execute Command tedge config get az.url 2>/dev/null exp_exit_code=1 Should Be Empty ${output} - ${stderr}= Execute Command tedge config get az.url 2>&1 >/dev/null exp_exit_code=0 + ${stderr}= Execute Command tedge config get az.url 2>&1 >/dev/null exp_exit_code=1 Should Not Be Empty ${stderr} Invalid keys should not return anything on stdout and warnings on stderr