-
Notifications
You must be signed in to change notification settings - Fork 27
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 generated code pretty to read (and write) #280
Merged
tonybaloney
merged 7 commits into
tonybaloney:main
from
atifaziz:pretty-code-gen-snapshots
Oct 18, 2024
Merged
Make generated code pretty to read (and write) #280
tonybaloney
merged 7 commits into
tonybaloney:main
from
atifaziz:pretty-code-gen-snapshots
Oct 18, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
atifaziz
commented
Oct 16, 2024
Comment on lines
+50
to
+63
catch (FileNotFoundException ex) when (ex.FileName is { } fn | ||
&& fn.Contains(".received.", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
// `ShouldMatchApproved` deletes the received file when the condition is met: | ||
// https://github.com/shouldly/shouldly/blob/4.2.1/src/Shouldly/ShouldlyExtensionMethods/ShouldMatchApprovedTestExtensions.cs#L70 | ||
// | ||
// `File.Delete` is documented to never throw an exception if the file doesn't exist: | ||
// | ||
// > If the file to be deleted does not exist, no exception is thrown. Source: | ||
// > https://learn.microsoft.com/en-us/dotnet/api/system.io.file.delete?view=net-8.0#remarks | ||
// | ||
// However, `FileNotFoundException` has been observed on some platforms during CI runs | ||
// so we catch it and should be harmless to ignore. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers: See failing run #31639380191 for why this ugly hack was needed; baffles the mind, frankly.
tonybaloney
approved these changes
Oct 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds snapshot-based testing of the generated code and then goes to make the generated code (hopefully) a joy to read and write by formatting with conventional spacing and indentation. This helps with readability for users who may want to step into the code in the debugger and also allows changes to the generate code to be Git-tracked, fully tested and reviewable as part of future PRs. While the integration tests covered the runtime behaviour, this helps to ensure that accidental source changes don't slip by without notice.
The new tests exercise the generated code for all the Python files from the integration tests:
CSnakes.Tests
PythonStaticGeneratorTests
FormatClassFromMethods
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_args.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_basic.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_buffer.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_defaults.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_dependency.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_dicts.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_exceptions.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_false_returns.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_generators.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_keywords.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_none.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_reload.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_reserved.py")
FormatClassFromMethods(resourceName: "CSnakes.Tests.python.test_tuples.py")
The snapshot testing is leveraged from Shouldly using its
ShouldMatchApproved
.This will also eventually help with generating prettier code via a CLI (#192).