@@ -5,7 +5,10 @@ use clap::{builder::PossibleValue, ValueEnum};
55use semver:: VersionReq ;
66
77use super :: Cli ;
8- use crate :: { clang_tools:: clang_tidy:: CompilationUnit , common_fs:: FileFilter } ;
8+ use crate :: {
9+ clang_tools:: clang_tidy:: CompilationUnit ,
10+ common_fs:: { normalize_path, FileFilter } ,
11+ } ;
912
1013#[ derive( Debug , Clone , PartialEq , Eq , Default ) ]
1114pub enum RequestedVersion {
@@ -48,9 +51,20 @@ impl FromStr for RequestedVersion {
4851 input
4952 ) ) ;
5053 }
51- let path = path
52- . canonicalize ( )
53- . map_err ( |e| anyhow ! ( "Failed to canonicalize path '{input}': {e:?}" ) ) ?;
54+ let path = if !path. is_dir ( ) {
55+ path. parent ( )
56+ . ok_or ( anyhow ! (
57+ "Unknown parent directory of the given file path for `--version`: {}" ,
58+ input
59+ ) ) ?
60+ . to_path_buf ( )
61+ } else {
62+ path
63+ } ;
64+ let path = match path. canonicalize ( ) {
65+ Ok ( p) => Ok ( normalize_path ( & p) ) ,
66+ Err ( e) => Err ( anyhow ! ( "Failed to canonicalize path '{input}': {e:?}" ) ) ,
67+ } ?;
5468 Ok ( Self :: Path ( path) )
5569 }
5670 }
@@ -292,6 +306,10 @@ impl Default for FeedbackInput {
292306mod test {
293307 // use crate::cli::get_arg_parser;
294308
309+ use std:: { path:: PathBuf , str:: FromStr } ;
310+
311+ use crate :: { cli:: RequestedVersion , common_fs:: normalize_path} ;
312+
295313 use super :: { Cli , LinesChangedOnly , ThreadComments } ;
296314 use clap:: { Parser , ValueEnum } ;
297315
@@ -332,4 +350,19 @@ mod test {
332350 ThreadComments :: Off
333351 ) ;
334352 }
353+
354+ #[ test]
355+ fn validate_version_path ( ) {
356+ let this_path_str = "src/cli/structs.rs" ;
357+ let this_path = PathBuf :: from ( this_path_str) ;
358+ let this_canonical = this_path. canonicalize ( ) . unwrap ( ) ;
359+ let parent = this_canonical. parent ( ) . unwrap ( ) ;
360+ let expected = normalize_path ( parent) ;
361+ let req_ver = RequestedVersion :: from_str ( this_path_str) . unwrap ( ) ;
362+ if let RequestedVersion :: Path ( parsed) = req_ver {
363+ assert_eq ! ( & parsed, & expected) ;
364+ }
365+
366+ assert ! ( RequestedVersion :: from_str( "file.rs" ) . is_err( ) ) ;
367+ }
335368}
0 commit comments