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

Centralize test setup #5362

Merged
merged 29 commits into from
Jul 31, 2024
Merged

Centralize test setup #5362

merged 29 commits into from
Jul 31, 2024

Conversation

snejus
Copy link
Member

@snejus snejus commented Jul 12, 2024

This PR deduplicates shared tests setup that was used across test files. It attempts to centralise the setup into a single source of truth implementation in beets.test.helper.

This works towards the eventual migration to pytest (#5361) making it easier to replace the tests setup with pytest fixtures.

It builds upon the temporary files cleanup in #5345.

Details

Test setup

  • Mostly duplicate test setup implementations in beets.test._common.TestCase and beets.test.helper.TestHelper were merged into a single implementation as beets.test.helper.BeetsTestCase.
  • Replaced unittest.TestCase and beets.test.helper.TestHelper combination with beets.test.helper.ImportTestCase
  • Refactored test/test_plugins.py removing duplicate import files setup.
  • Centralised setting up the database on disk.
  • Centralised plugin test setup into beets.test.helpers.PluginMixin.

Removals

  • Around 1/3 of the removed lines correspond to the removal of def suite() functions that were previously used for tests collection.
  • Removed redundant setUp and tearDown methods from many test files given that the functions that they performed had been centralised in beets.test.helper.
  • Removed redundant library initialisations

Importing

  • Centralised import directory creation (only gets created on the first access).
  • Deduplicated _setup_import_session implementations in TerminalImportSessionSetup and ImportHelper.
  • Merged ImportHelper._create_import_dir and TestHelper.create_importer implementations into ImportHelper.setup_importer.
  • Merged various takes on import files prep into ImportHelper.prepare_albums_for_import and ImportHelper.prepare_album_for_import.
  • Seeing that many tests used it, defined AsIsImporterMixin which provides a method run_asis_importer to setup asis (non-autotag) importer and run it

@snejus snejus self-assigned this Jul 12, 2024
@snejus snejus changed the base branch from master to cleanup-tests July 12, 2024 20:07
Copy link

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

@snejus snejus requested a review from Serene-Arc July 12, 2024 20:07
@snejus snejus force-pushed the centralize-test-setup branch 6 times, most recently from 604a683 to eec8c19 Compare July 13, 2024 18:18
Copy link
Member

@bal-e bal-e left a comment

Choose a reason for hiding this comment

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

This is an incredibly comprehensive PR! I've left a few notes, but everything looks good. Perhaps you could use pathlib in the functions you've rewritten, so that they don't need to be migrated later? Or we can leave that for the later pathlib-specific PR(s).

test/plugins/test_embedart.py Show resolved Hide resolved
test/test_plugins.py Show resolved Hide resolved
test/plugins/test_filefilter.py Outdated Show resolved Hide resolved
test/plugins/test_replaygain.py Outdated Show resolved Hide resolved
@Serene-Arc
Copy link
Contributor

I'll try and get my review in over the next day or two! I want to really sit down and go through it all.

@snejus snejus force-pushed the centralize-test-setup branch 3 times, most recently from 1153070 to 179d4d9 Compare July 15, 2024 01:56
@snejus
Copy link
Member Author

snejus commented Jul 15, 2024

Perhaps you could use pathlib in the functions you've rewritten, so that they don't need to be migrated later? Or we can leave that for the later pathlib-specific PR(s).

That's a very good point. I was careful to not try and refactor anything to do with paths (out of scope here, and the PR size would have quickly gotten out of hand) but I think it's a good idea to use pathlib in rewrites/new stuff.

For now I made the new methods in ImportHelper to use pathlib and you can see it's already made it cleaner!

@bal-e
Copy link
Member

bal-e commented Jul 15, 2024

That's a very good point. I was careful to not try and refactor anything to do with paths (out of scope here, and the PR size would have quickly gotten out of hand) but I think it's a good idea to use pathlib in rewrites/new stuff.

I agree. When we fully migrate to pytest, we can use its tempdir fixtures which already use pathlib. That should also end up migrating the majority of the tests to pathlib.

@snejus snejus mentioned this pull request Jul 16, 2024
@snejus snejus force-pushed the centralize-test-setup branch 2 times, most recently from ff9f479 to b91a7f9 Compare July 16, 2024 15:04
test/test_importer.py Outdated Show resolved Hide resolved
@bal-e
Copy link
Member

bal-e commented Jul 17, 2024

I like the new uses of pathlib and f-strings, the code feels a lot cleaner. It also looks like the net line change is becoming even more negative, which is awesome. Good job @snejus!

test/plugins/test_mbsubmit.py Show resolved Hide resolved
test/plugins/test_replaygain.py Outdated Show resolved Hide resolved
beets/test/helper.py Show resolved Hide resolved
test/test_importer.py Outdated Show resolved Hide resolved
@Serene-Arc
Copy link
Contributor

I think it would be good if I opened a pathlib PR (as I keep saying I will) after this PR is done. Like this PR, it will have a lot of small line changes that are just replacing function calls and simplifying, but this PR being merged will make that shorter and easier to parse.

@snejus snejus changed the base branch from cleanup-tests to enable-skipped-tests July 18, 2024 07:15
snejus added 20 commits July 28, 2024 18:58
A couple of `ConfigTest` would previously fail locally since they
somehow depended on the local environment to work fine. This commmit
decouples them from any environment by setting up patching of the
environment variables properly.
This aids with the removal of custom import files prep implementations.
And rewrite the tests since they were far too confusing to follow.
"test" plugin was not properly cleaned up after loading and would
therefore stay around and picked up by other tests.
A constant `preload_plugin` is used to disable loading the plugin in the
`setUp` initialisation, allowing the plugin to be loaded manually by the
tests.

Also added a cleanup instruction to remove listeners from configured
plugins, and removed this logic from several tests.
@snejus snejus force-pushed the centralize-test-setup branch 2 times, most recently from ee49c3b to edc820d Compare July 28, 2024 22:04
Copy link
Member

@bal-e bal-e left a comment

Choose a reason for hiding this comment

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

Amazing work @snejus!

@Serene-Arc
Copy link
Contributor

I have no objections! Merge whenever you'd like @snejus

@snejus snejus merged commit c05b3cf into master Jul 31, 2024
12 checks passed
@snejus snejus deleted the centralize-test-setup branch July 31, 2024 07:53
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.

3 participants