tschaub / mock-fs

Configurable mock for the fs module
https://npmjs.org/package/mock-fs
Other
907 stars 85 forks source link

feat: support bigint option in fs.stat, fs.lstat, and fs.fstat #325

Closed 3cp closed 3 years ago

3cp commented 3 years ago

closes #321

BREAKING CHANGE: drop support of Nodejs before v12.0.0

3cp commented 3 years ago

fillStatsArray needs to be updated to support bigint too.

3cp commented 3 years ago

There is one more stat array in fs binding: bigintStatValues needs to be implemented.

> process.binding('fs')
BaseObject {
  statValues: Float64Array(36) [
      16777220,     33188,          1,       501,
            20,         0,       4096, 102210022,
             0,         0, 1620469235, 811292225,
    1620469235, 811292225, 1620469235, 811292225,
    1620469235, 811292225,          0,         0,
             0,         0,          0,         0,
             0,         0,          0,         0,
             0,         0,          0,         0,
             0,         0,          0,         0
  ],
  bigintStatValues: BigUint64Array(36) [
      16777220n,      33188n,          1n,
           501n,         20n,          0n,
          4096n,   98442580n,       1270n,
             8n, 1615088646n,  360473114n,
    1615088646n,  243729709n, 1615088646n,
     243729709n, 1615082739n,  729366298n,
             0n,          0n,          0n,
             0n,          0n,          0n,
             0n,          0n,          0n,
             0n,          0n,          0n,
             0n,          0n,          0n,
             0n,          0n,          0n
  ],
3cp commented 3 years ago

Since nodejs v10.0.0, the internal return of binding.stat/lstat/fstat are now Float64Array or BigUint64Array. The old nodejs returns a Stats object with properties like stats.mode.

I am trying to make it work for both old and new nodejs. The implementation might work (I have not tested enough), but there are tons of test code needs two copies (one for old, and one for new).

Since nodejs v10.0.0 is already EOL, @tschaub should we consider for a major version upgrade to target only nodejs v12+? The benefits are:

  1. can remove lots of tricky code.
  2. also upgrade all dev deps to latest.
tschaub commented 3 years ago

Since nodejs v10.0.0 is already EOL, @tschaub should we consider for a major version upgrade to target only nodejs v12+?

Sounds like a good plan.

3cp commented 3 years ago

The added code are for test. The code in lib folder is 100 LOC less.

huocp@chunpengs-mbp ~/g/mock-fs (stats-bigint)> cloc lib/
      12 text files.
      12 unique files.                              
       0 files ignored.

github.com/AlDanial/cloc v 1.88  T=0.03 s (423.4 files/s, 100451.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      12            282            836           1729
-------------------------------------------------------------------------------
SUM:                            12            282            836           1729
-------------------------------------------------------------------------------

huocp@chunpengs-mbp ~/g/mock-fs (stats-bigint)> git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
huocp@chunpengs-mbp ~/g/mock-fs (main)> cloc lib/
      12 text files.
      12 unique files.                              
       0 files ignored.

github.com/AlDanial/cloc v 1.88  T=0.03 s (410.1 files/s, 103698.1 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                      12            295            908           1831
-------------------------------------------------------------------------------
SUM:                            12            295            908           1831
-------------------------------------------------------------------------------
3cp commented 3 years ago

I will get a win10 box to test the NaN uid/gid thing.

3cp commented 3 years ago

GitHub actions still wants to see nodejs 6/8/10 results. I don't know how to remove them in the checks.

Windows now uses 0 for uid and gid, not NaN anymore (this is probably a change in nodejs v10.0.0 when stats is a Float64Array internally).

3cp commented 3 years ago

@tschaub after merging this one, you might want to update README to clarify nodejs support in mock-fs v4 and v5. Also I think it's a good idea to highlight the missing support of fs.opendir #319 which will unlikely be fixed in future.

3cp commented 3 years ago

@tschaub I will do a small cleanup of uid/gid for Windows soon. It's unnecessary to use NaN internally in our code.

tschaub commented 3 years ago

mock-fs@5.0.0-beta.1 includes this change.

3cp commented 3 years ago

@tschaub can you release v5.0.0?