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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion builder/build_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def build_docs(
debug: bool,
offline: bool,
spec_lock_consistency_check: bool,
test_rust_blocks: bool,
) -> Path:
"""
Builds the Sphinx documentation with the specified options.
Expand Down Expand Up @@ -72,6 +73,8 @@ def build_docs(
conf_opt_values.append("offline=1")
if debug:
conf_opt_values.append("debug=1")
if test_rust_blocks:
conf_opt_values.append("test_rust_blocks=1")

# Only add the --define argument if there are options to define
if conf_opt_values:
Expand Down Expand Up @@ -151,6 +154,14 @@ def main(root):
help="build in offline mode",
action="store_true",
)

parser.add_argument(
"--test-rust-blocks",
help="Test extracted rust code blocks using rustc",
default=False,
action="store_true"
)

group = parser.add_mutually_exclusive_group()
parser.add_argument(
"--ignore-spec-lock-diff",
Expand Down Expand Up @@ -192,6 +203,6 @@ def main(root):
update_spec_lockfile(SPEC_CHECKSUM_URL, root / "src" / SPEC_LOCKFILE)

rendered = build_docs(
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug, args.offline, not args.ignore_spec_lock_diff,
root, "xml" if args.xml else "html", args.clear, args.serve, args.debug, args.offline, not args.ignore_spec_lock_diff, args.test_rust_blocks
)

27 changes: 21 additions & 6 deletions exts/coding_guidelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
from .common import logger, get_tqdm, bar_format, logging
from sphinx.domains import Domain

import logging

# Get the Sphinx logger
logger = logging.getLogger('sphinx')
logger.setLevel(logging.ERROR)

class CodingGuidelinesDomain(Domain):
name = "coding-guidelines"
label = "Rust Standard Library"
Expand Down Expand Up @@ -42,6 +48,13 @@ def on_build_finished(app, exception):
def setup(app):

app.add_domain(CodingGuidelinesDomain)

app.add_config_value(
name='test_rust_blocks',
default=False,
rebuild='env'
)

app.add_config_value(
name = "offline",
default=False,
Expand Down Expand Up @@ -73,12 +86,14 @@ def setup(app):
logger.setLevel(logging.INFO)
common.disable_tqdm = True

app.connect('env-check-consistency', guidelines_checks.validate_required_fields)
app.connect('env-check-consistency', fls_checks.check_fls)
app.connect('build-finished', write_guidelines_ids.build_finished)
app.connect('build-finished', fls_linking.build_finished)
app.connect('build-finished', on_build_finished)

# Ignore builds while testing code blocks
if not app.config.test_rust_blocks:
app.connect('env-check-consistency', guidelines_checks.validate_required_fields)
app.connect('env-check-consistency', fls_checks.check_fls)
app.connect('build-finished', write_guidelines_ids.build_finished)
app.connect('build-finished', fls_linking.build_finished)
app.connect('build-finished', on_build_finished)

return {
'version': '0.1',
'parallel_read_safe': True,
Expand Down
26 changes: 26 additions & 0 deletions exts/rust-code-runner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from . import rust_examples_aggregate
from . import rustc
import os

def setup(app):

app.output_rust_file = "build/rust-code-blocks/generated.rs"

# create build dir
if not os.path.exists("build/rust-code-blocks"):
os.makedirs("build/rust-code-blocks")
if os.path.isfile(app.output_rust_file):
with open(app.output_rust_file, 'w'):
pass

# we hook into 'source-read' because data is mutable at this point and easier to parse
# and it also makes this extension indepandant from `needs`.
#
app.connect('source-read', rust_examples_aggregate.preprocess_rst_for_rust_code)

if app.config.test_rust_blocks:
app.connect('build-finished', rustc.check_rust_test_errors)
return {
'version': '0.1',
'parallel_read_safe': False,
}
259 changes: 259 additions & 0 deletions exts/rust-code-runner/generated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
// ==== Code Block 1 ====
#[test]
fn test_block_coding_guidelines_expressions_1() {
#[repr(C)]
struct Base {
position: (u32, u32)
}
}

// ==== Code Block 2 ====
#[test]
fn test_block_coding_guidelines_expressions_2() {
#[repr(C)]
struct Base {
position: (u32, u32)
}
}

// ==== Code Block 1 ====
#[test]
fn test_block_coding_guidelines_macros_1() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 2 ====
#[test]
fn test_block_coding_guidelines_macros_2() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 3 ====
#[test]
fn test_block_coding_guidelines_macros_3() {
// TODO
}

// ==== Code Block 4 ====
#[test]
fn test_block_coding_guidelines_macros_4() {
// TODO
}

// ==== Code Block 5 ====
#[test]
fn test_block_coding_guidelines_macros_5() {
macro_rules! increment_and_double {
($x:expr) => {
{
$x += 1; // mutation is implicit
$x * 2
}
};
}
let mut num = 5;
let result = increment_and_double!(num);
println!("Result: {}, Num: {}", result, num);
// Result: 12, Num: 6
}

// ==== Code Block 6 ====
#[test]
fn test_block_coding_guidelines_macros_6() {
fn increment_and_double(x: &mut i32) -> i32 {
*x += 1; // mutation is explicit
*x * 2
}
let mut num = 5;
let result = increment_and_double(&mut num);
println!("Result: {}, Num: {}", result, num);
// Result: 12, Num: 6
}

// ==== Code Block 7 ====
#[test]
fn test_block_coding_guidelines_macros_7() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 8 ====
#[test]
fn test_block_coding_guidelines_macros_8() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 9 ====
#[test]
fn test_block_coding_guidelines_macros_9() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 10 ====
#[test]
fn test_block_coding_guidelines_macros_10() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 11 ====
#[test]
fn test_block_coding_guidelines_macros_11() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 12 ====
#[test]
fn test_block_coding_guidelines_macros_12() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 13 ====
#[test]
fn test_block_coding_guidelines_macros_13() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 14 ====
#[test]
fn test_block_coding_guidelines_macros_14() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 15 ====
#[test]
fn test_block_coding_guidelines_macros_15() {
#[tokio::main] // non-compliant
async fn main() {
}

// ==== Code Block 16 ====
#[test]
fn test_block_coding_guidelines_macros_16() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 17 ====
#[test]
fn test_block_coding_guidelines_macros_17() {
fn example_function() {
// Non-compliant implementation
}
}

// ==== Code Block 18 ====
#[test]
fn test_block_coding_guidelines_macros_18() {
fn example_function() {
// Compliant implementation
}
}

// ==== Code Block 19 ====
#[test]
fn test_block_coding_guidelines_macros_19() {
#[macro_export]
macro_rules! vec {
( $( $x:expr ),* ) => {
{
let mut temp_vec = Vec::new(); // non-global path
$(
temp_vec.push($x);
)*
temp_vec
}
};
}
}

// ==== Code Block 20 ====
#[test]
fn test_block_coding_guidelines_macros_20() {
#[macro_export]
macro_rules! vec {
( $( $x:expr ),* ) => {
{
let mut temp_vec = ::std::vec::Vec::new(); // global path
$(
temp_vec.push($x);
)*
temp_vec
}
};
}
}

// ==== Code Block 1 ====
#[test]
fn test_block_coding_guidelines_types_and_traits_1() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Potential for silent overflow in release builds
current + velocity
}
}

// ==== Code Block 2 ====
#[test]
fn test_block_coding_guidelines_types_and_traits_2() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Explicitly handle potential overflow with checked addition
current.checked_add(velocity).expect("Position calculation overflowed")
}
}

// ==== Code Block 1 ====
#[test]
fn test_block_process_style_guideline_1() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Potential for silent overflow in release builds
current + velocity
}
}

// ==== Code Block 2 ====
#[test]
fn test_block_process_style_guideline_2() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Explicitly handle potential overflow with checked addition
current.checked_add(velocity).expect("Position calculation overflowed")
}
}

// ==== Code Block 3 ====
#[test]
fn test_block_process_style_guideline_3() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Potential for silent overflow in release builds
current + velocity
}
}

// ==== Code Block 4 ====
#[test]
fn test_block_process_style_guideline_4() {
fn calculate_next_position(current: u32, velocity: u32) -> u32 {
// Explicitly handle potential overflow with checked addition
current.checked_add(velocity).expect("Position calculation overflowed")
}
}

Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy