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

Make running Playwright to update snapshots easier #4585

Merged

Conversation

madewithkode
Copy link
Collaborator

Fixes

Fixes #4535 by @sarayourfriend

Description

This PR aims to make debugging Playwright test failures caused by snapshot changes easier by modifying the current Playwright CI workflow to run tests with the -u option which basically updates modified snapshots automatically. We then upload a gzipped git patch blob of these changes so the initiating user can download and apply the patch to their branch without having to run Playwright updates(which happen to be a resource intensive process) locally.

Testing Instructions

This can be tested by triggering a failing Playwright workflow due to UI changes.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (./ov just catalog/generate-docs for catalog
    PRs) or the media properties generator (./ov just catalog/generate-docs media-props
    for the catalog or ./ov just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@madewithkode madewithkode requested a review from a team as a code owner July 1, 2024 14:35
@madewithkode madewithkode self-assigned this Jul 1, 2024
@openverse-bot openverse-bot added 🧱 stack: documentation Related to Sphinx documentation 🧱 stack: mgmt Related to repo management and automations 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 🤖 aspect: dx Concerns developers' experience with the codebase labels Jul 1, 2024
Copy link

github-actions bot commented Jul 1, 2024

Full-stack documentation: https://docs.openverse.org/_preview/4585

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Changed files 🔄:

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Looks good to me, just two small things to change:

  1. Remove the need to disable SC2086 or add a comment explaining why it needs to be disabled.
  2. Retain the previous behaviour of uploading the test-results trace file, as it serves a purpose beyond snapshot test debugging.

The rest are clarifications/questions/nit picks. Looks good otherwise though 🙂

documentation/frontend/reference/testing_guidelines.md Outdated Show resolved Hide resolved
documentation/frontend/reference/testing_guidelines.md Outdated Show resolved Hide resolved
.github/workflows/ci_cd.yml Outdated Show resolved Hide resolved
.github/workflows/ci_cd.yml Outdated Show resolved Hide resolved
.github/workflows/ci_cd.yml Outdated Show resolved Hide resolved
@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch from d0c8f98 to d4eff9b Compare July 2, 2024 10:46
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Just two small things on conditions for the steps I am pushing changes for, but otherwise this looks good to me. If we find we need to adjust, this is a perfect base to iterate on (but for now I think this should work just fine and will be a good improvement).

.github/workflows/ci_cd.yml Show resolved Hide resolved
@@ -822,13 +822,32 @@ jobs:
- name: Run Playwright tests
run: just frontend/run ${{ matrix.script }} --workers=2

- name: Check for changes after running tests with -u
id: check_snapshots_changed
Copy link
Contributor

Choose a reason for hiding this comment

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

It occurred to me when thinking about the condition on the step to upload the snapshot diff artefact, that we need this step to run even if there testing step fails. By default, it wouldn't, so if there are failures in addition to changed snapshots, we'd miss the changed snapshots.

Suggested change
id: check_snapshots_changed
id: check_snapshots_changed
if: success() || failure()

Copy link
Contributor

Choose a reason for hiding this comment

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

@madewithkode it looks like this change got lost in the mix, but I think we still need it in the case I mentioned where there are multiple types of failures.

Copy link
Member

@krysal krysal 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 looking very cool! Generating snapshots is quite a consuming task. I tried the flow in #4600 and (with some little changes) it was a breeze to get the updated snapshots.

My only concern is that I did not get failing tests from the changes. They're supposed to fail so you are aware that you need to update them. Are we on the same page here that this is still the expected behavior?

`snapshot_diff.patch.gz`, once downloaded, they can be decompressed and applied
to your working branch by running:

`cat snapshot_diff.patch.gz | ov gunzip -kc - | git apply`
Copy link
Member

Choose a reason for hiding this comment

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

I downloaded it as playwright_vr_snapshot_diff.zip so I had to decompress it twice (first from a .zip and then from a .gz). It seems that the GitHub actions always do the compression for downloads, so we can skip that part.

Suggested change
`cat snapshot_diff.patch.gz | ov gunzip -kc - | git apply`
`cat playwright_vr_snapshot_diff.zip | ov unzip -kc - | git apply`

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

Copy link
Collaborator Author

@madewithkode madewithkode Jul 5, 2024

Choose a reason for hiding this comment

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

Thanks for pointing this out @krysal and you're indeed correct as regards the expected behavior. A slight change to this part of your suggestion would be:

cat *_snapshot_diff.zip | ov unzip -kc - | git apply

This is because the prefix * represents the current playwirght test. It is dynamic(could vary between playwright_vr or storybook) and auto generated in the workflow using a matrix. I'd go ahead and note this on the doc too.

if: success() || failure()
run: |
if [ -n "$(git diff --binary --name-only)" ]; then
git add . && git diff --staged --binary | gzip -f > /tmp/snapshot_diff.patch.gz
Copy link
Member

@krysal krysal Jul 4, 2024

Choose a reason for hiding this comment

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

Let's skip this compression.

Suggested change
git add . && git diff --staged --binary | gzip -f > /tmp/snapshot_diff.patch.gz
git add . && git diff --staged --binary > /tmp/snapshot_diff.patch

- uses: actions/upload-artifact@v4
if: failure()
id: test-results
with:
name: ${{ matrix.name }}_test_results
path: frontend/test-results

# This step only runs if there was a failure and snapshots indeed changed.
Copy link
Member

Choose a reason for hiding this comment

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

Not exactly accurate and mostly redundant. I'd omit the comment altogether.

Suggested change
# This step only runs if there was a failure and snapshots indeed changed.
# This step only runs if snapshots are changed.

@sarayourfriend
Copy link
Contributor

You're right, @krysal, it should still consider it a failure in the check. I guess we can fix that by adding exit 1 to the script that creates the patch diff when there are updated snapshots. Maybe it should also log the list of updated snapshots in that step too.

@madewithkode
Copy link
Collaborator Author

@sarayourfriend I'd think that git diff --binary --name-only in the patch creation script itself already logs the list of updated snapshots. If this is not the case, perhaps something like git diff --binary --name-only | echo should suffice? Also, I agree with exiting with 1 to trigger a failure.

This would be my modification of the patch creation script(let me know if it makes sense):

      if [ -n "$(git diff --binary --name-only)" ]; then

        git add . && git diff --staged --binary > /tmp/snapshot_diff.patch

        echo "snapshots_changed=true" | tee "$GITHUB_OUTPUT"

        echo "The following snapshots were updated:"

        git diff --binary --name-only | echo

        exit 1

      else

        echo "snapshots_changed=false" | tee "$GITHUB_OUTPUT"

      fi

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 8, 2024

Because git diff --binary --name-only is captured in the test expression, it's stdout doesn't go to the actual logs. Or at least, that's my understanding of how output capture in scripts work, and it's how it behaves when I try it locally.

You should force a playwright failure in this PR so we can see how it goes in practice 🙂

The change to the script looks fine, but you don't need to pipe to echo. You'd also need to add --staged in the second invocation if you used your version, git diff only shows unstaged changes by default, and the git add . will have staged everything. Something like this would be a bit more idiomatic bash script-y (I think):

diff_names="$(git diff --binary --name-only)"
if [ -n "$diff_names" ]; then
  # ...
  printf "The following snapshots were updated:\n%s\n" "$diff_names"
  exit 1
else
  # ...
fi

By the way, just to clarify the reason to stage the changes, we want to make sure that new snapshots are included. git diff won't show unstaged changes for new paths. Maybe worth including a comment to explain that in the script, it's probably not obvious why it's needed.

@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch from a566aa6 to f00516f Compare July 10, 2024 09:16
@madewithkode madewithkode requested a review from a team as a code owner July 10, 2024 09:16
@madewithkode madewithkode changed the title Make running Playwright to update snapshots easier [CI WORKFLOW TEST – Don't merge] Make running Playwright to update snapshots easier Jul 10, 2024
@openverse-bot openverse-bot added the 🧱 stack: frontend Related to the Nuxt frontend label Jul 10, 2024
@madewithkode
Copy link
Collaborator Author

madewithkode commented Jul 10, 2024

@sarayourfriend @krysal I have just triggered a playwright failure with the latest workflow changes on this build on this PR. Everything on the workflow works as expected. An update to snapshots triggered a failure and both test results and the snapshot patch/diff were uploaded.

However, I have noted a few things that might require slight changes with the realization that we no longer need to gzip/gunzip as Github CI zips artifacts by default.

After trying to make this work for myself locally while trying as much as I can to assume the role of a new contributor, the following command set seems more intuitive and works out of the box.

unzip -q *_snapshot_diff.zip && git apply snapshot_diff.patch

  • unzip -q *_snapshot_diff.zip basically uses the unzip tool to unpack the contents of *_snapshot_diff.zip.
    The -q option tells unzip to operate quietly, suppressing informational messages during the extraction process.

Note: The ov toolkit does not seem to have the unzip utility installed yet. FWIW, unzip is widely available on most Unix based OS. However if we decide to use ov instead and add unzip to the ov toolkit, the command can easily be modified to ov unzip -q *_snapshot_diff.zip && git apply snapshot_diff.patch

Running the above allowed me to seamlessly apply the downloaded patch without any errors. If this is okayed across board i'd proceed to update the PR with the changes.

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 10, 2024

Awesome, glad the workflow is working!

You can definitely add unzip to ov, that'd be great! Piping the unzipped output would be better, I think, because unzipping two archives that have the same name of file inside the archive will overwrite each other, won't they? If both the storybook and playwright snapshots have diffs, wouldn't they output to the same snapshot_diffs.patch file, and overwrite whichever one the glob chose as second?

To add unzip to ov, add it to the dnf-installed packages in docker/dev_env/Dockerfile. Fedora calls it unzip (you can search packages with ov dnf search):

unzip.x86_64 : A utility for unpacking zip files

After that, this works well for me locally and is nice and succinct. We could probably add a just recipe (or ov alias) for the unzip part too, to make it less fiddly, but not required for this PR if it's a hassle or behaves strangely for whatever reason.

ov unzip -p *_snapshot_diff.zip | git apply

@openverse-bot
Copy link
Collaborator

Based on the contributor urgency of this PR, the following reviewers are being gently reminded to review this PR:

@zackkrida
@AetherUnbound
This reminder is being automatically generated due to the urgency configuration.

Excluding weekend1 days, this PR was ready for review 7 day(s) ago. PRs labelled with contributor urgency are expected to be reviewed within 3 weekday(s)2.

@madewithkode, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings.

Footnotes

  1. Specifically, Saturday and Sunday.

  2. For the purpose of these reminders we treat Monday - Friday as weekdays. Please note that the operation that generates these reminders runs at midnight UTC on Monday - Friday. This means that depending on your timezone, you may be pinged outside of the expected range.

@madewithkode madewithkode marked this pull request as draft July 11, 2024 14:44
@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch from f00516f to 95bc9af Compare July 11, 2024 16:23
@madewithkode madewithkode changed the title [CI WORKFLOW TEST – Don't merge] Make running Playwright to update snapshots easier Make running Playwright to update snapshots easier Jul 11, 2024
@madewithkode madewithkode marked this pull request as ready for review July 11, 2024 16:25
@madewithkode madewithkode marked this pull request as draft July 11, 2024 16:28
@madewithkode madewithkode changed the title Make running Playwright to update snapshots easier [CI WORKFLOW TEST – Don't merge] Make running Playwright to update snapshots easier Jul 11, 2024
@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch 2 times, most recently from ef96e5a to 47827bd Compare July 11, 2024 16:47
@madewithkode madewithkode changed the title [CI WORKFLOW TEST – Don't merge] Make running Playwright to update snapshots easier Make running Playwright to update snapshots easier Jul 11, 2024
@madewithkode
Copy link
Collaborator Author

@sarayourfriend you're indeed correct about the patch overriding possibilities as the actual patches for both tests would aways have same name, thanks for spotting that. This, alongside other suggestions have been addressed and I can confirm that the latest version works as it should locally and can also be confirmed from this test run.

@madewithkode madewithkode marked this pull request as ready for review July 11, 2024 17:04
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM. I have a few bits of feedback on the docs, but none are blocking. The only blocking part is the condition of the snapshot check job, which needs to run if the previous step fails. Otherwise this LGTM!

@@ -822,13 +822,32 @@ jobs:
- name: Run Playwright tests
run: just frontend/run ${{ matrix.script }} --workers=2

- name: Check for changes after running tests with -u
id: check_snapshots_changed
Copy link
Contributor

Choose a reason for hiding this comment

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

@madewithkode it looks like this change got lost in the mix, but I think we still need it in the case I mentioned where there are multiple types of failures.

standard output which is then piped to `git apply`.

```{note}
For the above command to work verbatim for you, make sure to have saved the downloaded artifact to the root of the project from where you're able to run git commands. Otherwise, you'd need to adjust the path to the downloaded file in the command accordingly. Should you settle for the former, keep in mind that you'd also need to delete the file(`*_snapshot_diff.zip`) after successfully applying the changes to avoid committing them.
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately #1986 means we need to manually wrap the text inside code blocks, to at least around 80 characters. It's so tedious I really don't try too hard other than to get it around 80. Still needs to be done, unfortunately.

There's also a space missing between file and the parenthetical after it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also: for the files to be in context for ov, they must be downloaded to the repository. The repository is the only host filesystem location that ov explicitly mounts into the container. Probably worth just adding that to the instructions: download the zipped snapshot patches to the repository root, etc.

Comment on lines 360 to 376
Playwright tests in CI are run with `-u` option by default, this means that
snapshots will automatically be updated for modified parts of the UI if
Playwright detects that. See
[Updating snapshots](/frontend/guides/test.md#updating-snapshots) for more
reading about this.

When this happens, a GitHub comment will appear with a link to download zipped
artifacts named in the form `*_snapshot_diff.zip`. Once downloaded, they can be
decompressed and applied to your working branch by running:

`ov unzip -p *_snapshot_diff.zip | git apply`

The above command basically uses the `unzip` tool to unpack the contents of the
downloaded archive file named `*_snapshot_diff.zip`, the `-p` option prevents it
from actually creating any extracted file but rather prints the contents to the
standard output which is then piped to `git apply`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a nit, and something we need to address more widely in our documentation (it's unfortunately pervasive, including in my own writing), but this would read a lot more clearly in active voice, rather than passive, and even more so as imperative instructions. "Once downloaded, apply them to your working branch by running", etc.


Remember to replace `*_snapshot_diff.zip` with the actual downloaded filename.

Additionally, you might need to replace `ov` with a relative path `./ov` if you do not have `ov` added to your PATH yet.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can leave this out, in favour of the general advice we've written in #4567. We've decided not to have to write this caveat everywhere and just have it in a single place.

@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch from 47827bd to 41a334f Compare July 12, 2024 11:42
@madewithkode
Copy link
Collaborator Author

LGTM. I have a few bits of feedback on the docs, but none are blocking. The only blocking part is the condition of the snapshot check job, which needs to run if the previous step fails. Otherwise this LGTM!

@sarayourfriend If I get you correctly, you basically want a failure() check added to check_snapshots_changed step? If this is correct, this would mean that it'd only run when playwright test step above exits with 1, however, I don't think snapshot changes/updates are typically considered a failure by playwright, so this might mean that the snapshot check step would be skipped if there were no actual test failures but only snapshot changes, which I assume isn't the behavior we want. I stand to be corrected.

@sarayourfriend
Copy link
Contributor

It should be failure() || success(). If the previous step fails on non-snapshot tests, but also includes changes to the snapshots, then we still need the next step to run and generate the diff. It needs to run:

  • When the test run step "succeeds", so we can check for changed snapshots, generate the diff, and fail if necessary
  • When the test run step fails due to non-snapshot related errors, so that we can also check for changed snapshots, generate the diff, etc.

It needs to run in both cases. Right now it will only run in the first one, so that if there are both types of test failures, the CI run would only show non-snapshot related failures.

@madewithkode madewithkode force-pushed the 4535_make_running_playwright_to_update_snapshots_easier branch from 41a334f to 49da491 Compare July 15, 2024 10:02
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM!

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Jul 15, 2024

Docs build failure was in the storybook build part of the job, not the docs changed in this PR. I've just kicked off a re-run of the job. Looks like we got rate limited in the request to GlotPress.

@dhruvkb Maybe we should consider caching translation results for 24 hours and only pulling new ones once a day instead of on every single CI/CD run. I'll write an issue for it. Issue here: #4614

@sarayourfriend sarayourfriend mentioned this pull request Jul 18, 2024
8 tasks
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

Thank you for all the work to get this working, @madewithkode ! This will make the process of updating tests much simpler

@obulat obulat merged commit e6259b4 into main Jul 19, 2024
55 checks passed
@obulat obulat deleted the 4535_make_running_playwright_to_update_snapshots_easier branch July 19, 2024 17:07
obulat added a commit to szymon-polaczy/openverse that referenced this pull request Jul 19, 2024
Signed-off-by: Olga Bulat <obulat@gmail.com>
obulat added a commit that referenced this pull request Jul 20, 2024
* Made the tables responsive using CSS

* Revert "Made the tables responsive using CSS"

This reverts commit e194701.

* Added mobile source table using separate elements

* Linting

* Update snapshots

Signed-off-by: Olga Bulat <obulat@gmail.com>

* Remove snapshots to test #4585

Signed-off-by: Olga Bulat <obulat@gmail.com>

* Added the css nit picks and the accessibility fix for the article element

* Lint

* Readd snapshots

Signed-off-by: Olga Bulat <obulat@gmail.com>

---------

Signed-off-by: Olga Bulat <obulat@gmail.com>
Co-authored-by: Olga Bulat <obulat@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖 aspect: dx Concerns developers' experience with the codebase ✨ goal: improvement Improvement to an existing user-facing feature 🟩 priority: low Low priority and doesn't need to be rushed 🧱 stack: documentation Related to Sphinx documentation 🧱 stack: frontend Related to the Nuxt frontend 🧱 stack: mgmt Related to repo management and automations
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Make running Playwright to update snapshots easier
5 participants