lsmem: Fix specified --output columns with JSON print

This commit is contained in:
Foorack 2025-02-09 12:19:40 +01:00
parent 89eeb23806
commit 58b6b85a8f
4 changed files with 46 additions and 17 deletions
Cargo.lockCargo.toml
src/uu/lsmem/src
tests/by-util

23
Cargo.lock generated

@ -258,6 +258,12 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.10"
@ -292,6 +298,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -327,6 +339,16 @@ dependencies = [
"cc",
]
[[package]]
name = "indexmap"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
@ -773,6 +795,7 @@ version = "1.0.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949"
dependencies = [
"indexmap",
"itoa",
"memchr",
"ryu",

@ -51,7 +51,7 @@ phf_codegen = "0.11.2"
rand = { version = "0.9.0", features = ["small_rng"] }
regex = "1.10.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.122"
serde_json = { version = "1.0.122", features = ["preserve_order"] }
sysinfo = "0.33"
tempfile = "3.9.0"
textwrap = { version = "0.16.0", features = ["terminal_size"] }

@ -238,11 +238,6 @@ impl TableRow {
}
}
#[derive(Serialize)]
struct TableRowJson {
memory: Vec<TableRow>,
}
struct Options {
// Set by command-line arguments
all: bool,
@ -575,9 +570,21 @@ fn print_table(lsmem: &Lsmem, opts: &Options) {
}
fn print_json(lsmem: &Lsmem, opts: &Options) {
let table_json = TableRowJson {
memory: create_table_rows(lsmem, opts),
};
let table_rows = create_table_rows(lsmem, opts);
let mut memory_records = Vec::new();
for row in table_rows {
let mut record = serde_json::Map::new();
for column in &opts.columns {
record.insert(
column.get_name().to_lowercase(),
serde_json::Value::String(row.get_value(column)),
);
}
memory_records.push(serde_json::Value::Object(record));
}
let table_json = serde_json::json!({ "memory": memory_records });
let mut table_json_string = serde_json::to_string_pretty(&table_json)
.unwrap()

@ -26,14 +26,13 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}
// FAILS, COMMENT FOR NOW - TODO
// #[test]
// fn test_columns_json() {
// sysroot_test_with_args(
// "test_lsmem_columns_json.expected",
// &["-o", "block,size", "-J"],
// );
// }
#[test]
fn test_columns_json() {
sysroot_test_with_args(
"test_lsmem_columns_json.expected",
&["-o", "block,size", "-J"],
);
}
#[test]
fn test_columns_pairs() {