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

Spec cleanup #629

Closed
wants to merge 4 commits into from
Closed

Spec cleanup #629

wants to merge 4 commits into from

Conversation

raphinesse
Copy link
Contributor

This is part of an ongoing effort to make the tests of cordova-lib faster, more reliable and more maintainable.

This PR is mainly about maintainability and a bit about reliability. It focuses on the two suites pkgJson.spec and pkgJson-restore.spec. These are the two suites with the longest duration (taken together responsible for about 3/5 of the E2E test runtime). Furthermore, their code quality was so awful that I could reduce the LOC by over 30% respective over 50% without dropping any test cases or sacrificing any clarity (IMHO even drastically improving it).

Other highlights include:

  • Use new, verbose test reporter for better information, especially in CI
  • Unique, randomized directory name generated by tmpDir to prevent fs caching from affecting tests
  • Proper suite-wide setting of test timeouts without affecting the global default
  • Some minor improvements here and there

As I said before, this is part of an ongoing effort. In the next steps I will try to make the tests in the two pkgJson suites more focused (they are often testing more/other things than what it says in their description) and faster (will try to mock cordova-fetch which is a bit of a PITA for these E2E tests).

I'm also aware that many of the factored-out helpers in these two suites are identical. I'd like to leave them that way for now though and consider reusing them across suites after the steps outlined above.

Also, if you review the changes be sure to ignore white space changes. Diff anchoring to it/describe might also help. If someone is interested in that, I can provide more granular commits for these. But I thought it would even be worse to have a 100 commit PR 😒

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 2, 2018

Any thoughts on this? I'll assume lazy consensus if there's no feedback.

The test failure is spurious. If anyone has an idea what could have caused it, I'll be happy to investigate. The error message is very obscure.

@raphinesse raphinesse closed this Aug 2, 2018
@raphinesse raphinesse reopened this Aug 2, 2018
@brodybits
Copy link
Contributor

Sorry I didn't get a chance to look through this one, looks pretty important from the commit messages. I gotta say that spurious test failures do not surprise me so much.

@brodybits
Copy link
Contributor

The first 6 commits (0917185..b6441d1) look fine to me. Not so easy for me to follow most of the changes after that, using -w flag doesn't seem to help me a lot. This is a bit of a tough spot since I don't have a lot of spare cycles, also would not favor continuing with lazy consensus.

I wonder if we (you) should do this cleanup in a bunch of smaller PRs that we can review with some more leisure, one at a time?

@brodybits
Copy link
Contributor

I tried using --anchored=describe --anchored=it options (git 2.18.0 with help from homebrew) to look at 08459ac, gotta say it doesn't seem to help. I also tried opendiff, no better. I think I would have to do side-by-side before and after file comparison to check the changes. I think one function at a time would really help.

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 3, 2018

Thanks for taking the time @brodybits!

I can take out the first six commits so we can land it in a separate PR.

Regarding the other changes: As I mentioned, I have about a hundred commits in private branches that originally made up those changes. Sadly they are not really straightforward and producing a nice history would approximately equal redoing most of the changes from scratch. I guess you understand that I'm not really keen on doing that kind of work.

Last time I checked, I was able to produce diffs that were anchored to the different tests. I will check that again when I find the time. Also notice that I wrote extensive commit messages for these commits, trying to convey what is going on.

How do you wish to proceed with this?

@brodybits
Copy link
Contributor

@raphinesse I would like to get the first 6 commits (0917185..a94a5ca) and FIXUP in a8015e6 landed asap, hereby approve these changes. (Not sure if FIXUP in a8015e6 was supposed to be squashed into another commit or not, I am fine either way.) Build started to look a little flakey once I merged #631 (the contributed change itself was green), which I would really rather not revert unless absolutely necessary, started working on followup fix in #632 and tried the wanted spec commits there.

I also hope we can get the build more consistently green at the same time.

This commit does not drop any test cases. However, it fixes various bugs
in the existing expectations and often asserts stronger expectations
than the original tests.

Changes include:

- DRY code
- Improve formatting
- Reduce excessive comments
- Simplify platform list generation
- More meaningful usage of describe blocks
- Wrap all tests in common describe to allow common setup/teardown
- Use fs-extra for setup/teardown
This commit is separated from the previous cleanup to make the diff more
readable.
This commit is separated from the previous cleanup to make the diff more
readable.
This commit does not drop any test cases. However, it fixes various bugs
in the existing expectations and often asserts stronger expectations
than the original tests.

Changes include:

- DRY code
- Improve formatting
- Reduce excessive comments
- Simplify platform list generation
- Wrap all tests in common describe to allow common setup/teardown
- Use a fresh tmpDir for every test to avoid caching issues
- Use fs-extra for setup/teardown
- FIX Wait for promise
- Properly set default timeout for the whole suite
- Don't symlink into fixtures
- Sane config file handling
- Remove event listeners on teardown
- Re-enable disabled test 005 that was fixed by apache#628
- Do not quote props when not needed
- Do not pass array when we only have scalar
- Nicer imports
- Save test platforms/plugins as constants
@raphinesse
Copy link
Contributor Author

@brodybits Anchored diff works for me (per commit, of course). Example

git show --ignore-space-change --anchored=it f9686c0bc09f

@brodybits
Copy link
Contributor

What is the motivation to move the tests around in f5ffeec?

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 7, 2018

@brodybits It's about getting rid of the last of all the useless describe blocks that were originally in the spec file. I put it in a separate commit to not make the diff completely useless.

@brodybits
Copy link
Contributor

Can you explain why events is removed? I guess it is not needed any longer?

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 7, 2018

@brodybits Correct, events isn't needed anymore.

Previously it was used as part of an overly complicated function to get the list of installed platforms. This is now done much simpler using listPlatforms from cordova/util.

Edit: Here's the commit: fb1bff3

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 7, 2018

@brodybits since you seem to be interested in the nitty gritty details, I prepared two branches with the detailed history for these refactorings. These are not necessarily up-to-date with the changes in this PR nor are they cleaned up (e.g. there are changes that are first introduced and then later removed again). However, they might give you a better idea of what was done.

I suggest looking at the pkgJson branch first, since I did the refactoring second and had a better idea of what was expecting me. I hope this helps.

master...raphinesse:verbose-pkgJson-spec-cleanup
master...raphinesse:verbose-restore-spec-cleanup

@brodybits
Copy link
Contributor

Thanks @raphinesse, just saw your comments. I actually did my own work to break f9686c0 into smaller parts (along with f5ffeec ("merge more describe blocks"), c606e1f ("better names"), and a couple little things I would like to fix) in: master...brodybits:bb-cleanup-restore-spec-in-parts-wip1

It would be great if you can move the last commit (4d139e6 "Cleanup pkgJson.spec") into a separate PR that I (or someone else) could maybe review at some more leisure.

I can take a quick look at the branches you prepared, probably not in very deep detail. The changes do look really good in general and I think I will be taking some of the changes "in faith".

@raphinesse
Copy link
Contributor Author

raphinesse commented Aug 7, 2018

Thanks for all the review effort, @brodybits.

Frankly, I had hoped to merge the whole original PR "in faith". The preparation of this PR by itself already was a lot of work. And getting the tests to a state where I would actually be satisfied with the result will require a lot more work.

As I tried to convey in the original PR description, the two suites I refactored here were written very poorly. And although the refactor made them way more approachable and maintainable, they still are not good test suites in my book for various reasons. So IMO, they will have to undergo further radical changes.

Consequently, I did not want to spend much time waiting for this change to be reviewed in detail and polished to perfection or even formulating this very message, but rather get on with working on the problems I tried to fix here.

In the next few months, my free time will be scarce, and it's unlikely that I will spend it on something I don't really want to do.

expect(getPkgJson('cordova.platforms')).toEqual([PLATFORM_2]);
}).then(function () {
// Remove all platforms without --save.
return cordovaPlatform('rm', [PLATFORM_1, PLATFORM_2]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a very minor change in tested behavior from the following:

                // Remove helpers.testPlatform without --save.
                return cordovaPlatform('rm', ['browser']);

I would favor avoiding change in tested behavior during this kind of cleanup commit if possible.

Copy link
Contributor Author

@raphinesse raphinesse Aug 14, 2018

Choose a reason for hiding this comment

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

I only joined the two adjacent calls to cordovaPlatform('rm', ...); into one. I wouldn't say that this changes tested behavior. Especially since restore is under test here, not cordovaPlatform.

The original code was

.then(function () {
    // Remove helpers.testPlatform without --save.
    return cordovaPlatform('rm', ['browser']);
}).then(function () {
    // Remove secondPlatformAdded without --save.
    return cordovaPlatform('rm', secondPlatformAdded);
})


/** Testing will check if "cordova prepare" is restoring platforms and plugins as expected.
* Uses different basePkgJson files depending on testing expecations of what (platforms/plugins/variables)
* should initially be in pkg.json and/or config.xml.
*/
describe('restore', function () {
setDefaultTimeout(240 * 1000);
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: I would rather see this defined as a constant at the beginning, even if it now appears only once.

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 actually like this better the way it is. The function name already tells us what the argument is. So it's not really a magic number.

Something like setDefaultTimeout(4 * ONE_MINUTE_IN_MS) would be nicer, but hopefully we will be able to reduce this soon anyway.

That being said, I have no strong feelings about this. So I will change it and merge this part of the PR.

@brodybits
Copy link
Contributor

I would personally be fine with merge "in faith", would be a little happier if you would first ask on mailing list with subject something like "Any objections with spec updates in faith?". I did find a couple minor things already which I just commented in the changes, would be happy to deal with them after the fact.

I was also wondering if we should consider breaking some of the spec scripts into separate files, maybe not so urgent with the cleanup.

I definitely see your point about the quality of the existing tests, definitely grateful for your efforts to clean it up.

This was referenced Aug 14, 2018
@raphinesse raphinesse closed this Aug 14, 2018
@raphinesse raphinesse mentioned this pull request Aug 15, 2018
7 tasks
@raphinesse raphinesse deleted the spec-cleanup branch August 22, 2018 21:17
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