Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: allow setting Stats/BigIntStats Date properties #52708

Merged
merged 5 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,51 +461,43 @@ const lazyDateFields = {
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.atimeMs);
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
return this.atime;
return this.atime = dateFromMs(this.atimeMs);
},
set(value) {
this.atime = value;
ObjectDefineProperty(this, 'atime', { __proto__: null, value, writable: true });
},
},
mtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.mtimeMs);
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
return this.mtime;
return this.mtime = dateFromMs(this.mtimeMs);
},
set(value) {
this.mtime = value;
ObjectDefineProperty(this, 'mtime', { __proto__: null, value, writable: true });
},
},
ctime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.ctimeMs);
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
return this.ctime;
return this.ctime = dateFromMs(this.ctimeMs);
},
set(value) {
this.ctime = value;
ObjectDefineProperty(this, 'ctime', { __proto__: null, value, writable: true });
},
},
birthtime: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
const value = dateFromMs(this.birthtimeMs);
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
return this.birthtime;
return this.birthtime = dateFromMs(this.birthtimeMs);
},
set(value) {
this.birthtime = value;
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value, writable: true });
},
},
};
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-fs-stat-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,36 @@ if (!common.isWindows) {
verifyStats(bigintStats, numStats, allowableDelta);
await handle.close();
})().then(common.mustCall());

{
// These two tests have an equivalent in ./test-fs-stat.js

// BigIntStats Date properties can be set before reading them
fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => {
s.atime = 2;
s.mtime = 3;
s.ctime = 4;
s.birthtime = 5;

assert.strictEqual(s.atime, 2);
assert.strictEqual(s.mtime, 3);
assert.strictEqual(s.ctime, 4);
assert.strictEqual(s.birthtime, 5);
}));

// BigIntStats Date properties can be set after reading them
fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => {
// eslint-disable-next-line no-unused-expressions
s.atime, s.mtime, s.ctime, s.birthtime;

s.atime = 2;
s.mtime = 3;
s.ctime = 4;
s.birthtime = 5;

assert.strictEqual(s.atime, 2);
assert.strictEqual(s.mtime, 3);
assert.strictEqual(s.ctime, 4);
assert.strictEqual(s.birthtime, 5);
}));
}
33 changes: 33 additions & 0 deletions test/parallel/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,36 @@ fs.lstat(__filename, undefined, common.mustCall());
]
});
}

{
// These two tests have an equivalent in ./test-fs-stat-bigint.js

// Stats Date properties can be set before reading them
fs.stat(__filename, common.mustSucceed((s) => {
s.atime = 2;
s.mtime = 3;
s.ctime = 4;
s.birthtime = 5;

assert.strictEqual(s.atime, 2);
assert.strictEqual(s.mtime, 3);
assert.strictEqual(s.ctime, 4);
assert.strictEqual(s.birthtime, 5);
}));

// Stats Date properties can be set after reading them
fs.stat(__filename, common.mustSucceed((s) => {
// eslint-disable-next-line no-unused-expressions
s.atime, s.mtime, s.ctime, s.birthtime;

s.atime = 2;
s.mtime = 3;
s.ctime = 4;
s.birthtime = 5;

assert.strictEqual(s.atime, 2);
assert.strictEqual(s.mtime, 3);
assert.strictEqual(s.ctime, 4);
assert.strictEqual(s.birthtime, 5);
}));
}