File
Blob: src/workerd/api/node/tests/fs-utimes-test.js
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import { ok, match, rejects, strictEqual, throws } from 'node:assert'; |
| 5 | |
| 6 | import { |
| 7 | openSync, |
| 8 | existsSync, |
| 9 | closeSync, |
| 10 | statSync, |
| 11 | fstatSync, |
| 12 | utimesSync, |
| 13 | futimesSync, |
| 14 | lutimesSync, |
| 15 | utimes, |
| 16 | futimes, |
| 17 | lutimes, |
| 18 | promises, |
| 19 | } from 'node:fs'; |
| 20 | |
| 21 | strictEqual(typeof openSync, 'function'); |
| 22 | strictEqual(typeof closeSync, 'function'); |
| 23 | strictEqual(typeof statSync, 'function'); |
| 24 | strictEqual(typeof utimesSync, 'function'); |
| 25 | strictEqual(typeof futimesSync, 'function'); |
| 26 | strictEqual(typeof lutimesSync, 'function'); |
| 27 | strictEqual(typeof utimes, 'function'); |
| 28 | strictEqual(typeof futimes, 'function'); |
| 29 | strictEqual(typeof lutimes, 'function'); |
| 30 | strictEqual(typeof promises.utimes, 'function'); |
| 31 | strictEqual(typeof promises.lutimes, 'function'); |
| 32 | |
| 33 | const kInvalidArgTypeError = { code: 'ERR_INVALID_ARG_TYPE' }; |
| 34 | |
| 35 | function checkStat(path, mtimeMsCheck) { |
| 36 | const bigint = typeof mtimeMsCheck === 'bigint'; |
| 37 | const { atimeMs, mtimeMs, ctimeMs, birthtimeMs } = |
| 38 | typeof path === 'number' |
| 39 | ? fstatSync(path, { bigint }) |
| 40 | : statSync(path, { bigint }); |
| 41 | strictEqual(mtimeMs, mtimeMsCheck); |
| 42 | strictEqual(ctimeMs, mtimeMsCheck); |
| 43 | strictEqual(atimeMs, bigint ? 0n : 0); |
| 44 | strictEqual(birthtimeMs, bigint ? 0n : 0); |
| 45 | } |
| 46 | |
| 47 | export const utimesTest = { |
| 48 | async test() { |
| 49 | throws(() => utimesSync(123), kInvalidArgTypeError); |
| 50 | throws(() => utimesSync('', {}), kInvalidArgTypeError); |
| 51 | throws(() => utimesSync('', 0, {}), kInvalidArgTypeError); |
| 52 | throws(() => lutimesSync(123), kInvalidArgTypeError); |
| 53 | throws(() => lutimesSync('', {}), kInvalidArgTypeError); |
| 54 | throws(() => lutimesSync('', 0, {}), kInvalidArgTypeError); |
| 55 | throws(() => futimesSync(''), kInvalidArgTypeError); |
| 56 | throws(() => futimesSync(0, {}), kInvalidArgTypeError); |
| 57 | throws(() => futimesSync(0, 0, {}), kInvalidArgTypeError); |
| 58 | throws(() => utimes(0), kInvalidArgTypeError); |
| 59 | throws(() => utimes('', {}), kInvalidArgTypeError); |
| 60 | throws(() => utimes('', 0, {}), kInvalidArgTypeError); |
| 61 | throws(() => utimes('', 0, 0), kInvalidArgTypeError); |
| 62 | throws(() => utimes(''), kInvalidArgTypeError); |
| 63 | throws(() => lutimes(0), kInvalidArgTypeError); |
| 64 | throws(() => lutimes('', {}), kInvalidArgTypeError); |
| 65 | throws(() => lutimes('', 0, {}), kInvalidArgTypeError); |
| 66 | throws(() => lutimes('', 0, 0), kInvalidArgTypeError); |
| 67 | throws(() => lutimes(''), kInvalidArgTypeError); |
| 68 | throws(() => futimes(''), kInvalidArgTypeError); |
| 69 | throws(() => futimes(0, {}), kInvalidArgTypeError); |
| 70 | throws(() => futimes(0, 0, {}), kInvalidArgTypeError); |
| 71 | throws(() => futimes(0, 0, 0), kInvalidArgTypeError); |
| 72 | throws(() => futimes(0), kInvalidArgTypeError); |
| 73 | await rejects(promises.utimes(123), kInvalidArgTypeError); |
| 74 | await rejects(promises.utimes('', {}), kInvalidArgTypeError); |
| 75 | await rejects(promises.utimes('', 0, {}), kInvalidArgTypeError); |
| 76 | await rejects(promises.lutimes(123), kInvalidArgTypeError); |
| 77 | await rejects(promises.lutimes('', {}), kInvalidArgTypeError); |
| 78 | await rejects(promises.lutimes('', 0, {}), kInvalidArgTypeError); |
| 79 | |
| 80 | throws( |
| 81 | () => { |
| 82 | utimesSync('/tmp/test.txt', 1000, new Date('not a valid date')); |
| 83 | }, |
| 84 | { |
| 85 | message: /^The value cannot be converted/, |
| 86 | name: 'TypeError', |
| 87 | } |
| 88 | ); |
| 89 | |
| 90 | const fd = openSync('/tmp/test.txt', 'w+'); |
| 91 | ok(existsSync('/tmp/test.txt')); |
| 92 | |
| 93 | checkStat(fd, 0n); |
| 94 | |
| 95 | utimesSync('/tmp/test.txt', 1000, 2000); |
| 96 | checkStat(fd, 2000n); |
| 97 | |
| 98 | utimesSync('/tmp/test.txt', 1000, new Date(0)); |
| 99 | checkStat(fd, 0); |
| 100 | |
| 101 | utimesSync('/tmp/test.txt', 3000, '1970-01-01T01:00:00.000Z'); |
| 102 | checkStat(fd, 3600000n); |
| 103 | |
| 104 | lutimesSync('/tmp/test.txt', 3000, 4000); |
| 105 | checkStat(fd, 4000n); |
| 106 | |
| 107 | lutimesSync('/tmp/test.txt', 1000, new Date(0)); |
| 108 | checkStat(fd, 0); |
| 109 | |
| 110 | lutimesSync('/tmp/test.txt', 3000, '1970-01-01T01:00:00.000Z'); |
| 111 | checkStat(fd, 3600000n); |
| 112 | |
| 113 | futimesSync(fd, 5000, 6000); |
| 114 | checkStat(fd, 6000n); |
| 115 | |
| 116 | futimesSync(fd, 1000, new Date(0)); |
| 117 | checkStat(fd, 0); |
| 118 | |
| 119 | futimesSync(fd, 3000, '1970-01-01T01:00:00.000Z'); |
| 120 | checkStat(fd, 3600000n); |
| 121 | |
| 122 | { |
| 123 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 124 | utimes('/tmp/test.txt', 8000, new Date('not a valid date'), (err) => { |
| 125 | try { |
| 126 | ok(err); |
| 127 | strictEqual(err.name, 'TypeError'); |
| 128 | match(err.message, /The value cannot be converted/); |
| 129 | resolve(); |
| 130 | } catch (err) { |
| 131 | reject(err); |
| 132 | } |
| 133 | }); |
| 134 | await promise; |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 139 | utimes('/tmp/test.txt', 8000, 9000, (err) => { |
| 140 | if (err) return reject(err); |
| 141 | try { |
| 142 | checkStat(fd, 9000n); |
| 143 | resolve(); |
| 144 | } catch (err) { |
| 145 | reject(err); |
| 146 | } |
| 147 | }); |
| 148 | await promise; |
| 149 | } |
| 150 | |
| 151 | { |
| 152 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 153 | lutimes('/tmp/test.txt', 8000, 10000, (err) => { |
| 154 | if (err) return reject(err); |
| 155 | try { |
| 156 | checkStat(fd, 10000n); |
| 157 | resolve(); |
| 158 | } catch (err) { |
| 159 | reject(err); |
| 160 | } |
| 161 | }); |
| 162 | await promise; |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | const { promise, resolve, reject } = Promise.withResolvers(); |
| 167 | futimes(fd, 7000, 11000, (err) => { |
| 168 | if (err) return reject(err); |
| 169 | try { |
| 170 | checkStat(fd, 11000n); |
| 171 | resolve(); |
| 172 | } catch (err) { |
| 173 | reject(err); |
| 174 | } |
| 175 | }); |
| 176 | await promise; |
| 177 | } |
| 178 | |
| 179 | await promises.utimes('/tmp/test.txt', 12000, 13000); |
| 180 | checkStat(fd, 13000n); |
| 181 | await promises.lutimes('/tmp/test.txt', 14000, 15000); |
| 182 | checkStat(fd, 15000n); |
| 183 | |
| 184 | closeSync(fd); |
| 185 | }, |
| 186 | }; |