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

🧹 Reduce duplication in snippet testing #5345

Merged
merged 24 commits into from
Apr 5, 2024

Conversation

rix0rrr
Copy link
Collaborator

@rix0rrr rix0rrr commented Apr 1, 2024

The snippet testing code had some duplication. In this code, we separate out how to find the interesting snippets in the YAML files. For every snippet, we remember where in a YAML file we found it (by keeping track of the keys we used to descend into dictionaries and the indexes we used to descend into lists).

That way, the "restore to English" code can become generic: since every snippet knows where in the YAML structure it comes from, we can automatically find the same snippet in the English language file and copy it over.

How to test

If the tests pass, everything works!

@rix0rrr rix0rrr changed the title 🧹 Remove duplication in snippet testing 🧹 Reduce duplication in snippet testing Apr 2, 2024


def filter_snippets(snippets, level=None, lang=None):
if (lang or level) and os.getenv('CI'):
raise RuntimeError('Whoops, it looks like you left a snippet filter in!')

if lang:
snippets = [(name, snippet) for (name, snippet) in snippets if snippet.language[:2] == lang]
snippets = [s for s in snippets if x.language[:len(lang)] == lang]
Copy link
Collaborator

Choose a reason for hiding this comment

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

x.language should be s.language in this line and the following

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tiens. I thought pylint would catch errors like these.

# English file is always 'en.yaml' in the same dir
en_file = path.join(path.dirname(snippet.filename), 'en.yaml')

# Read English yaml file
original_yaml = YamlFile.for_file(en_file)
original_loc = yaml_locator(snippet, original_yaml)
original_loc = locate_snippet_in_yaml(original_yaml, snippet, yaml_locator)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The yaml_locator does not need to be supplied any more

@@ -9,18 +9,17 @@
- Snippets can hold on to the information where in the YAML file they were
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be scratched now, right :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ha. Of course!

@boryanagoncharenko boryanagoncharenko self-assigned this Apr 3, 2024
@rix0rrr
Copy link
Collaborator Author

rix0rrr commented Apr 4, 2024

Good catches @boryanagoncharenko, thanks! I'm a bit surprised that the static analysis tools we run didn't catch these, since they're pretty obvious.

I've confirmed that pylint doesn't catch them (grr). pylance could potentially do it, but I didn't find out how to run it.

We should probably find a static checker to catch trivial errors?

@rix0rrr
Copy link
Collaborator Author

rix0rrr commented Apr 4, 2024

I've re-added pylint to the build checks, and confirmed that pylint would have caught those errors.

We used to have it, not sure why we removed it?

@rix0rrr
Copy link
Collaborator Author

rix0rrr commented Apr 4, 2024

Shit, pylint was removed in this PR: #5134

@jpelay, do you remember why?

@rix0rrr
Copy link
Collaborator Author

rix0rrr commented Apr 5, 2024

Doesn't seem to be easy to re-enable. My struggles will continue in here: #5377

In the mean time, this one should be good to go.

Copy link
Contributor

mergify bot commented Apr 5, 2024

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit a9a9986 into main Apr 5, 2024
12 checks passed
@mergify mergify bot deleted the reduce-snippet-testing-duplication branch April 5, 2024 07:45
Copy link
Contributor

mergify bot commented Apr 5, 2024

Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants