@@ -98,24 +98,55 @@ fn directory() {
98
98
assert ! ( git:: get_diff_base( & dir) . is_err( ) ) ;
99
99
}
100
100
101
- /// Test that `get_file_head` does not return content for a symlink.
102
- /// This is important to correctly cover cases where a symlink is removed and replaced by a file.
103
- /// If the contents of the symlink object were returned a diff between a path and the actual file would be produced (bad ui).
101
+ /// Test that `get_diff_base` resolves symlinks so that the same diff base is
102
+ /// used as the target file.
103
+ ///
104
+ /// This is important to correctly cover cases where a symlink is removed and
105
+ /// replaced by a file. If the contents of the symlink object were returned
106
+ /// a diff between a literal file path and the actual file content would be
107
+ /// produced (bad ui).
104
108
#[ cfg( any( unix, windows) ) ]
105
109
#[ test]
106
110
fn symlink ( ) {
107
111
#[ cfg( unix) ]
108
112
use std:: os:: unix:: fs:: symlink;
109
113
#[ cfg( not( unix) ) ]
110
114
use std:: os:: windows:: fs:: symlink_file as symlink;
115
+
111
116
let temp_git = empty_git_repo ( ) ;
112
117
let file = temp_git. path ( ) . join ( "file.txt" ) ;
113
- let contents = b"foo" . as_slice ( ) ;
114
- File :: create ( & file) . unwrap ( ) . write_all ( contents) . unwrap ( ) ;
118
+ let contents = Vec :: from ( b"foo" ) ;
119
+ File :: create ( & file) . unwrap ( ) . write_all ( & contents) . unwrap ( ) ;
115
120
let file_link = temp_git. path ( ) . join ( "file_link.txt" ) ;
121
+
116
122
symlink ( "file.txt" , & file_link) . unwrap ( ) ;
123
+ create_commit ( temp_git. path ( ) , true ) ;
124
+
125
+ assert_eq ! ( git:: get_diff_base( & file_link) . unwrap( ) , contents) ;
126
+ assert_eq ! ( git:: get_diff_base( & file) . unwrap( ) , contents) ;
127
+ }
128
+
129
+ /// Test that `get_diff_base` returns content when the file is a symlink to
130
+ /// another file that is in a git repo, but the symlink itself is not.
131
+ #[ cfg( any( unix, windows) ) ]
132
+ #[ test]
133
+ fn symlink_to_git_repo ( ) {
134
+ #[ cfg( unix) ]
135
+ use std:: os:: unix:: fs:: symlink;
136
+ #[ cfg( not( unix) ) ]
137
+ use std:: os:: windows:: fs:: symlink_file as symlink;
138
+
139
+ let temp_dir = tempfile:: tempdir ( ) . expect ( "create temp dir" ) ;
140
+ let temp_git = empty_git_repo ( ) ;
117
141
142
+ let file = temp_git. path ( ) . join ( "file.txt" ) ;
143
+ let contents = Vec :: from ( b"foo" ) ;
144
+ File :: create ( & file) . unwrap ( ) . write_all ( & contents) . unwrap ( ) ;
118
145
create_commit ( temp_git. path ( ) , true ) ;
119
- assert ! ( git:: get_diff_base( & file_link) . is_err( ) ) ;
120
- assert_eq ! ( git:: get_diff_base( & file) . unwrap( ) , Vec :: from( contents) ) ;
146
+
147
+ let file_link = temp_dir. path ( ) . join ( "file_link.txt" ) ;
148
+ symlink ( & file, & file_link) . unwrap ( ) ;
149
+
150
+ assert_eq ! ( git:: get_diff_base( & file_link) . unwrap( ) , contents) ;
151
+ assert_eq ! ( git:: get_diff_base( & file) . unwrap( ) , contents) ;
121
152
}
0 commit comments