@@ -24,6 +24,43 @@ void test_index_nsec__cleanup(void)
24
24
cl_git_sandbox_cleanup ();
25
25
}
26
26
27
+ static bool try_create_file_with_nsec_timestamp (const char * path )
28
+ {
29
+ struct stat st ;
30
+ int try ;
31
+
32
+ /* retry a few times to avoid nanos *actually* equal 0 race condition */
33
+ for (try = 0 ; try < 3 ; try ++ ) {
34
+ cl_git_mkfile (path , "This is hopefully a file with nanoseconds!" );
35
+
36
+ cl_must_pass (p_stat (path , & st ));
37
+
38
+ if (st .st_ctime_nsec && st .st_mtime_nsec )
39
+ return true;
40
+ }
41
+
42
+ return false;
43
+ }
44
+
45
+ /* try to determine if the underlying filesystem supports a resolution
46
+ * higher than a single second. (i'm looking at you, hfs+)
47
+ */
48
+ static bool should_expect_nsecs (void )
49
+ {
50
+ git_buf nsec_path = GIT_BUF_INIT ;
51
+ bool expect ;
52
+
53
+ git_buf_joinpath (& nsec_path , clar_sandbox_path (), "nsec_test" );
54
+
55
+ expect = try_create_file_with_nsec_timestamp (nsec_path .ptr );
56
+
57
+ p_unlink (nsec_path .ptr );
58
+
59
+ git_buf_clear (& nsec_path );
60
+
61
+ return expect ;
62
+ }
63
+
27
64
static bool has_nsecs (void )
28
65
{
29
66
const git_index_entry * entry ;
@@ -50,8 +87,13 @@ void test_index_nsec__has_nanos(void)
50
87
void test_index_nsec__staging_maintains_other_nanos (void )
51
88
{
52
89
const git_index_entry * entry ;
90
+ bool expect_nsec , test_file_has_nsec ;
91
+
92
+ expect_nsec = should_expect_nsecs ();
93
+ test_file_has_nsec = try_create_file_with_nsec_timestamp ("nsecs/a.txt" );
94
+
95
+ cl_assert_equal_b (expect_nsec , test_file_has_nsec );
53
96
54
- cl_git_rewritefile ("nsecs/a.txt" , "This is file A" );
55
97
cl_git_pass (git_index_add_bypath (repo_index , "a.txt" ));
56
98
cl_git_pass (git_index_write (repo_index ));
57
99
@@ -63,15 +105,15 @@ void test_index_nsec__staging_maintains_other_nanos(void)
63
105
cl_assert ((entry = git_index_get_bypath (repo_index , "a.txt" , 0 )));
64
106
65
107
/* if we are writing nanoseconds to the index, expect them to be
66
- * nonzero. if we are *not*, expect that we truncated the entry.
108
+ * nonzero.
67
109
*/
68
- #ifdef GIT_USE_NSEC
69
- cl_assert (entry -> ctime .nanoseconds != 0 );
70
- cl_assert (entry -> mtime .nanoseconds != 0 );
71
- # else
72
- cl_assert_equal_i (0 , entry -> ctime .nanoseconds );
73
- cl_assert_equal_i (0 , entry -> mtime .nanoseconds );
74
- #endif
110
+ if ( expect_nsec ) {
111
+ cl_assert (entry -> ctime .nanoseconds != 0 );
112
+ cl_assert (entry -> mtime .nanoseconds != 0 );
113
+ } else {
114
+ cl_assert_equal_i (0 , entry -> ctime .nanoseconds );
115
+ cl_assert_equal_i (0 , entry -> mtime .nanoseconds );
116
+ }
75
117
}
76
118
77
119
void test_index_nsec__status_doesnt_clear_nsecs (void )
0 commit comments