1+ use anyhow:: Context ;
12use cargo_deny:: {
2- PathBuf ,
3+ Path , PathBuf ,
34 diag:: { self , FileId , Files , Severity } ,
45 licenses:: LicenseStore ,
56} ;
@@ -12,6 +13,17 @@ pub(crate) fn load_license_store() -> Result<LicenseStore, anyhow::Error> {
1213 LicenseStore :: from_cache ( )
1314}
1415
16+ pub ( crate ) fn current_dir_utf8 ( ) -> anyhow:: Result < PathBuf > {
17+ let current_dir =
18+ std:: env:: current_dir ( ) . context ( "could not access current working directory" ) ?;
19+
20+ let current_dir = PathBuf :: from_path_buf ( current_dir)
21+ . map_err ( |pb| anyhow:: anyhow!( "path '{}' is not utf-8" , pb. display( ) ) )
22+ . context ( "could not access current working directory" ) ?;
23+
24+ Ok ( current_dir)
25+ }
26+
1527pub struct KrateContext {
1628 pub manifest_path : PathBuf ,
1729 pub workspace : bool ,
@@ -28,41 +40,51 @@ pub struct KrateContext {
2840}
2941
3042impl KrateContext {
31- pub fn get_config_path ( & self , config_path : Option < PathBuf > ) -> Option < PathBuf > {
32- if let Some ( cp) = config_path {
33- if cp. is_absolute ( ) {
34- Some ( cp)
35- } else {
36- Some ( self . manifest_path . parent ( ) . unwrap ( ) . join ( cp) )
37- }
43+ pub fn get_config_path ( & self , config_path : Option < & Path > ) -> anyhow:: Result < Option < PathBuf > > {
44+ if let Some ( config_path) = config_path {
45+ let current_dir = current_dir_utf8 ( ) ?;
46+ Self :: resolve_config_path ( & current_dir, config_path) . map ( Some )
3847 } else {
39- let mut p = self . manifest_path . parent ( ) ;
48+ Ok ( Self :: default_config_path ( & self . manifest_path ) )
49+ }
50+ }
4051
41- while let Some ( parent) = p {
42- let mut config_path = parent. join ( "deny.toml" ) ;
52+ fn resolve_config_path ( current_dir : & Path , config_path : & Path ) -> anyhow:: Result < PathBuf > {
53+ if config_path. is_absolute ( ) {
54+ Ok ( config_path. to_owned ( ) )
55+ } else {
56+ Ok ( current_dir. join ( config_path) )
57+ }
58+ }
4359
44- if config_path. exists ( ) {
45- return Some ( config_path) ;
46- }
60+ fn default_config_path ( manifest_path : & Path ) -> Option < PathBuf > {
61+ let mut p = manifest_path. parent ( ) ;
4762
48- config_path . pop ( ) ;
49- config_path . push ( ". deny.toml") ;
63+ while let Some ( parent ) = p {
64+ let mut config_path = parent . join ( " deny.toml") ;
5065
51- if config_path. exists ( ) {
52- return Some ( config_path) ;
53- }
66+ if config_path. exists ( ) {
67+ return Some ( config_path) ;
68+ }
5469
55- config_path. pop ( ) ;
56- config_path. push ( ".cargo/deny.toml" ) ;
57- if config_path. exists ( ) {
58- return Some ( config_path) ;
59- }
70+ config_path. pop ( ) ;
71+ config_path. push ( ".deny.toml" ) ;
72+
73+ if config_path. exists ( ) {
74+ return Some ( config_path) ;
75+ }
6076
61- p = parent. parent ( ) ;
77+ config_path. pop ( ) ;
78+ config_path. push ( ".cargo/deny.toml" ) ;
79+
80+ if config_path. exists ( ) {
81+ return Some ( config_path) ;
6282 }
6383
64- None
84+ p = parent . parent ( ) ;
6585 }
86+
87+ None
6688 }
6789
6890 pub fn get_local_exceptions_path ( & self ) -> Option < PathBuf > {
@@ -545,3 +567,49 @@ impl<'a> DiagPrinter<'a> {
545567 self . which . lock ( self . max_severity )
546568 }
547569}
570+
571+ #[ cfg( test) ]
572+ mod tests {
573+ use super :: * ;
574+
575+ #[ inline]
576+ fn temp_dir ( ) -> tempfile:: TempDir {
577+ tempfile:: tempdir ( ) . unwrap ( )
578+ }
579+
580+ #[ test]
581+ fn test_resolve_config_path ( ) {
582+ let temp_dir = temp_dir ( ) ;
583+
584+ let current_dir = PathBuf :: from_path_buf ( temp_dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
585+ std:: fs:: File :: create ( current_dir. join ( "deny.toml" ) ) . unwrap ( ) ;
586+
587+ let expected = current_dir. join ( "deny.toml" ) ;
588+
589+ let abs_path = current_dir. join ( "deny.toml" ) ;
590+ let actual = KrateContext :: resolve_config_path ( & current_dir, & abs_path) . unwrap ( ) ;
591+ assert_eq ! ( actual, abs_path) ;
592+
593+ let rel_path = Path :: new ( "./deny.toml" ) ;
594+ let actual = KrateContext :: resolve_config_path ( & current_dir, rel_path) . unwrap ( ) ;
595+ assert_eq ! ( actual, expected) ;
596+ }
597+
598+ #[ test]
599+ fn test_default_config_path ( ) {
600+ let temp_dir = temp_dir ( ) ;
601+
602+ let current_dir = PathBuf :: from_path_buf ( temp_dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
603+ std:: fs:: File :: create ( current_dir. join ( "Cargo.toml" ) ) . unwrap ( ) ;
604+ let manifest_path = current_dir. join ( "Cargo.toml" ) ;
605+
606+ let actual = KrateContext :: default_config_path ( & manifest_path) ;
607+ let expected = None ;
608+ assert_eq ! ( actual, expected) ;
609+
610+ std:: fs:: File :: create ( current_dir. join ( "deny.toml" ) ) . unwrap ( ) ;
611+ let actual = KrateContext :: default_config_path ( & manifest_path) ;
612+ let expected = Some ( current_dir. join ( "deny.toml" ) ) ;
613+ assert_eq ! ( actual, expected) ;
614+ }
615+ }
0 commit comments