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

Modernize, remove outdated code, add code-analysis checks #168

Merged
merged 16 commits into from
Jul 1, 2022

Conversation

mauritsvanrees
Copy link
Sponsor Member

@jensens I saw your closed PR #160 again after I started this. I took over a few ideas, but probably not all. Some room for improvement in a later PR, or if you want you can edit this one.

@ericof I added the plone/code-analysis action/docker here, in a bit trimmed down form. I only have make format and make lint, and only one GitHub action job which runs all checks after each other.

In coredev, when you run `bin/test -u`, all is well.
When you run `bin/test -s plone.dexterity`, with or without `-u`, one test fails:

```
Error in test testAddContentToContainer_preserves_existing_id (plone.dexterity.tests.test_utils.TestUtils)
Traceback (most recent call last):
  File "/Users/maurits/.pyenv/versions/3.10.4/lib/python3.10/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/Users/maurits/.pyenv/versions/3.10.4/lib/python3.10/unittest/case.py", line 591, in run
    self._callTestMethod(testMethod)
  File "/Users/maurits/.pyenv/versions/3.10.4/lib/python3.10/unittest/case.py", line 549, in _callTestMethod
    method()
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.dexterity/plone/dexterity/tests/test_utils.py", line 83, in testAddContentToContainer_preserves_existing_id
    item = addContentToContainer(container, item, checkConstraints=False)
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.dexterity/plone/dexterity/utils.py", line 175, in addContentToContainer
    name = INameChooser(container).chooseName(name, object)
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.content/plone/app/content/namechooser.py", line 55, in chooseName
    return self._findUniqueName(name, obj)
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.content/plone/app/content/namechooser.py", line 64, in _findUniqueName
    if not check_id(name, required=1):
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.app.content/plone/app/content/namechooser.py", line 104, in do_Plone_check
    return check_id(obj, newid, required=required, contained_by=parent)
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.base/src/plone/base/utils.py", line 412, in check_id
    result = _check_for_collision(contained_by, id, **kwargs)
  File "/Users/maurits/community/plone-coredev/6.0/src/plone.base/src/plone/base/utils.py", line 476, in _check_for_collision
    if portal and cid in portal.contentIds():
AttributeError: 'Dummy' object has no attribute 'contentIds'
```

So the portal here is a Dummy from our tests.  No idea why there is a difference in how we run the tests, especially without layers.
Anyway, fixed by adding a `contentIds` method to our Dummy class, returning an empty list.
This uses the plone/code-quality docker image.  Really fast after the first run.
On Mac: `brew install --cask docker`, then start Docker Desktop, or have it start always in the background or something.

I made it easy: you can only do `make format` and `make lint`.
There is only one gh-actions job, which runs each linter in a separate step.

I did not want to create a pre-commit hook, but you could.
'make lint' now finishes without error.
Removed the conditional code.
Removed `bbb.py`, which was only used internally.
@mister-roboto
Copy link

@mauritsvanrees thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@mauritsvanrees
Copy link
Sponsor Member Author

@jenkins-plone-org please run jobs

@ericof
Copy link
Sponsor Member

ericof commented Jul 1, 2022

Why did you prefer to run one check at a time? My approach was to run all of them to:

  • Get all results (no matter if a previous one fail)
  • Be a bit faster

@ericof ericof self-requested a review July 1, 2022 13:26
@mauritsvanrees
Copy link
Sponsor Member Author

Why did you prefer to run one check at a time? My approach was to run all of them to:

  • Get all results (no matter if a previous one fail)
  • Be a bit faster

Your approach spins up five VMs, which each checkout the source code and get the docker image.
My approach uses only one VM, one checkout, one docker image.

The checks in this PR took 20 seconds.
For plone.app.transmogrifier where you added it this week, see [these checks]. The longest one is 12 seconds, so it is indeed faster. Total duration over all VMs though is 44 seconds.

@wesleybl
Copy link
Member

wesleybl commented Jul 1, 2022

I still don't understand the advantage of running code-analysis in a container. It's faster? On the first run it sure takes longer. Wouldn't creating a venv solve the problem?

Copy link
Sponsor Member

@ericof ericof left a comment

Choose a reason for hiding this comment

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

LGTM

@ericof
Copy link
Sponsor Member

ericof commented Jul 1, 2022

I still don't understand the advantage of running code-analysis in a container. It's faster? On the first run it sure takes longer. Wouldn't creating a venv solve the problem?

The idea of having a GitHub action that does that was to have sane defaults and versions for all tools, something that is reproducible easily.

Having the action to use a pre-built Docker image speeds up everything, reducing the time to a few seconds.

Now, locally, we decided to go with the Docker image to avoid unpleasant surprises when it comes to versions (and yes, that happened a lot).

@mauritsvanrees
Copy link
Sponsor Member Author

I still don't understand the advantage of running code-analysis in a container. It's faster? On the first run it sure takes longer. Wouldn't creating a venv solve the problem?

On my laptop make format in this repo takes 3.6 seconds, make lint takes 7.6 seconds. That is fast enough. Certainly much faster than I had expected with my very limited Docker experience.

A venv works too. You could add a tox.ini that installs all these tools, either in one shared or in multiple venvs, and call for example tox -e format and tox -e lint. But then you have to store a list of versions somewhere, either in tox.ini or a constraints file. And each repository will have its own list of versions. And each its own Python version to run them with. And you have one or more venvs in every repository. And then you have a passing lint check locally, and in gh-actions it fails and you have to figure out why.

In contrast, with the Docker approach, you download the image once, and then use it in all repositories. There is one version number (1.0.1) for the used image in Makefile.

There are multiple ways to tackle this, with pros and cons, and I don't think we have the best solution yet. I still feel like I am exploring possibilities. Some of my goals:

  • Run the same checks, with the same versions and config locally and on gh-actions.
  • Do this in as few lines of code as possible. I don't like Makefiles or gh-actions yaml files with hundreds of lines. They tend to gather cruft that no one dares clean up because someone might be using it.

@mauritsvanrees mauritsvanrees merged commit 6ab1f7e into master Jul 1, 2022
@mauritsvanrees mauritsvanrees deleted the maurits-modernize branch July 1, 2022 21:30
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.

5 participants