@@ -396,7 +396,7 @@ impl RootStore {
396396 } ) ?)
397397 }
398398
399- pub async fn is_nvidia_host ( & self ) -> Result < bool , distrobox :: Error > {
399+ pub async fn is_nvidia_host ( & self ) -> bool {
400400 // uses lspci to check if the host has an NVIDIA GPU
401401 debug ! ( "Checking if host is NVIDIA" ) ;
402402 let cmd = Command :: new ( "lspci" ) ;
@@ -405,15 +405,27 @@ impl RootStore {
405405 Ok ( output) => {
406406 let is_nvidia = output. contains ( "NVIDIA" ) || output. contains ( "nVidia" ) ;
407407 debug ! ( is_nvidia, "Checked if host is NVIDIA" ) ;
408- Ok ( is_nvidia)
408+ is_nvidia
409409 }
410410 Err ( e) => {
411411 debug ! ( ?e, "Failed to check if host is NVIDIA" ) ;
412- Ok ( false ) // If we can't run lspci, we assume it's not NVIDIA
412+ false // If we can't run lspci, we assume it's not NVIDIA
413413 }
414414 }
415415 }
416416
417+ fn getfattr_cmd ( path : & str ) -> Command {
418+ Command :: new_with_args (
419+ "getfattr" ,
420+ [
421+ "-n" ,
422+ "user.document-portal.host-path" ,
423+ "--only-values" ,
424+ path,
425+ ] ,
426+ )
427+ }
428+
417429 pub async fn resolve_host_path ( & self , path : & str ) -> Result < String , distrobox:: Error > {
418430 // The path could be a:
419431 // 1. Host path, already resolved to a real location, e.g., "/home/user/Documents/custom-home-folder".
@@ -425,15 +437,7 @@ impl RootStore {
425437
426438 debug ! ( ?path, "Resolving host path" ) ;
427439
428- let cmd = Command :: new_with_args (
429- "getfattr" ,
430- [
431- "-n" ,
432- "user.document-portal.host-path" ,
433- "--only-values" ,
434- path,
435- ] ,
436- ) ;
440+ let cmd = Self :: getfattr_cmd ( path) ;
437441 let output = self
438442 . run_to_string ( cmd)
439443 . await
@@ -470,3 +474,49 @@ impl Default for RootStore {
470474 glib:: Object :: builder ( ) . build ( )
471475 }
472476}
477+
478+ #[ cfg( test) ]
479+ mod tests {
480+ use std:: io;
481+
482+ use super :: * ;
483+ use crate :: fakers:: NullCommandRunnerBuilder ;
484+
485+ #[ test]
486+ fn test_resolve_path ( ) {
487+ // (input_path, getfattr_output, expected_resolved_path)
488+ let tests = [
489+ (
490+ "/run/user/1000/doc/abc123" ,
491+ Ok ( "/home/user/Documents/custom-home-folder" ) ,
492+ Ok ( "/home/user/Documents/custom-home-folder" ) ,
493+ ) ,
494+ ( "/home/user/Documents/custom-home-folder" , Ok ( "" ) , {
495+ Ok ( "/home/user/Documents/custom-home-folder" )
496+ } ) ,
497+ // If the resolution fails and the path is from a sandbox, we expect an error
498+ ( "/run/user/1000/doc/xyz456" , Err ( ( ) ) , Err ( ( ) ) ) ,
499+ ] ;
500+
501+ for ( input_path, getfattr_output, expected_resolved_path) in tests {
502+ let runner = NullCommandRunnerBuilder :: new ( )
503+ . cmd_full ( RootStore :: getfattr_cmd ( input_path) , move || {
504+ getfattr_output
505+ . map ( |s| s. to_string ( ) )
506+ // we need to return a real io::Error here
507+ . map_err ( |_| io:: Error :: new ( io:: ErrorKind :: NotFound , "Command not found" ) )
508+ } )
509+ . build ( ) ;
510+ let store = RootStore :: new ( runner) ;
511+
512+ let resolved_path: Result < String , distrobox:: Error > =
513+ smol:: block_on ( store. resolve_host_path ( input_path) ) ;
514+
515+ if let Ok ( expected_resolved_path) = expected_resolved_path {
516+ assert_eq ! ( resolved_path. unwrap( ) , expected_resolved_path) ;
517+ } else {
518+ assert ! ( expected_resolved_path. is_err( ) ) ;
519+ }
520+ }
521+ }
522+ }
0 commit comments