Move .spy playground files in examples and test all examples#481
Move .spy playground files in examples and test all examples#481paugier wants to merge 5 commits intospylang:mainfrom
Conversation
cc5ef8e to
de97778
Compare
|
This is a first step but it might be enough for now. At least, it can be useful to have feedback on this strategy. |
antocuni
left a comment
There was a problem hiding this comment.
thank you a lot for this!
I generally like the approach and I'm super happy to add this functionality.
I have added some minor comments.
One additional discussion point: have you considered to embed the expected output directly in the example files? Something like this:
def main() -> None:
print("hello")
# ==== expected output ====
# helloThis would make the update logic slightly more complex but it has the advantage of avoiding creating a lot of new tiny files.
I'm not 100% sure it's a good idea though, happy to hear your thoughts about it
| @@ -0,0 +1,2 @@ | |||
| 20 | |||
| <spy struct _dict::dict[i32, i32]::_dict({'__ll__': W_Ptr('gc', _dict::dict[i32, i32]::DictData, 0x1001460, length=1)})> | |||
There was a problem hiding this comment.
this is a bit fragile (we should have a better repr for the dict, but that's a different story) because it contains a hardcoded address, which happens to be always the same because malloc on WASM is very deterministic.
If we want to be more robust, we probably want to support some kind of regexp/glob syntax in these "expected" files.
Anyway, not a big deal and not a merge blocker, just something to keep in mind for the future.
There was a problem hiding this comment.
Done with a regexp (line = re.sub(r"0x[0-9a-fA-F]+", "<ADDR>", line)).
| expected_file.read_text() | ||
| ), ( | ||
| f"Output of {spy_file.name} differs from {expected_file.relative_to(EXAMPLES_DIR)}" | ||
| ) |
There was a problem hiding this comment.
could you use spy.util.print_diff to print a readable diff here?
| python examples/update_expected_output.py | ||
|
|
||
| After running, review the diff and commit the updated files. | ||
| """ |
There was a problem hiding this comment.
this works, but I propose a different UI.
What about introducing an option pytest --update-examples or similar?
This way:
- by default,
test_examplechecks the output against the expected output - with
--update-examples, it SAVES the new expected output
This has some nice properties like the builtin ability to update only a single test by using pytest standard filtering mechanism.
There was a problem hiding this comment.
I did that and I agree it's better!
| @@ -0,0 +1,3 @@ | |||
| argv: | |||
| /home/users/augier3pi/dev/spy/examples/exit_code.spy | |||
| exit code: 88 | |||
There was a problem hiding this comment.
how is it possible that this test passes in CI? Doesn't it print a different path? 🤔
There was a problem hiding this comment.
The tests were not run in the CI because of
[tool.pytest.ini_options]
testpaths = ["spy"]
I added an explicit step in the CI for the examples.
There was a problem hiding this comment.
Note that I didn't change testpaths = ["spy"], meaning that just running pytest in the root directory does not run the examples. It seems to me that it is a reasonable behavior. To run the example tests locally:
pytest examples
# or
cd examples
pytest
| 0 | ||
| 1 | ||
| 4 | ||
| 9 |
There was a problem hiding this comment.
I think it's fine to modify the example to print less numbers here :)
de97778 to
16e3c6e
Compare
I don't love the idea. I think the advantage (avoiding few small files in Then I thought about the "advantage" of having the expected output directly in the examples in terms of pedagogy. However, it is not a very nice way to present the output, in particular when the examples will be used in the doc. We could of course trim this part for the doc but it seems to me that it's going to be hacky. It is also not unreasonable to have in future examples with relatively long and not very interesting output, which is not a problem if the output is not embedded in the source file. Therefore, I propose that we keep it simple with standard golden files in |
Fixes #416