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

test: adding chai+snapshot support. adding --ordering unit tests #357

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mmaietta
Copy link
Contributor

@mmaietta mmaietta commented Feb 12, 2025

(Proposing this as an alternative to migrating to jest?)
Note: This was test-only changes, but I was encouraged to add jsdoc to ordering, so I'm not sure if test: is the correct tag now.

Following up from a convo and the jest PR, this provides interim support for snapshot tests while still leveraging mocha and electron-mocha. This PR adds dynamically generated ordering.txt files for test coverage of current ordering logic

Adds 3 unit tests to cover ordering.txt files w/:

  • format: filepath
  • format: : filepath
  • format: random-number : filepath

Functionally, this PR adds:

  • GHA test.yaml -> fail-fast: false allows the matrix jobs to continue even when one fails. This will allow verifying asar behavior across each node version before the overall test is marked as a failure.
  • verifySmartUnpack helper function that does the following:
    • verifyHeader - parses out unstable properties ("offset" varies per build machine?), converts all paths (e.g. link) to system independent strings, and then stores it as a snapshot
    • verifyFileTree - Traverses a directory and snapshots the tree hierarchy and plain text files (also used for unpacked dir snapshots)
  • createSymlinkedApp - Creates a new app at runtime from the app template, adds folders, symlinks, and allows you to pass in additionalFiles that you can use to customize each app's directory

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 included in 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'));

…ing it needs to be fixed but currently the fixture is flaky and warrants a separate investigation ¯\_(ツ)_/¯
@mmaietta mmaietta marked this pull request as ready for review February 12, 2025 20:58
@mmaietta mmaietta requested a review from a team as a code owner February 12, 2025 20:58
.mocharc.js Outdated
"recursive": true,
"file": "./mocha.setup.js", // setup file before everything else loads
"forbid-only": true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add root-level ./*.js scripts to prettier:write as well

package.json Outdated
@@ -44,10 +45,12 @@
"devDependencies": {
"@types/minimatch": "^3.0.5",
"@types/node": "^12.0.0",
"chai": "^4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we usually use caret ranges with a full version number, so that'd be like ^4.5.0

const privateVarPath = path.join(tmpPath, 'private', 'var');
const varPath = path.join(tmpPath, 'var');
module.exports = async (testName, additionalFiles = {}) => {
const outDir = (testName || 'app') + Math.floor(Math.random() * 100);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use fs.mkdtemp here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but the reason why I did it this way was that all other test artifacts/fixtures are outputted to ${ROOT_PROJECT_DIR}/tmp, so I figured I would keep it consistent. It has the added benefit of the tmp dir is cleaned before each individual test and being able to debug a test failure from an easy location.

Happy to translate the logic to mkdtemp if preferred!


const orderingPath = path.join(testPath, '../ordered-app-ordering3.txt');
const data = filesOrdering.reduce((prev, curr) => {
return `${prev}${Math.floor(Math.random() * 1000)} : ${curr} \n`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: what happens if the random number has a collision?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing, current logic ignores anything before the :

asar/src/asar.ts

Lines 93 to 95 in 9b7ccfe

if (line.includes(':')) {
line = line.split(':').pop()!;
}

…itional files to verify that "ordering" is successfully applied
@mmaietta mmaietta force-pushed the snapshot-tests-mocha branch from 99608e5 to 4996c57 Compare February 14, 2025 04:43
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.

2 participants