Add assert -> writeln transformation magic for runnable unittest examples#1582
Add assert -> writeln transformation magic for runnable unittest examples#1582CyberShadow merged 10 commits intodlang:masterfrom
Conversation
|
This could use a bit more background (for myself included, sorry :)). What problem does this solve again? From my limited understanding of this it really seems like we are fixing this in the wrong place. For example, could this go into DPaste instead? Perhaps a CGI proxy between dlang.org and DPaste? I guess fixing this in the compiler/runtime is off the table.
I don't remember offhand if Digger can cope with one repository's build script modifying another repository, but the test service shouldn't complain. Still, it would be confusing to users: what if you're testing a change, build the website, and suddenly your working directory is dirtied with hundreds of changes that you'd have to carefully clean up so you can commit just your intended changes? |
|
My fixes to libdparse have been merged (thanks a lot for being so reponsive @Hackerpilot), so now you can preview the effects of the script.
No it's my fault for not explaining it properly! The idea is that So this script tries to rewrite assert([2, 1, 4, 3].minElement == 1);
assert([5, 3, 7, 9].enumerate.minElement!"a.value" == tuple(1, 3));would be rewritten as: writeln([2, 1, 4, 3].minElement); // 1
writeln([5, 3, 7, 9].enumerate.minElement!"a.value"); // tuple(1, 3))Providing examples with output is a common style across almost all language and library documentations. A couple of examples: Even all examples on dlang.org (and the examples included in the Ddoc comment) use CC @MartinNowak and @andralex as they also had the same idea.
Well it's really intended to be shown on
Yeah I am quite aware of this downside, but doing this transformation in JavaScript with the examples rendered as syntax-highlighted HTML didn't turn it to be a great idea. |
|
I see, thanks for the explanation. What do you think about integrating it into DDox? |
|
Something seems to have gone terribly wrong here: |
It seems that the switch to DDox won't happen in the foreseeable near future, so this wouldn't allow us to use this "feature" for the default documentation and thus for the majority of all readers/users.
The script automatically adds a CC @s-ludwig |
I found & fixed the problem: [ |
|
Alright, well, I think this is fine provided that we can avoid modifying Phobos source code in-place with the caveat I described. It's just not great that this puts in an additional step between the source code and the final output, which makes the entire thing more complicated, but if that can't be avoided then so be it. So, if it's not too difficult, would be good if the changes were done to another private copy, either by copying the source files and modifying them in-place, or having the tool read from one location and write to another. Otherwise we should probably exclude this from the default target (maybe we should introduce a new target that specifies that the files are built by DAutoTest, so we'd only have to change DAutoTest once). |
A PR which does that as well: #1214 Though, I think the justification for that one is much weaker (which is also why it's been stalled forever). |
Sure! I used
Also as |
|
Yeah, I guess this works, though probably the way I'd do it is a makefile wildcard rule to copy the files that need to be copied. This is probably fine too. |
You know, I don't really care. If you want to, I am happy to change ;-) When I introduced the runnable examples for the prerelease pages, they sat there for half a year without any feedback/notices. Hence I also added this writeln transformation process to the stable pages, because if we decide to go with it, this "testing phase" would be useless. The preview of the changes on DAutoTest is awesome and should be enough to make an informed decision. |
No, that's completely fine. |
CyberShadow
left a comment
There was a problem hiding this comment.
LGTM aside the rsync22 thing.
.gitignore
Outdated
| /.generated | ||
|
|
||
| # DUB binaries | ||
| assert_writeln_magic |
There was a problem hiding this comment.
Nit: absolute paths (starting with /) are a bit better perfomant
assert_writeln_magic.d
Outdated
| /// A single line | ||
| override void visit(const DeclarationOrStatement expr) | ||
| { | ||
| processLastAssert(); |
posix.mak
Outdated
|
|
||
| # Automatically generated directories | ||
| GENERATED=.generated | ||
| PHOBOS_DIR_GENERATED=../phobos.ddoc.tmp |
There was a problem hiding this comment.
I'd say that the .generated directory would be a better fit, to avoid more littering
| ################################################################################ | ||
|
|
||
| # --update allows to copy only the newer files and thus only propagate these | ||
| # changes |
There was a problem hiding this comment.
Why rsync22? Typo? The commands below run rsync.
There was a problem hiding this comment.
Is command like which here?
| lines = File(destFile).byLineCopy.array; | ||
|
|
||
| // it's a good practise to use a common tmp folder -> easier to look at or clean | ||
| tmpDir = buildPath(tempDir, "file_tester", path.stripExtension.replace("/", "_")); |
There was a problem hiding this comment.
tmpDir is only used when writing the file line by line and doing the swap. It will be immediately be removed afterwards. Isn't this the dedicated purpose of a tempDir() or is there another reason to use .generated?
There was a problem hiding this comment.
OK. Currently on DAutoTest .generated is on a RAM disk but /tmp is not, but I should set the TMP variable to fix that.
There was a problem hiding this comment.
(I wouldn't mind changing this)
assert_writeln_magic.d
Outdated
|
|
||
| void resetTestState() | ||
| { | ||
|
|
I didn't do this initially because of build errors due to the hard-coded paths, but I forgot that one can simply overwrite variables in
Yeah :/
Yes |
|
I have no further feedback so the only thing remaining is to squash any comments you think ought to be squashed before merging. |
c2f926c to
142acd0
Compare
Currently |
|
Does dub allow specifying dependencies by commit hash yet? I think Sönke said he'd do it at some point. |
Yes it's on the roadmap, but not implemented (as Sönke is quite busy with Vibe.d and other projects). |
88fbd38 to
9d02049
Compare
In order to have a common style, these variables have been introduced
DMD_STABLE_DIR=${DMD_DIR}-${LATEST}
DRUNTIME_STABLE_DIR=${DRUNTIME_DIR}-${LATEST}
PHOBOS_STABLE_DIR=${PHOBOS_DIR}-${LATEST}
PHOBOS_STABLE_DIR_GENERATED=${PHOBOS_STABLE_DIR}.ddoc.tmp
9d02049 to
b361d73
Compare
Arrgh it seems that there was an error with the DUB version. Fixed & it's green again :) |
|
Nothing else is blocking this, right? I'll be looking forward to removing the intermediate source file generation whenever DDox becomes the main documentation method. BTW, I went ahead and hacked DPaste to accept dtest.dlang.io for cross-side requests, so that previews of runnable examples work. |
Not that I would know of.
Ditto!
Sweet! Thanks a lot! I haven't been able to reach Damian lately, but my wishlist would include:
|
If you want to have a go at it I'll gladly give you access :) |
|
Uh oh, the dlang.org autodeploy build failed: |
|
Right, because the same directory is being shared by multiple users. I'm going to work around this by setting |
I am happy to make a follow-up PR to change from |
| /** | ||
| A simple line-based in-memory representation of a file. | ||
| - will automatically write all changes when the objct is destructed | ||
| - will use a temporary file to do safe, whole file swaps |
There was a problem hiding this comment.
BTW if you wanted safe, atomic file replacements I think writing to <filename>.tmp and renaming over it would be closer to that goal, since POSIX renames are atomic.
There was a problem hiding this comment.
My motivation to save <filename>.tmp in a tmp folder was that the process could be stopped/interrupted during the write of the temporary file.
There was a problem hiding this comment.
Yes, but the same applies to your copy call.
There was a problem hiding this comment.
Yah, renames are atomic only within the same filesystem which is why it's important to put the xyz.tmp file in the same place as xyz (/tmp might be on a different filesystem etc).
BTW the pattern @CyberShadow has saved my behind many times, and is considered par for the course across Facebook eng.
|
This broke the builds script b/c your invocation is relying on some system-wide dmd. Please change it to use |
-> #1592 |
Okay this is a more "sophisticated" approach towards rewriting the AssertExpressions in the runnable examples than the initial regex.
I think we all now the problem: "All test passed" is a quite boring output and not so much than being able to directly change a single character.
The objective of this tool is to be conservative as a broken example looks a lot worse than a few statements that could have potentially been rewritten.
For now:
However
writelntransformationsddocis used to generate the documentation, it's ensured that no invalid transformations happened.@CyberShadow I am not sure on the best way to integrate this into the existing Makefile is. At the moment the tool replaces occurrences directly in the Phobos directory which might be a bit inconvenient if enabled as default. Should we make a copy of the Phobos sources before?
This depends on these two fixes to libdparse (hence blocked):