Skip to content

Commit

Permalink
Check if code is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Jul 11, 2024
1 parent caa4418 commit 723794d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Binary file added add_player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions scripts/check_all_chapters_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# Check that all code use in a chapter is indeed still part
# of the code it is copy-pasted from
#
# Assumes that './scripts/clone_all_chapters.sh' has been run already
#
# ../bevy_tdd_book
# ../bevy_tdd_book_hello_world
# ../bevy_tdd_book_[other chapters]
#
# Usage:
#
# ./scripts/check_all_chapters_code.sh

if [[ "$PWD" =~ scripts$ ]]; then
echo "FATAL ERROR."
echo "Please run the script from the project root. "
echo "Present working director: $PWD"
echo " "
echo "Tip: like this"
echo " "
echo " ./scripts/check_all_chapters_code.sh"
echo " "
exit 42
fi

# ls *.md | grep -v [A-Z] | grep -v "faq\\.md"
Rscript scripts/clone_all_chapters_impl.R


88 changes: 88 additions & 0 deletions scripts/check_all_chapters_code_impl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Current working directory must end with bevy_tdd_book

pwd <- getwd()

if (!stringr::str_detect(pwd, "bevy_tdd_book$")) {
stop("Run this script in the 'bevy_tdd_book' folder")
if (1 == 2) {
setwd("..")
}
}

get_all_chapter_filenames <- function() {
all_md_files <- list.files(pattern = "md")
md_files <- stringr::str_subset(all_md_files, "[A-Z]", negate = TRUE)
md_files <- stringr::str_subset(md_files, "faq.md", negate = TRUE)
md_files <- stringr::str_subset(md_files, "functions.md", negate = TRUE)
md_files

}
chapter_filenames <- get_all_chapter_filenames()


#' Returns a vector of booleans whether a line is code
is_line_code <- function(lines) {
lines
n_lines <- length(lines)
is_code <- rep(FALSE, n_lines)
is_now_code <- FALSE
for (i in seq_along(lines)) {
if (lines[i] == "```rust") {
is_now_code <- TRUE
next
}
if (lines[i] == "```") {
is_now_code <- FALSE
next
}
if (is_now_code) is_code[i] <- TRUE
}
is_code
}

#' Returns all the code that is present in a chapter
extract_all_code_from_chapter <- function(chapter_filename) {
testthat::expect_true(file.exists(chapter_filename))
lines <- readr::read_lines(chapter_filename)
code <- lines[is_line_code(lines)]
code
}

for (chapter_filename in chapter_filenames) {
# chapter_filename <- "add_camera.md"
testthat::expect_true(file.exists(chapter_filename))

chapter_name <- stringr::str_sub(chapter_filename, end = -4)
print(paste0("chapter: ", chapter_name))

code_filename <- paste0(getwd(), "_", chapter_name, "/src/app.rs")
testthat::expect_true(file.exists(code_filename))

chapter_code <- extract_all_code_from_chapter(chapter_filename)
code <- readr::read_lines(code_filename)

if (length(chapter_code) == 0) {
print("No code in chapter yet")
next
}

trimmed_chapter_code <- stringr::str_trim(chapter_code)
trimmed_code <- stringr::str_trim(code)

if (all(trimmed_chapter_code %in% trimmed_code)) {
print("OK")
next
}

print(
paste0(
"ERROR: ",
sum(!trimmed_chapter_code %in% trimmed_code),
"x line found in chapter, but not in code"
)
)
missing_lines <- trimmed_chapter_code[!trimmed_chapter_code %in% trimmed_code]
for (i in seq_along(missing_lines)) {
print(paste0(i, ": ", missing_lines[i]))
}
}

0 comments on commit 723794d

Please sign in to comment.