Skip to content

[Extension] Extract and test Compliant and Non-compliant code blocks #91

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

x0rw
Copy link
Contributor

@x0rw x0rw commented May 25, 2025

Extract code blocks from .rst files and create a global Rust test file (generated.rs) to check them all together using
rustc --test --edition=2021 exts/rust-code-runner/generated.rs under the hood.

Features:

  • marker comments to hide specific code from being rendered in html:
      .. code-block:: rust

        // HIDDEN START
        use std::fs;
        use std::io::{self, Read};
        // HIDDEN END 

anything between // HIDDEN START and // HIDDEN END will not be rendered but will still be included for compilation in the generated test file.

  • This extension is not tied to the other 'needs' extension so it can be used in other projects that require similar features.
  • A set of functions that parse rustc --error-format=json output and extract minimal error information.

Tasks:

  • Write unit tests -- this mostly uses regex(scary) for extracting code blocks.
  • Comments
  • Refactor
  • Feature-gate running code, for CI integration.

How to test:

Run: ./make.py -c --offline it will notify you in case there is any issue in any code block,
also check exts/rust-code-runner/generated.rs after doing so.

(Not rebased yet)
Resolves #80

Copy link

netlify bot commented May 25, 2025

Deploy Preview for scrc-coding-guidelines ready!

Name Link
🔨 Latest commit b882c1d
🔍 Latest deploy log https://app.netlify.com/projects/scrc-coding-guidelines/deploys/68694de1027678000825062c
😎 Deploy Preview https://deploy-preview-91--scrc-coding-guidelines.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@x0rw x0rw marked this pull request as draft May 25, 2025 22:02
@PLeVasseur
Copy link
Collaborator

Hey @x0rw -- thanks for starting on this!

Could I ask for your reason for choosing the 2021 edition instead of the newer 2024 edition?

@x0rw
Copy link
Contributor Author

x0rw commented May 26, 2025

Could I ask for your reason for choosing the 2021 edition instead of the newer 2024 edition?

I just went with 2021 out of habit.

@PLeVasseur
Copy link
Collaborator

Could I ask for your reason for choosing the 2021 edition instead of the newer 2024 edition?

I just went with 2021 out of habit.

Could we update to the 2024 edition then?

@PLeVasseur
Copy link
Collaborator

Hey @x0rw -- wanted to check in to see if this is a work in progress or ready for review

@x0rw
Copy link
Contributor Author

x0rw commented Jun 7, 2025

It’s still in progress. I need to write some tests for it (haven’t found the time yet) because it can easily fail and cause false alerts, I guess.

@PLeVasseur
Copy link
Collaborator

Okay, thanks for the update.

@x0rw x0rw force-pushed the extension/execute-code-blocks branch from e5df07c to 65a6733 Compare June 25, 2025 22:50
@PLeVasseur
Copy link
Collaborator

Hey @x0rw -- checking in on this PR. How's it going? Anything needed to help? ☺️

@x0rw x0rw force-pushed the extension/execute-code-blocks branch from 97509eb to b882c1d Compare July 5, 2025 16:07
@x0rw
Copy link
Contributor Author

x0rw commented Jul 5, 2025

Hey @PLeVasseur.
It's ready,
Now, it aggregates all the code blocks in a rust project structure and prevents hidden sections from appearing in other builds(html..), then it sanitises these codeb blocks, to remove stuffs like "#[macro_export]" and "#[tokio::main]" that could break the tests.

  • the standard generated cargo.toml can be set to include external crates that are common in the tests like tokio, because i think when we reach Concurrency chapters, we will need external crates for some examples.
  • running ./make.py collects test code blocks in build/rust-code-blocks/ and running ./make.py -c --offline --test-rust-blocks will test them and report the errors back,
    also try to intentionally write fallible code in a code block.
    and tell me if more flags or configurable options are necessary.

@felix91gr
Copy link
Collaborator

It's ready,

Hi @x0rw, sorry to bother you. Should I mark this as ready for review? Since it's currently a Draft PR :3

@x0rw
Copy link
Contributor Author

x0rw commented Aug 8, 2025

Hi @felix91gr 👋🏻
This still needs some improvements and consensus. We need to consider all possible code cases, for example, code that could cause a segmentation fault or other critical issues that might break our tests.
A simple way to address this would be to allow certain code blocks to be marked as non-executable, so they won’t be run during testing.

@PLeVasseur what do you think?

@x0rw x0rw marked this pull request as ready for review August 8, 2025 08:49
@felix91gr
Copy link
Collaborator

code that could cause a segmentation fault or other critical issues that might break our tests

@x0rw you mean like code in a non-compliant example that triggers a panic, that sort of thing?

@felix91gr felix91gr added enhancement New feature or request ci/cd Related to CI/CD labels Aug 11, 2025
@x0rw
Copy link
Contributor Author

x0rw commented Aug 12, 2025

Hi @felix91gr

Triggering a panic is fine because we can set the should_panic attribute(TODO) in the test, the real issues are seg faults and syntaxicaly incorrect code, they will basically crash or prevent our program from compiling(resp), a simple solution i could think of is having a flag like //DONT RUN at the top of the code block to signal that it should not run in our tests.

Sorry for the late reply 😄.

@PLeVasseur
Copy link
Collaborator

PLeVasseur commented Aug 12, 2025

Hey @x0rw -- here's an idea: what if we have folks submit examples using the rustdoc syntax?

They've had to put some thought into attributes to allow for things like this.

For example, pulling from the above doc, we could have bits that are intended to not be compiled / checked be tagged like this:

/// ```ignore

/// fn foo() {

/// ```

And for bits of code that are known to panic like this:

/// ```should_panic

/// assert!(false);

/// ```

(sorry for the weird formatting, I struggled to get this to show up correctly with the "embedded" backticks)

We could consider then on whether to leave these annotations in the rendered version or remove it (using an additional part added to our Sphinx extension)

Thoughts?

tagging @felix91gr as well given he's been interested ☺️

@felix91gr
Copy link
Collaborator

here's an idea: what if we have folks submit examples using the rustdoc syntax?

I think this is brilliant. rustdoc has already more or less managed to solve this

@x0rw

the real issues are seg faults and syntactically incorrect code; they will basically crash or prevent our program from compiling(resp), a simple solution i could think of is having a flag like //DONT RUN at the top of the code block to signal that it should not run in our tests.

I'm not 100% sure about how rustdoc handles segfaulting code. If it doesn't handle it, I think we'd probably want to do something like sandbox the code being run. We might be able to read the program's return value if we do, and do something like assert_eq!(return_value, 1).

Regarding non-compiling code... well, if code doesn't compile, then it can't be either a compliant nor a non-compliant example, right? Since the point of both kinds of examples is that they would compile, and therefore the guidelines (and tooling associated with the guidelines) are needed to differentiate both of them.

Sorry for the late reply 😄.

Dw mate, it's all good :)
Always go at your own pace. This is volunteer work after all ^^

@senier
Copy link

senier commented Aug 21, 2025

As a side note: I did something similar with rustdoc lately (working from a markdown file). As this was a bare metal target, I passed a shell script using the --test-runtool (which would execute Qemu). In that setup, rustdoc would treat any non-zero exit status as a panic (i.e. if you had specified should_panic, this would be treated as a success). Not sure how segfaults are handled when executing on the host.

IMO the should_panic behavior of rustdoc is not ideal for us as any panic (even those unrelated to the point you're trying to make with your non-compliant example) will be accepted. It would be better if we could make sure that the expected panic has happened by matching the panic message. I'm thinking of something like the expected parameter of should_panic.

@x0rw Do you think such a matching feature would be easy to add?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci/cd Related to CI/CD enhancement New feature or request sphinx extension
Development

Successfully merging this pull request may close these issues.

Add infrastructure for how to extract and test compliant and non-compliant code
4 participants