1
1
mod checkout {
2
+ mod cache {
3
+ use git_index:: entry:: Mode ;
4
+ use git_worktree:: index:: checkout:: PathCache ;
5
+ use std:: path:: Path ;
6
+ use tempfile:: { tempdir, TempDir } ;
7
+
8
+ #[ test]
9
+ fn root_is_assumed_to_exist_and_files_in_root_do_not_create_directory ( ) {
10
+ let dir = tempdir ( ) . unwrap ( ) ;
11
+ let mut cache = PathCache :: new ( dir. path ( ) . join ( "non-existing-root" ) ) ;
12
+ assert_eq ! ( cache. test_mkdir_calls, 0 ) ;
13
+
14
+ let path = cache
15
+ . append_relative_path_assure_leading_dir ( "hello" , Mode :: FILE )
16
+ . unwrap ( ) ;
17
+ assert ! ( !path. parent( ) . unwrap( ) . exists( ) , "prefix itself is never created" ) ;
18
+ assert_eq ! ( cache. test_mkdir_calls, 0 ) ;
19
+ }
20
+
21
+ #[ test]
22
+ fn directory_paths_are_created_in_full ( ) {
23
+ let ( mut cache, _tmp) = new_cache ( ) ;
24
+
25
+ for ( name, mode) in & [
26
+ ( "dir" , Mode :: DIR ) ,
27
+ ( "submodule" , Mode :: COMMIT ) ,
28
+ ( "file" , Mode :: FILE ) ,
29
+ ( "exe" , Mode :: FILE_EXECUTABLE ) ,
30
+ ( "link" , Mode :: SYMLINK ) ,
31
+ ] {
32
+ let path = cache
33
+ . append_relative_path_assure_leading_dir ( Path :: new ( "dir" ) . join ( name) , * mode)
34
+ . unwrap ( ) ;
35
+ assert ! ( path. parent( ) . unwrap( ) . is_dir( ) , "dir exists" ) ;
36
+ }
37
+
38
+ assert_eq ! ( cache. test_mkdir_calls, 3 ) ;
39
+ }
40
+
41
+ #[ test]
42
+ fn existing_directories_are_fine ( ) {
43
+ let ( mut cache, tmp) = new_cache ( ) ;
44
+ std:: fs:: create_dir ( tmp. path ( ) . join ( "dir" ) ) . unwrap ( ) ;
45
+
46
+ let path = cache
47
+ . append_relative_path_assure_leading_dir ( "dir/file" , Mode :: FILE )
48
+ . unwrap ( ) ;
49
+ assert ! ( path. parent( ) . unwrap( ) . is_dir( ) , "directory is still present" ) ;
50
+ assert ! ( !path. exists( ) , "it won't create the file" ) ;
51
+ }
52
+
53
+ #[ test]
54
+ fn symlinks_or_files_in_path_are_forbidden_or_unlinked_when_forced ( ) {
55
+ let ( mut cache, tmp) = new_cache ( ) ;
56
+ let forbidden = tmp. path ( ) . join ( "forbidden" ) ;
57
+ std:: fs:: create_dir ( & forbidden) . unwrap ( ) ;
58
+ symlink:: symlink_dir ( & forbidden, tmp. path ( ) . join ( "link-to-dir" ) ) . unwrap ( ) ;
59
+ std:: fs:: write ( tmp. path ( ) . join ( "file-in-dir" ) , & [ ] ) . unwrap ( ) ;
60
+
61
+ for dirname in & [ "link-to-dir" , "file-in-dir" ] {
62
+ assert_eq ! (
63
+ cache
64
+ . append_relative_path_assure_leading_dir( format!( "{}/file" , dirname) , Mode :: FILE )
65
+ . unwrap_err( )
66
+ . kind( ) ,
67
+ std:: io:: ErrorKind :: AlreadyExists
68
+ ) ;
69
+ }
70
+ }
71
+
72
+ fn new_cache ( ) -> ( PathCache , TempDir ) {
73
+ let dir = tempdir ( ) . unwrap ( ) ;
74
+ let cache = PathCache :: new ( dir. path ( ) ) ;
75
+ ( cache, dir)
76
+ }
77
+ }
78
+
2
79
#[ cfg( unix) ]
3
80
use std:: os:: unix:: prelude:: MetadataExt ;
4
81
use std:: {
@@ -16,13 +93,14 @@ mod checkout {
16
93
use crate :: fixture_path;
17
94
18
95
#[ test]
19
- fn symlinks_become_files_if_disabled ( ) {
96
+ fn symlinks_become_files_if_disabled ( ) -> crate :: Result {
20
97
let opts = opts_with_symlink ( false ) ;
21
98
let ( source_tree, destination, _index, outcome) =
22
- checkout_index_in_tmp_dir ( opts, "make_mixed_without_submodules" ) . unwrap ( ) ;
99
+ checkout_index_in_tmp_dir ( opts, "make_mixed_without_submodules" ) ? ;
23
100
24
- assert_equality ( & source_tree, & destination, opts. fs . symlink ) . unwrap ( ) ;
101
+ assert_equality ( & source_tree, & destination, opts. fs . symlink ) ? ;
25
102
assert ! ( outcome. collisions. is_empty( ) ) ;
103
+ Ok ( ( ) )
26
104
}
27
105
28
106
#[ test]
0 commit comments