Skip to content

Commit 896a67f

Browse files
committed
Rewrite app listing script to ensure it handles files with spaces
fixes #41
1 parent cdd9ac6 commit 896a67f

File tree

1 file changed

+82
-36
lines changed

1 file changed

+82
-36
lines changed

src/distrobox/mod.rs

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ mod desktop_file;
1818

1919
pub use desktop_file::*;
2020

21+
const POSIX_FIND_AND_CONCAT_DESKTOP_FILES: &str = "grep -L '^[[:space:]]*NoDisplay[[:space:]]*=[[:space:]]*true[[:space:]]*$' /usr/share/applications/*.desktop 2>/dev/null | while IFS= read -r file; do printf '# START FILE %s\n' \"$file\"; cat \"$file\"; done";
22+
2123
#[derive(Clone)]
2224
pub struct FlatpakCommandRunner {
2325
pub command_runner: Rc<dyn InnerCommandRunner>,
@@ -437,9 +439,18 @@ impl DistroboxCommandRunnerResponse {
437439
));
438440
}
439441
commands.push((
440-
Command::new_with_args("distrobox",
441-
["enter", box_name, "--", "sh", "-c", "for file in $(grep --files-without-match \"NoDisplay=true\" /usr/share/applications/*.desktop); do echo \"# START FILE $file\"; cat \"$file\"; done"]),
442-
contents
442+
Command::new_with_args(
443+
"distrobox",
444+
[
445+
"enter",
446+
box_name,
447+
"--",
448+
"sh",
449+
"-c",
450+
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
451+
],
452+
),
453+
contents,
443454
));
444455

445456
commands
@@ -604,7 +615,7 @@ impl Distrobox {
604615
"--",
605616
"sh",
606617
"-c",
607-
"for file in $(grep --files-without-match \"NoDisplay=true\" /usr/share/applications/*.desktop); do echo \"# START FILE $file\"; cat \"$file\"; done",
618+
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
608619
]);
609620
let concatenated_files = self.cmd_output_string(cmd).await?;
610621
debug!(concatenated_files = concatenated_files);
@@ -922,36 +933,25 @@ d24405b14180 | ubuntu | Created | ghcr.io/ublue-os/ubun
922933

923934
#[test]
924935
fn list_apps() -> Result<(), Error> {
925-
let db = Distrobox::new(NullCommandRunnerBuilder::new()
926-
.cmd(
927-
&[
928-
"sh", "-c", "echo $XDG_DATA_HOME"
929-
],
930-
""
931-
)
932-
.cmd(
933-
&[
934-
"sh", "-c", "echo $HOME"
935-
],
936-
"/home/me"
937-
)
938-
.cmd(
939-
&[
940-
"ls", "/home/me/.local/share/applications"
941-
],
942-
"ubuntu-vim.desktop\n"
943-
)
944-
.cmd(
945-
&[
946-
"distrobox",
947-
"enter",
948-
"ubuntu",
949-
"--",
950-
"sh",
951-
"-c",
952-
"for file in $(grep --files-without-match \"NoDisplay=true\" /usr/share/applications/*.desktop); do echo \"# START FILE $file\"; cat \"$file\"; done",
953-
],
954-
"# START FILE /usr/share/applications/vim.desktop
936+
let db = Distrobox::new(
937+
NullCommandRunnerBuilder::new()
938+
.cmd(&["sh", "-c", "echo $XDG_DATA_HOME"], "")
939+
.cmd(&["sh", "-c", "echo $HOME"], "/home/me")
940+
.cmd(
941+
&["ls", "/home/me/.local/share/applications"],
942+
"ubuntu-vim.desktop\n",
943+
)
944+
.cmd(
945+
&[
946+
"distrobox",
947+
"enter",
948+
"ubuntu",
949+
"--",
950+
"sh",
951+
"-c",
952+
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
953+
],
954+
"# START FILE /usr/share/applications/vim.desktop
955955
[Desktop Entry]
956956
Type=Application
957957
Name=Vim
@@ -967,8 +967,9 @@ Exec=/path/to/fish
967967
Icon=/path/to/icon.png
968968
Comment=A brief description of my application
969969
Categories=Utility;Network;
970-
",)
971-
.build(),
970+
",
971+
)
972+
.build(),
972973
);
973974

974975
let apps = block_on(db.list_apps("ubuntu"))?;
@@ -980,6 +981,51 @@ Categories=Utility;Network;
980981
assert!(!apps[1].exported);
981982
Ok(())
982983
}
984+
985+
#[test]
986+
fn list_apps_with_space_in_filename() -> Result<(), Error> {
987+
// Simulate a desktop file with a space in its filename and ensure it's parsed/export-detected correctly
988+
let db = Distrobox::new(
989+
NullCommandRunnerBuilder::new()
990+
.cmd(&["sh", "-c", "echo $XDG_DATA_HOME"], "")
991+
.cmd(&["sh", "-c", "echo $HOME"], "/home/me")
992+
.cmd(
993+
&["ls", "/home/me/.local/share/applications"],
994+
"ubuntu-Proton Authenticator.desktop\n",
995+
)
996+
.cmd(
997+
&[
998+
"distrobox",
999+
"enter",
1000+
"ubuntu",
1001+
"--",
1002+
"sh",
1003+
"-c",
1004+
POSIX_FIND_AND_CONCAT_DESKTOP_FILES,
1005+
],
1006+
"# START FILE /usr/share/applications/Proton Authenticator.desktop
1007+
[Desktop Entry]
1008+
Type=Application
1009+
Name=Proton Authenticator
1010+
Exec=/usr/bin/proton-authenticator %u
1011+
Icon=proton-authenticator
1012+
Categories=Utility;Security;",
1013+
)
1014+
.build(),
1015+
);
1016+
1017+
let apps = block_on(db.list_apps("ubuntu"))?;
1018+
assert_eq!(apps.len(), 1);
1019+
assert_eq!(&apps[0].entry.name, "Proton Authenticator");
1020+
assert_eq!(&apps[0].entry.exec, "/usr/bin/proton-authenticator %u");
1021+
assert_eq!(
1022+
&apps[0].desktop_file_path,
1023+
"/usr/share/applications/Proton Authenticator.desktop"
1024+
);
1025+
// Ensure exported detection matches the filename with space
1026+
assert!(apps[0].exported);
1027+
Ok(())
1028+
}
9831029
#[test]
9841030
fn create() -> Result<(), Error> {
9851031
let _ = tracing_subscriber::fmt().with_test_writer().try_init();

0 commit comments

Comments
 (0)