@@ -41,8 +41,17 @@ pub struct SourceCache {
4141impl SourceCache {
4242 pub fn new ( ) -> anyhow:: Result < Self > {
4343 Ok ( Self {
44- cran : Cache :: open ( & format ! ( "source/{CACHE_VERSION}/cran" ) , CRAN_CAPACITY ) ?,
45- r : Cache :: open ( & format ! ( "source/{CACHE_VERSION}/r" ) , R_CAPACITY ) ?,
44+ cran : Cache :: new ( & format ! ( "source/{CACHE_VERSION}/cran" ) , CRAN_CAPACITY ) ?,
45+ r : Cache :: new ( & format ! ( "source/{CACHE_VERSION}/r" ) , R_CAPACITY ) ?,
46+ } )
47+ }
48+
49+ /// Like [`SourceCache::new`], but rooted at an explicit `root` rather than the shared
50+ /// cache directory. Only useful for testing against a temp directory.
51+ pub fn new_in ( root : PathBuf ) -> anyhow:: Result < Self > {
52+ Ok ( Self {
53+ cran : Cache :: new_in ( root. join ( "cran" ) , CRAN_CAPACITY ) ?,
54+ r : Cache :: new_in ( root. join ( "r" ) , R_CAPACITY ) ?,
4655 } )
4756 }
4857
@@ -85,26 +94,15 @@ impl SourceCache {
8594
8695#[ cfg( test) ]
8796mod tests {
88- use oak_cache:: Cache ;
8997 use tempfile:: TempDir ;
9098
9199 use crate :: SourceCache ;
92- use crate :: CRAN_CAPACITY ;
93- use crate :: R_CAPACITY ;
94-
95- /// A `SourceCache` rooted in a temp dir so tests don't touch the real cache.
96- fn source_in ( dir : & TempDir ) -> SourceCache {
97- SourceCache {
98- cran : Cache :: open_in ( dir. path ( ) . join ( "cran" ) , CRAN_CAPACITY ) . unwrap ( ) ,
99- r : Cache :: open_in ( dir. path ( ) . join ( "r" ) , R_CAPACITY ) . unwrap ( ) ,
100- }
101- }
102100
103101 /// Requires internet access
104102 #[ test]
105103 fn test_cran_round_trip ( ) {
106104 let dir = TempDir :: new ( ) . unwrap ( ) ;
107- let source = source_in ( & dir) ;
105+ let source = SourceCache :: new_in ( dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
108106
109107 // Miss before insert
110108 assert_eq ! ( source. get_cran( "vctrs" , "0.7.2" ) , None ) ;
@@ -129,7 +127,7 @@ mod tests {
129127 #[ test]
130128 fn test_cran_not_found ( ) {
131129 let dir = TempDir :: new ( ) . unwrap ( ) ;
132- let source = source_in ( & dir) ;
130+ let source = SourceCache :: new_in ( dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
133131 assert_eq ! (
134132 source. insert_cran( "definitely_not_a_package" , "0.0.0" ) ,
135133 None
@@ -141,7 +139,7 @@ mod tests {
141139 #[ test]
142140 fn test_r_round_trip ( ) {
143141 let dir = TempDir :: new ( ) . unwrap ( ) ;
144- let source = source_in ( & dir) ;
142+ let source = SourceCache :: new_in ( dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
145143
146144 let root = source. insert_r ( "4.5.0" ) . unwrap ( ) ;
147145
@@ -158,7 +156,7 @@ mod tests {
158156 #[ test]
159157 fn test_r_unknown_version ( ) {
160158 let dir = TempDir :: new ( ) . unwrap ( ) ;
161- let source = source_in ( & dir) ;
159+ let source = SourceCache :: new_in ( dir. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
162160 assert_eq ! ( source. insert_r( "0.0.0" ) , None ) ;
163161 }
164162}
0 commit comments