1
1
use crate :: config;
2
+ #[ cfg( test) ]
3
+ use crate :: fixture;
4
+ use crate :: subcommand:: RadSubCmdRunnable ;
2
5
use crate :: utils;
3
6
use anyhow:: { Context , Result } ;
4
7
use rualdlib:: Aliases ;
8
+ #[ cfg( test) ]
9
+ use serial_test:: serial;
5
10
use std:: ffi:: OsStr ;
6
11
use std:: path:: { Path , PathBuf } ;
12
+ #[ cfg( test) ]
13
+ use std:: str:: FromStr ;
7
14
use structopt:: StructOpt ;
8
15
9
16
/// Resolve alias
@@ -13,7 +20,7 @@ pub struct Resolve {
13
20
pub path : PathBuf ,
14
21
}
15
22
16
- impl super :: RadSubCmdRunnable for Resolve {
23
+ impl RadSubCmdRunnable for Resolve {
17
24
fn run ( & self ) -> Result < String > {
18
25
let aliases_dir = config:: rad_aliases_dir ( )
19
26
. with_context ( || format ! ( "fail to resolve alias path '{}'" , self . path. display( ) ) ) ?;
@@ -62,3 +69,68 @@ fn resolve_alias<P: AsRef<Path>>(path: P, aliases: Aliases) -> Result<PathBuf> {
62
69
} ;
63
70
Ok ( result)
64
71
}
72
+
73
+ #[ cfg( test) ]
74
+ mod tests {
75
+ use super :: * ;
76
+
77
+ #[ test]
78
+ #[ serial]
79
+ fn existing_alias ( ) {
80
+ let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
81
+ let mut subcmd = fixture:: create_subcmd ( Resolve {
82
+ path : PathBuf :: from_str ( "test" ) . unwrap ( ) ,
83
+ } ) ;
84
+ subcmd. use_config ( toml:: toml![ test = "not-existing-path" ] ) ;
85
+ let res = subcmd. run ( ) ;
86
+ let expected = format ! (
87
+ "could not resolve path: {}/not-existing-path" ,
88
+ current_dir. to_str( ) . unwrap( )
89
+ ) ;
90
+ assert ! ( res. is_err( ) ) ;
91
+ assert_eq ! ( res. unwrap_err( ) . to_string( ) , expected) ;
92
+ }
93
+
94
+ #[ test]
95
+ #[ serial]
96
+ fn existing_path_without_alias ( ) {
97
+ let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
98
+ let subcmd = fixture:: create_subcmd ( Resolve {
99
+ path : PathBuf :: from_str ( current_dir. to_str ( ) . unwrap ( ) ) . unwrap ( ) ,
100
+ } ) ;
101
+ let res = subcmd. run ( ) ;
102
+ let expected = format ! ( "{}\n " , current_dir. to_str( ) . unwrap( ) ) ;
103
+ assert ! ( res. is_ok( ) ) ;
104
+ assert_eq ! ( res. unwrap( ) , expected) ;
105
+ }
106
+
107
+ #[ test]
108
+ #[ serial]
109
+ fn not_existing_path_without_alias ( ) {
110
+ let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
111
+ let subcmd = fixture:: create_subcmd ( Resolve {
112
+ path : PathBuf :: from_str ( "test" ) . unwrap ( ) ,
113
+ } ) ;
114
+ let res = subcmd. run ( ) ;
115
+ let expected = format ! (
116
+ "could not resolve path: {}/test" ,
117
+ current_dir. to_str( ) . unwrap( )
118
+ ) ;
119
+ assert ! ( res. is_err( ) ) ;
120
+ assert_eq ! ( res. unwrap_err( ) . to_string( ) , expected) ;
121
+ }
122
+
123
+ #[ test]
124
+ #[ serial]
125
+ fn tild_alias ( ) {
126
+ let home_dir = std:: env:: var ( "HOME" ) . unwrap ( ) ;
127
+ let mut subcmd = fixture:: create_subcmd ( Resolve {
128
+ path : PathBuf :: from_str ( "home" ) . unwrap ( ) ,
129
+ } ) ;
130
+ subcmd. use_config ( toml:: toml![ home = "~" ] ) ;
131
+ let res = subcmd. run ( ) ;
132
+ let expected = format ! ( "{}\n " , home_dir) ;
133
+ assert ! ( res. is_ok( ) ) ;
134
+ assert_eq ! ( res. unwrap( ) , expected) ;
135
+ }
136
+ }
0 commit comments