Skip to content

Commit 3d81d6e

Browse files
committed
Fix tests
1 parent 9ae720b commit 3d81d6e

File tree

3 files changed

+82
-42
lines changed

3 files changed

+82
-42
lines changed

src/backends/distrobox/distrobox.rs

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,32 @@ mod tests {
10561056
use super::*;
10571057
use smol::block_on;
10581058

1059+
/// Helper to encode a string as hex (matching the shell script's base16 function)
1060+
fn to_hex(s: &str) -> String {
1061+
s.bytes().map(|b| format!("{:02x}", b)).collect()
1062+
}
1063+
1064+
/// Helper to generate TOML output matching the shell script format
1065+
fn make_desktop_files_toml(
1066+
home_dir: &str,
1067+
system_files: &[(&str, &str)],
1068+
user_files: &[(&str, &str)],
1069+
) -> String {
1070+
let mut toml = format!("home_dir=\"{}\"\n", to_hex(home_dir));
1071+
1072+
toml.push_str("[system]\n");
1073+
for (path, content) in system_files {
1074+
toml.push_str(&format!("\"{}\"=\"{}\"\n", to_hex(path), to_hex(content)));
1075+
}
1076+
1077+
toml.push_str("[user]\n");
1078+
for (path, content) in user_files {
1079+
toml.push_str(&format!("\"{}\"=\"{}\"\n", to_hex(path), to_hex(content)));
1080+
}
1081+
1082+
toml
1083+
}
1084+
10591085
#[test]
10601086
fn list() -> Result<(), Error> {
10611087
block_on(async {
@@ -1098,6 +1124,31 @@ d24405b14180 | ubuntu | Created | ghcr.io/ublue-os/ubun
10981124

10991125
#[test]
11001126
fn list_apps() -> Result<(), Error> {
1127+
let vim_desktop = "[Desktop Entry]
1128+
Type=Application
1129+
Name=Vim
1130+
Exec=/path/to/vim
1131+
Icon=/path/to/icon.png
1132+
Comment=A brief description of my application
1133+
Categories=Utility;Network;";
1134+
1135+
let fish_desktop = "[Desktop Entry]
1136+
Type=Application
1137+
Name=Fish
1138+
Exec=/path/to/fish
1139+
Icon=/path/to/icon.png
1140+
Comment=A brief description of my application
1141+
Categories=Utility;Network;";
1142+
1143+
let desktop_files_toml = make_desktop_files_toml(
1144+
"/home/me",
1145+
&[
1146+
("/usr/share/applications/vim.desktop", vim_desktop),
1147+
("/usr/share/applications/fish.desktop", fish_desktop),
1148+
],
1149+
&[],
1150+
);
1151+
11011152
let db = Distrobox::new(
11021153
NullCommandRunnerBuilder::new()
11031154
.cmd(&["sh", "-c", "echo $XDG_DATA_HOME"], "")
@@ -1116,40 +1167,40 @@ d24405b14180 | ubuntu | Created | ghcr.io/ublue-os/ubun
11161167
"-c",
11171168
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
11181169
],
1119-
"# START FILE /usr/share/applications/vim.desktop
1120-
[Desktop Entry]
1121-
Type=Application
1122-
Name=Vim
1123-
Exec=/path/to/vim
1124-
Icon=/path/to/icon.png
1125-
Comment=A brief description of my application
1126-
Categories=Utility;Network;
1127-
# START FILE /usr/share/applications/fish.desktop
1128-
[Desktop Entry]
1129-
Type=Application
1130-
Name=Fish
1131-
Exec=/path/to/fish
1132-
Icon=/path/to/icon.png
1133-
Comment=A brief description of my application
1134-
Categories=Utility;Network;
1135-
",
1170+
&desktop_files_toml,
11361171
)
11371172
.build(),
11381173
);
11391174

11401175
let apps = block_on(db.list_apps("ubuntu"))?;
1141-
assert_eq!(&apps[0].entry.name, "Vim");
1142-
assert_eq!(&apps[0].entry.exec, "/path/to/vim");
1143-
assert!(apps[0].exported);
1144-
assert_eq!(&apps[1].entry.name, "Fish");
1145-
assert_eq!(&apps[1].entry.exec, "/path/to/fish");
1146-
assert!(!apps[1].exported);
1176+
assert_eq!(&apps[0].entry.name, "Fish");
1177+
assert_eq!(&apps[0].entry.exec, "/path/to/fish");
1178+
assert!(!apps[0].exported);
1179+
assert_eq!(&apps[1].entry.name, "Vim");
1180+
assert_eq!(&apps[1].entry.exec, "/path/to/vim");
1181+
assert!(apps[1].exported);
11471182
Ok(())
11481183
}
11491184

11501185
#[test]
11511186
fn list_apps_with_space_in_filename() -> Result<(), Error> {
11521187
// Simulate a desktop file with a space in its filename and ensure it's parsed/export-detected correctly
1188+
let proton_desktop = "[Desktop Entry]
1189+
Type=Application
1190+
Name=Proton Authenticator
1191+
Exec=/usr/bin/proton-authenticator %u
1192+
Icon=proton-authenticator
1193+
Categories=Utility;Security;";
1194+
1195+
let desktop_files_toml = make_desktop_files_toml(
1196+
"/home/me",
1197+
&[(
1198+
"/usr/share/applications/Proton Authenticator.desktop",
1199+
proton_desktop,
1200+
)],
1201+
&[],
1202+
);
1203+
11531204
let db = Distrobox::new(
11541205
NullCommandRunnerBuilder::new()
11551206
.cmd(&["sh", "-c", "echo $XDG_DATA_HOME"], "")
@@ -1168,13 +1219,7 @@ Categories=Utility;Network;
11681219
"-c",
11691220
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
11701221
],
1171-
"# START FILE /usr/share/applications/Proton Authenticator.desktop
1172-
[Desktop Entry]
1173-
Type=Application
1174-
Name=Proton Authenticator
1175-
Exec=/usr/bin/proton-authenticator %u
1176-
Icon=proton-authenticator
1177-
Categories=Utility;Security;",
1222+
&desktop_files_toml,
11781223
)
11791224
.build(),
11801225
);

src/gtk_utils/typed_list_store.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,8 @@ impl<T: IsA<glib::Object>> DoubleEndedIterator for TypedListStoreIter<T> {
276276
mod tests {
277277
use super::*;
278278

279-
#[test]
279+
#[gtk::test]
280280
fn test_typed_list_store() {
281-
gtk::init().unwrap();
282-
283281
let store = TypedListStore::<gtk::StringObject>::new();
284282
assert_eq!(store.len(), 0);
285283
assert!(store.is_empty());
@@ -306,10 +304,8 @@ mod tests {
306304
assert_eq!(items[1].string(), "Item 2");
307305
}
308306

309-
#[test]
307+
#[gtk::test]
310308
fn test_retain() {
311-
gtk::init().unwrap();
312-
313309
let store = TypedListStore::<gtk::StringObject>::new();
314310
store.append(&gtk::StringObject::new("keep1"));
315311
store.append(&gtk::StringObject::new("remove"));
@@ -322,10 +318,8 @@ mod tests {
322318
assert_eq!(store.get(1).unwrap().string(), "keep2");
323319
}
324320

325-
#[test]
321+
#[gtk::test]
326322
fn test_find_with() {
327-
gtk::init().unwrap();
328-
329323
let store = TypedListStore::<gtk::StringObject>::new();
330324
store.append(&gtk::StringObject::new("first"));
331325
store.append(&gtk::StringObject::new("second"));

src/models/root_store.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ mod tests {
732732
use super::*;
733733
use crate::fakers::NullCommandRunnerBuilder;
734734

735-
#[test]
735+
#[gtk::test]
736736
fn test_resolve_path() {
737737
// (input_path, getfattr_output, expected_resolved_path)
738738
let tests = [
@@ -741,8 +741,9 @@ mod tests {
741741
Ok("/home/user/Documents/custom-home-folder"),
742742
Ok("/home/user/Documents/custom-home-folder"),
743743
),
744+
// When getfattr returns empty for a non-sandbox path, we return the original path
744745
("/home/user/Documents/custom-home-folder", Ok(""), {
745-
Ok("/home/user/Documents/custom/home/folder")
746+
Ok("/home/user/Documents/custom-home-folder")
746747
}),
747748
// If the resolution fails and the path is from a sandbox, we expect an error
748749
("/run/user/1000/doc/xyz456", Err(()), Err(())),
@@ -765,7 +766,7 @@ mod tests {
765766
if let Ok(expected_resolved_path) = expected_resolved_path {
766767
assert_eq!(resolved_path.unwrap(), expected_resolved_path);
767768
} else {
768-
assert!(expected_resolved_path.is_err());
769+
assert!(resolved_path.is_err());
769770
}
770771
}
771772
}

0 commit comments

Comments
 (0)