Handle TracWiki PageOutline directive#9
Conversation
Also address pytest warnings: convert escaped regexes to raw strings
After mistakenly hit "Resolve conversation" here: https://github.com/chevah/trac-to-github/pull/8/files#r566765180
|
needs-review Would appreciate a look! |
adiroiban
left a comment
There was a problem hiding this comment.
It looks like the right direction, but it needs a bit more work on the testing side.
When implementing a feature try to make a list of low-level requirement to keep you focused on what you should implement. Use the list for writing tests.
If the implementation has any side-effect that don't conflict with the requirements, you don't have to bother writing tests for that side-effect behavior.
Note that the list of requirements is just an example.
You can decide to remove the requirement for handling multiple existing TOC directives, as we don't have that in practice.
Also, you can decide that if a page already has at least one RST local TOC directive, the page is kept as it is.
You can simplify the requirements.
The important part is to have the requirements and write a test for each requirement.
Thanks for the update. Looking forward to the re-review :)
|
|
||
| def test_remove_tracwiki_pageoutline(self): | ||
| """ | ||
| Make sure no TracWiki [[PageOutline]] directives remain. |
There was a problem hiding this comment.
| Make sure no TracWiki [[PageOutline]] directives remain. | |
| It removes any TracWiki [[PageOutline]] directives and replaced it with a single top of the page local TOC. |
There was a problem hiding this comment.
I opted for the docstring from the list below:
Pages that have one or more TracWiki local TOC (PageOutline)
will have them removed and replaced with a single RST local TOC.
| 'Sample content' | ||
| ) |
There was a problem hiding this comment.
Maybe add more, to make sure that it will not remove only the first one.
| 'Sample content' | |
| ) | |
| '[[PageOutline]]\n' | |
| 'Sample content' | |
| '[[PageOutline]]\n' | |
| ) |
| 'Sample content' | ||
| ) | ||
|
|
||
| self.assertConvertedContent( |
There was a problem hiding this comment.
what is the difference between the previous assertion?
There was a problem hiding this comment.
There is an extra space - I found some files where there are 2 spaces instead of 1 between the .. and the contents::. I will split the test case to clarify.
|
|
||
| def test_single_content_directive(self): | ||
| """ | ||
| Ensure only one RST content directive, not multiple. |
There was a problem hiding this comment.
I think that here we need multiple tests as I see more requirements.
You can think of a test as a requirement validator.
And then you test docstring can be written as a requirement.
I see these requirements:
- Pages without RST or TracWiki local TOC markup will have a single RST local TOC inserted at the start of the page, before the first section.
- Pages that already have a RST local TOC at the start of the page are not changed.
- Pages that already have a RST local TOC inside the content (not at the top of the page), or have multiple local TOCs, will have them removed and a new local TOC is inserted at the start of the page.
- Pages that have one or more TracWiki local TOC (PageOutline) will have them removed and replaced with a single RST local TOC.
You can take each requirement and copy it as the docstring for a test.
Try to write a single test first, run it and see it fail and then update the code.
Then continue with the next test, see it fail, fix the code...etc
| Ensure only one RST content directive, not multiple. | ||
| """ | ||
|
|
||
| self.assertConvertedContent( |
There was a problem hiding this comment.
Here a prefer to see a normal assertEqual as otherwise the assertion is confusing in this context.
But you can observe that the assertConvertedContent did its job here, as you only had to update the assertion once, and all the existing content conversion tests didn't require any update.
assertConvertedContent should only be used for content convertion... the loca TOC is not really content, it's more page meta-content.
There was a problem hiding this comment.
I agree. The tradeoff between assertEqual and assertConvertedContent is in readability vs. maintainability; and the TOC tests needed more readability.
| In-place conversion of files; no backup. | ||
| """ | ||
| if '.git' not in path: | ||
| if _path_allowed(path): |
There was a problem hiding this comment.
Minor comment.
I bit of extra verbosity will not hurt, and the is should hint that this returns a boolean, like _is_path_allowed ... or even better:
| if _path_allowed(path): | |
| if _is_rst_file(path): |
| Only work on actual rst and rest documents. | ||
| """ | ||
|
|
||
| return path.endswith('rst') or path.endswith('rest') |
There was a problem hiding this comment.
maybe do case insensitive matching
| return path.endswith('rst') or path.endswith('rest') | |
| path = path.lower() | |
| return path.endswith('rst') or path.endswith('rest') |
|
|
||
| def _path_allowed(path: str): | ||
| """ | ||
| Only work on actual rst and rest documents. |
There was a problem hiding this comment.
maybe :
| Only work on actual rst and rest documents. | |
| Returns `True` if path looks like a ReStructuredText file. |
The docstring should hint how and why to use it.
It shouldn't be a human readable version of the implementation :)
Also, "only work" is a vague ... what does that mean ?
I understand how you ended up with this docstring...but the doctring is not ok.
This is a valid comment for the if _is_path_allowed(path): but with a good name, we shouldn't need a comment.
|
needs-review Handling "multiple local TOCs" test actually simplified the code! I could remove an "if" branch. Thank you! |
adiroiban
left a comment
There was a problem hiding this comment.
Great work. Thanks! I have applied to the wiki.
Scope
This is the easier part of ticket #5 .
Ensure that the RST
.. contents::directive exists on every page, and remove all TracWiki [[PageOutline]] directives.Changes
The changes were implemented by removing all
[[PageOutline]]mentions, and then adding RST.. contents::to every page that is missing it.Several places had the RST directive with an extra space; they are handled.
Also, address
pytestwarnings about escaping by using raw strings when needed for regexes.Also, address a comment I missed about removing
pytestargs from the GitHub Action, now that there is asetup.cfg.What is not done: The harder part of #5: replacing TracWiki
[[TitleIndex]]directives with a list of RST links in a re-runnable and update-able way.How to try and test the changes
reviewers: @adiroiban
Check that all files have a mention of
.. contents::::Meld (or your diff tool of choice) should show the same RST files inside.
Check that no files have more than one instance of
.. contents::: the following command should not return anything:Also, I looked at the diff actual output of the script, and tried out changes in GitHub edit preview::