Skip to content

Conversation

@mmaietta
Copy link
Contributor

@mmaietta mmaietta commented Jan 28, 2025

Was having a fun chat with @erickzhao and wanted to see if I could lend a hand!

This PR accomplishes a few things:

  • Migrates from mocha to vitest
  • Converts all test files to typescript 🙌🏻
  • Adds a new helper function verifySmartUnpack that I pulled from electron-builder's testing approach
    • It reads the asar header and validates it against the snapshot file
    • Provides a hook for additional asar Filesystem operations/verifications should so be desired
    • "Walks" the unpacked directory and stores a snapshot of the folder/file/symlink hierarchy.

With verifySmartUnpack, the header snapshot allows finer granularity of what is happening under the hood, especially with regards to symlinks and integrity calculations. Example respective snapshots excerpts below:

    "var": {
      "link": "private/var",
      "unpacked": true,
    },
"dir1": {
 "files": {
   "file1.txt": { 
     "integrity": {
       "algorithm": "SHA256",
       "blockSize": 4194304,
       "blocks": [
         "420149d3f852894ba7f32e9d3ec7d8919ee2451724bf2580b0186cd373bd6d82",
       ],
       "hash": "420149d3f852894ba7f32e9d3ec7d8919ee2451724bf2580b0186cd373bd6d82",
     },
     "size": 9,
     "unpacked": true,
   },
 },
}

Then the "walk" snapshot functionality allows all these lines to become just verifySmartUnpack('tmp/packthis-unpack-subdir-cli.asar')

asar/test/cli-spec.js

Lines 182 to 185 in 121efeb

assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/file0.txt'));
assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/dir1/file1.txt'));
assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/dir2/subdir/file2.png'));
assert.ok(fs.existsSync('tmp/packthis-unpack-subdir-cli.asar.unpacked/dir2/subdir/file3.txt'));

Additional Notes:

  • vitest required dropping rimraf.sync(...) from the beforeEach hooks since the tests are in parallel and would cause random tests to fail. I've moved the rimraf to the vitest-setup.ts
  • Ideally, to retain the previous functionality of beforeEach->rimraf, the tests need to run in isolation (which I wasn't able to achieve with singleThread) OR a random tmp dir is used each time that is unique to each test case. The latter option would require too large of a change and I wanted to keep changes in this PR isolated to the test/ folder as much as possible.
  • src/asar.ts file change is required though for options?: ListOptions to match its upstream .listFiles(options) API
  • lib required being bumped to es2019 so as to have access to flat() API
  • cleaned up package.json devDependencies

Should this PR be accepted w/ verifySmartUnpack approach, the next steps would be cleaning up the test asserts that are now covered by the "walking" snapshots.

@mmaietta
Copy link
Contributor Author

Whelp, looks like this PR would depend on the upcoming node 22 upgrade. CC @erickzhao

error [email protected]: The engine "node" is incompatible with this module. Expected version "^18.0.0 || ^20.0.0 || >=22.0.0". Got "16.20.1"

@erickzhao erickzhao changed the title feat: Migrate to vitest with .unpacked and Asar Header snapshots test: Migrate to vitest with .unpacked and Asar Header snapshots Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant