Skip to content

Commit

Permalink
Merge pull request #2995 from rina23q/improve/2990/tedge-config-retur…
Browse files Browse the repository at this point in the history
…ns-1-when-config-not-set

fix(tedge): `tedge config get` returns 1 when a key is not set
  • Loading branch information
rina23q authored Jul 15, 2024
2 parents 39c4b5b + 4a805f3 commit 180f878
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 2 additions & 0 deletions crates/core/tedge/src/cli/config/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
Expand Down
30 changes: 19 additions & 11 deletions crates/core/tedge/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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(())
Expand All @@ -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([
Expand Down Expand Up @@ -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",
));
Expand Down
4 changes: 2 additions & 2 deletions tests/RobotFramework/tests/tedge/tedge_config_get.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 180f878

Please sign in to comment.