@@ -24,13 +24,17 @@ const common = require('../common');
24
24
const assert = require ( 'assert' ) ;
25
25
const util = require ( 'util' ) ;
26
26
const fs = require ( 'fs' ) ;
27
+ const url = require ( 'url' ) ;
27
28
28
29
const tmpdir = require ( '../common/tmpdir' ) ;
29
30
tmpdir . refresh ( ) ;
30
31
31
- function stat_resource ( resource ) {
32
+ const lpath = `${ tmpdir . path } /symlink` ;
33
+ fs . symlinkSync ( 'unoent-entry' , lpath ) ;
34
+
35
+ function stat_resource ( resource , statSync = fs . statSync ) {
32
36
if ( typeof resource === 'string' ) {
33
- return fs . statSync ( resource ) ;
37
+ return statSync ( resource ) ;
34
38
}
35
39
const stats = fs . fstatSync ( resource ) ;
36
40
// Ensure mtime has been written to disk
@@ -41,9 +45,9 @@ function stat_resource(resource) {
41
45
return fs . fstatSync ( resource ) ;
42
46
}
43
47
44
- function check_mtime ( resource , mtime ) {
48
+ function check_mtime ( resource , mtime , statSync ) {
45
49
mtime = fs . _toUnixTimestamp ( mtime ) ;
46
- const stats = stat_resource ( resource ) ;
50
+ const stats = stat_resource ( resource , statSync ) ;
47
51
const real_mtime = fs . _toUnixTimestamp ( stats . mtime ) ;
48
52
return mtime - real_mtime ;
49
53
}
@@ -55,8 +59,8 @@ function expect_errno(syscall, resource, err, errno) {
55
59
) ;
56
60
}
57
61
58
- function expect_ok ( syscall , resource , err , atime , mtime ) {
59
- const mtime_diff = check_mtime ( resource , mtime ) ;
62
+ function expect_ok ( syscall , resource , err , atime , mtime , statSync ) {
63
+ const mtime_diff = check_mtime ( resource , mtime , statSync ) ;
60
64
assert (
61
65
// Check up to single-second precision.
62
66
// Sub-second precision is OS and fs dependant.
@@ -68,45 +72,55 @@ function expect_ok(syscall, resource, err, atime, mtime) {
68
72
69
73
const stats = fs . statSync ( tmpdir . path ) ;
70
74
75
+ const asPath = ( path ) => path ;
76
+ const asUrl = ( path ) => url . pathToFileURL ( path ) ;
77
+
71
78
const cases = [
72
- new Date ( '1982-09-10 13:37' ) ,
73
- new Date ( ) ,
74
- 123456.789 ,
75
- stats . mtime ,
76
- [ '123456' , - 1 ] ,
77
- new Date ( '2017-04-08T17:59:38.008Z' )
79
+ [ asPath , new Date ( '1982-09-10 13:37' ) ] ,
80
+ [ asPath , new Date ( ) ] ,
81
+ [ asPath , 123456.789 ] ,
82
+ [ asPath , stats . mtime ] ,
83
+ [ asPath , '123456' , - 1 ] ,
84
+ [ asPath , new Date ( '2017-04-08T17:59:38.008Z' ) ] ,
85
+ [ asUrl , new Date ( ) ] ,
78
86
] ;
87
+
79
88
runTests ( cases . values ( ) ) ;
80
89
81
90
function runTests ( iter ) {
82
91
const { value, done } = iter . next ( ) ;
83
92
if ( done ) return ;
93
+
84
94
// Support easy setting same or different atime / mtime values.
85
- const [ atime , mtime ] = Array . isArray ( value ) ? value : [ value , value ] ;
95
+ const [ pathType , atime , mtime = atime ] = value ;
86
96
87
97
let fd ;
88
98
//
89
99
// test async code paths
90
100
//
91
- fs . utimes ( tmpdir . path , atime , mtime , common . mustCall ( ( err ) => {
101
+ fs . utimes ( pathType ( tmpdir . path ) , atime , mtime , common . mustCall ( ( err ) => {
92
102
expect_ok ( 'utimes' , tmpdir . path , err , atime , mtime ) ;
93
103
94
- fs . utimes ( 'foobarbaz' , atime , mtime , common . mustCall ( ( err ) => {
95
- expect_errno ( 'utimes' , 'foobarbaz' , err , 'ENOENT' ) ;
104
+ fs . lutimes ( pathType ( lpath ) , atime , mtime , common . mustCall ( ( err ) => {
105
+ expect_ok ( 'lutimes' , lpath , err , atime , mtime , fs . lstatSync ) ;
106
+
107
+ fs . utimes ( pathType ( 'foobarbaz' ) , atime , mtime , common . mustCall ( ( err ) => {
108
+ expect_errno ( 'utimes' , 'foobarbaz' , err , 'ENOENT' ) ;
96
109
97
- // don't close this fd
98
- if ( common . isWindows ) {
99
- fd = fs . openSync ( tmpdir . path , 'r+' ) ;
100
- } else {
101
- fd = fs . openSync ( tmpdir . path , 'r' ) ;
102
- }
110
+ // don't close this fd
111
+ if ( common . isWindows ) {
112
+ fd = fs . openSync ( tmpdir . path , 'r+' ) ;
113
+ } else {
114
+ fd = fs . openSync ( tmpdir . path , 'r' ) ;
115
+ }
103
116
104
- fs . futimes ( fd , atime , mtime , common . mustCall ( ( err ) => {
105
- expect_ok ( 'futimes' , fd , err , atime , mtime ) ;
117
+ fs . futimes ( fd , atime , mtime , common . mustCall ( ( err ) => {
118
+ expect_ok ( 'futimes' , fd , err , atime , mtime ) ;
106
119
107
- syncTests ( ) ;
120
+ syncTests ( ) ;
108
121
109
- setImmediate ( common . mustCall ( runTests ) , iter ) ;
122
+ setImmediate ( common . mustCall ( runTests ) , iter ) ;
123
+ } ) ) ;
110
124
} ) ) ;
111
125
} ) ) ;
112
126
} ) ) ;
@@ -115,9 +129,12 @@ function runTests(iter) {
115
129
// test synchronized code paths, these functions throw on failure
116
130
//
117
131
function syncTests ( ) {
118
- fs . utimesSync ( tmpdir . path , atime , mtime ) ;
132
+ fs . utimesSync ( pathType ( tmpdir . path ) , atime , mtime ) ;
119
133
expect_ok ( 'utimesSync' , tmpdir . path , undefined , atime , mtime ) ;
120
134
135
+ fs . lutimesSync ( pathType ( lpath ) , atime , mtime ) ;
136
+ expect_ok ( 'lutimesSync' , lpath , undefined , atime , mtime , fs . lstatSync ) ;
137
+
121
138
// Some systems don't have futimes
122
139
// if there's an error, it should be ENOSYS
123
140
try {
@@ -129,7 +146,7 @@ function runTests(iter) {
129
146
130
147
let err ;
131
148
try {
132
- fs . utimesSync ( 'foobarbaz' , atime , mtime ) ;
149
+ fs . utimesSync ( pathType ( 'foobarbaz' ) , atime , mtime ) ;
133
150
} catch ( ex ) {
134
151
err = ex ;
135
152
}
0 commit comments