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 code optimization: avoid running ~70 test cases twice #2095

Merged
2 commits merged into from Nov 16, 2020

Conversation

pdcastro
Copy link
Contributor

Change-type: patch

@pdcastro pdcastro marked this pull request as ready for review November 16, 2020 00:13
@pdcastro pdcastro requested a review from a team as a code owner November 16, 2020 00:13

module.exports = {
...commonConfig,
spec: ['tests/auth/*.spec.ts', 'tests/commands/**/*.spec.ts'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The objective here was to test CLI commands only, which is the second element in this array. However, I found that it was necessary to run the tests under the auth folder first, as they appear to have CLI login side effects. (There are three test files under the auth folder and, interestingly, it was necessary to run all three of them. Running any single one of the three test files was not sufficient. Hence auth/*.spec.ts.)

(By the way, adding the auth patterns to the mocha command line directly, as mentioned in another comment in this PR, is also something that I tried without success.)

@@ -61,13 +61,13 @@
"test:shrinkwrap": "ts-node --transpile-only automation/run.ts test-shrinkwrap",
"test:source": "cross-env BALENA_CLI_TEST_TYPE=source mocha",
"test:standalone": "npm run build:standalone && npm run test:standalone:fast",
"test:standalone:fast": "cross-env BALENA_CLI_TEST_TYPE=standalone mocha",
"test:standalone:fast": "cross-env BALENA_CLI_TEST_TYPE=standalone mocha --config .mocharc-standalone.js",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The objective of this commit is to avoid running a sizeable fraction of the test cases twice, redundantly. The CLI still intentionally runs many tests cases twice, first using the Node.js interpreter available in the PATH, and then using the built pkg binary (standalone zip package), in order to test the pkg binary too. However, this only makes sense for the end-to-end test cases that invoke CLI commands, using the runCommand function in tests/helpers.ts. The runCommand function decides whether to execute the built pkg binary as a child process, or whether to call the CLI entry point in the same mocha process, depending on the value of the BALENA_CLI_TEST_TYPE env var. Other test cases, typically unit tests, don't call the runCommand function, and always run in the mocha process, regardless of the value of BALENA_CLI_TEST_TYPE. It is these other test cases that were being run twice, unnecessarily. This commit avoids running these other tests cases for the test:standalone[:fast] script.

Before I came up with the solution of adding the .mocharc.js and .mocharc-standalone.js files, I tried a smaller change:

"test:standalone:fast": "cross-env BALENA_CLI_TEST_TYPE=standalone mocha tests/commands/**/*.ts",

... i.e., I tried specifying the desired set of test cases on the mocha command line itself. It didn't work: it would always run more tests than specified on the command line. I tried enclosing the pattern with single quotes or double quotes: no difference. I tried different patterns for different folders, I tried explicitly specifying files instead of using wildcard characters, but it would either not work for other reasons, or it would not run the test cases I wanted. Then I tried upgrading mocha from v6 to v8: no difference. (I decided the keep the mocha upgrade anyway, because it seems like a good thing.) Then I finally tested with separate configuration files and the --config option, and it worked fine!

"test:fast": "npm run build:fast && npm run test:source",
"test:only": "npm run build:fast && cross-env BALENA_CLI_TEST_TYPE=source mocha \"tests/**/${npm_config_test}.spec.ts\"",
"catch-uncommitted": "ts-node --transpile-only automation/run.ts catch-uncommitted",
"ci": "npm run test && npm run catch-uncommitted",
"watch": "gulp watch",
"lint": "balena-lint -e ts -e js --typescript --fix automation/ lib/ typings/ tests/ bin/balena bin/balena-dev gulpfile.js",
"lint": "balena-lint -e ts -e js --typescript --fix automation/ lib/ typings/ tests/ bin/balena bin/balena-dev gulpfile.js .mocharc.js .mocharc-standalone.js",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

.mocha*.js works on Unix, but not on Windows, hence spelling out the names in full.

@@ -59,7 +59,7 @@ macOS: | `/usr/local/lib/balena-cli/` <br> `/usr/local/bin/balena`
[macOS](https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.Uydjga1dXDg) |
[Windows](https://www.computerhope.com/issues/ch000549.htm)

> * If you are using macOS Catalina (10.15), [check this known issue and
> * If you are using macOS 10.15 or later (Catalina, Big Sur), [check this known issue and
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is in its own commit. I have also updated issue #1479 to cover both Catalina and Big Sur.

Copy link
Contributor

@srlowe srlowe left a comment

Choose a reason for hiding this comment

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

Good stuff! Interested to see what effect it has on test times.

@ghost ghost merged commit 4681d90 into master Nov 16, 2020
@ghost ghost deleted the big-sur-notarization branch November 16, 2020 15:32
@pdcastro
Copy link
Contributor Author

Good stuff! Interested to see what effect it has on test times.

Sadly the impact is minor - seconds rather than minutes. Still, unnecessarily running half of test cases twice seemed like something to avoid...

This pull request was closed.
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.

None yet

2 participants