@@ -24,13 +24,17 @@ const common = require('../common');
2424const  assert  =  require ( 'assert' ) ; 
2525const  util  =  require ( 'util' ) ; 
2626const  fs  =  require ( 'fs' ) ; 
27+ const  url  =  require ( 'url' ) ; 
2728
2829const  tmpdir  =  require ( '../common/tmpdir' ) ; 
2930tmpdir . refresh ( ) ; 
3031
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 )  { 
3236  if  ( typeof  resource  ===  'string' )  { 
33-     return  fs . statSync ( resource ) ; 
37+     return  statSync ( resource ) ; 
3438  } 
3539  const  stats  =  fs . fstatSync ( resource ) ; 
3640  // Ensure mtime has been written to disk 
@@ -41,9 +45,9 @@ function stat_resource(resource) {
4145  return  fs . fstatSync ( resource ) ; 
4246} 
4347
44- function  check_mtime ( resource ,  mtime )  { 
48+ function  check_mtime ( resource ,  mtime ,   statSync )  { 
4549  mtime  =  fs . _toUnixTimestamp ( mtime ) ; 
46-   const  stats  =  stat_resource ( resource ) ; 
50+   const  stats  =  stat_resource ( resource ,   statSync ) ; 
4751  const  real_mtime  =  fs . _toUnixTimestamp ( stats . mtime ) ; 
4852  return  mtime  -  real_mtime ; 
4953} 
@@ -55,8 +59,8 @@ function expect_errno(syscall, resource, err, errno) {
5559  ) ; 
5660} 
5761
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 ) ; 
6064  assert ( 
6165    // Check up to single-second precision. 
6266    // Sub-second precision is OS and fs dependant. 
@@ -68,45 +72,55 @@ function expect_ok(syscall, resource, err, atime, mtime) {
6872
6973const  stats  =  fs . statSync ( tmpdir . path ) ; 
7074
75+ const  asPath  =  ( path )  =>  path ; 
76+ const  asUrl  =  ( path )  =>  url . pathToFileURL ( path ) ; 
77+ 
7178const  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 ( ) ] , 
7886] ; 
87+ 
7988runTests ( cases . values ( ) ) ; 
8089
8190function  runTests ( iter )  { 
8291  const  {  value,  done }  =  iter . next ( ) ; 
8392  if  ( done )  return ; 
93+ 
8494  // 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 ; 
8696
8797  let  fd ; 
8898  // 
8999  // test async code paths 
90100  // 
91-   fs . utimes ( tmpdir . path ,  atime ,  mtime ,  common . mustCall ( ( err )  =>  { 
101+   fs . utimes ( pathType ( tmpdir . path ) ,  atime ,  mtime ,  common . mustCall ( ( err )  =>  { 
92102    expect_ok ( 'utimes' ,  tmpdir . path ,  err ,  atime ,  mtime ) ; 
93103
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' ) ; 
96109
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+          } 
103116
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 ) ; 
106119
107-         syncTests ( ) ; 
120+            syncTests ( ) ; 
108121
109-         setImmediate ( common . mustCall ( runTests ) ,  iter ) ; 
122+           setImmediate ( common . mustCall ( runTests ) ,  iter ) ; 
123+         } ) ) ; 
110124      } ) ) ; 
111125    } ) ) ; 
112126  } ) ) ; 
@@ -115,9 +129,12 @@ function runTests(iter) {
115129  // test synchronized code paths, these functions throw on failure 
116130  // 
117131  function  syncTests ( )  { 
118-     fs . utimesSync ( tmpdir . path ,  atime ,  mtime ) ; 
132+     fs . utimesSync ( pathType ( tmpdir . path ) ,  atime ,  mtime ) ; 
119133    expect_ok ( 'utimesSync' ,  tmpdir . path ,  undefined ,  atime ,  mtime ) ; 
120134
135+     fs . lutimesSync ( pathType ( lpath ) ,  atime ,  mtime ) ; 
136+     expect_ok ( 'lutimesSync' ,  lpath ,  undefined ,  atime ,  mtime ,  fs . lstatSync ) ; 
137+ 
121138    // Some systems don't have futimes 
122139    // if there's an error, it should be ENOSYS 
123140    try  { 
@@ -129,7 +146,7 @@ function runTests(iter) {
129146
130147    let  err ; 
131148    try  { 
132-       fs . utimesSync ( 'foobarbaz' ,  atime ,  mtime ) ; 
149+       fs . utimesSync ( pathType ( 'foobarbaz' ) ,  atime ,  mtime ) ; 
133150    }  catch  ( ex )  { 
134151      err  =  ex ; 
135152    } 
0 commit comments