Skip to content

Commit 4e5b0f9

Browse files
ci: add basic tests for main.R
1 parent d8a8317 commit 4e5b0f9

5 files changed

Lines changed: 174 additions & 0 deletions

File tree

.github/workflows/tests.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
pull_request:
7+
branches: [ main, dev ]
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Parse environment.json and set Docker image
16+
id: docker_image
17+
run: |
18+
IMAGE=$(jq -r '.base_image' .codeocean/environment.json)
19+
IMAGE=${IMAGE//codeocean/nciccbr}
20+
echo "image=$IMAGE" >> $GITHUB_OUTPUT
21+
echo "Using Docker image: $IMAGE"
22+
23+
- name: Run tests in Docker
24+
run: |
25+
docker run --rm \
26+
-v ${{ github.workspace }}:/workspace \
27+
-w /workspace \
28+
${{ steps.docker_image.outputs.image }} \
29+
Rscript tests/testthat.R

tests/test-setup.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env Rscript
2+
3+
cat("Testing basic test setup...\n")
4+
5+
cwd <- getwd()
6+
cat("Current directory:", cwd, "\n")
7+
8+
repo_root_script <- normalizePath(dirname(cwd))
9+
cat("Calculated repo_root:", repo_root_script, "\n")
10+
11+
fixture_file <- file.path(
12+
repo_root_script,
13+
"..",
14+
"code",
15+
"MOSuite",
16+
"tests",
17+
"testthat",
18+
"data",
19+
"moo.rds"
20+
)
21+
cat("\nLooking for fixture data at:", fixture_file, "\n")
22+
cat("Fixture data exists:", file.exists(fixture_file), "\n")
23+
24+
code_main <- file.path(repo_root_script, "..", "code", "main.R")
25+
code_run <- file.path(repo_root_script, "..", "code", "run")
26+
cat("code/main.R exists:", file.exists(code_main), "\n")
27+
cat("code/run exists:", file.exists(code_run), "\n")
28+
29+
if (file.exists(fixture_file)) {
30+
cat("\nTrying to load fixture MOO object...\n")
31+
library(readr)
32+
moo <- readr::read_rds(fixture_file)
33+
cat("Fixture MOO loaded successfully!\n")
34+
cat("MOO class:", class(moo), "\n")
35+
}

tests/testthat.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
library(testthat)
2+
test_dir(here::here('tests/testthat/'))

tests/testthat/helper-cli.R

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
setup_cli_workspace <- function(prefix = "mosuite_plot_expr_heatmap_test_") {
2+
workspace <- tempfile(prefix)
3+
dir.create(workspace)
4+
5+
code_dir <- file.path(workspace, "code")
6+
data_dir <- file.path(workspace, "data")
7+
results_dir <- file.path(workspace, "results")
8+
dir.create(code_dir, recursive = TRUE)
9+
dir.create(data_dir, recursive = TRUE)
10+
dir.create(file.path(results_dir, "figures"), recursive = TRUE)
11+
dir.create(file.path(results_dir, "moo"), recursive = TRUE)
12+
13+
repo_root <- normalizePath(
14+
file.path(testthat::test_path(), "..", ".."),
15+
mustWork = TRUE
16+
)
17+
18+
test_data_file <- file.path(
19+
repo_root,
20+
"code",
21+
"MOSuite",
22+
"tests",
23+
"testthat",
24+
"data",
25+
"moo.rds"
26+
)
27+
28+
expect_true(
29+
file.exists(test_data_file),
30+
info = paste("Test data file should exist at", test_data_file)
31+
)
32+
file.copy(test_data_file, file.path(data_dir, "moo.rds"), overwrite = TRUE)
33+
34+
file.copy(
35+
file.path(repo_root, "code", "main.R"),
36+
file.path(code_dir, "main.R"),
37+
overwrite = TRUE
38+
)
39+
40+
# Keep main.R behavior the same while pointing to this checkout's MOSuite package.
41+
main_copy <- file.path(code_dir, "main.R")
42+
main_lines <- readLines(main_copy)
43+
main_lines <- gsub(
44+
'devtools::load_all("/code/MOSuite")',
45+
sprintf(
46+
'devtools::load_all("%s")',
47+
file.path(repo_root, "code", "MOSuite")
48+
),
49+
main_lines,
50+
fixed = TRUE
51+
)
52+
writeLines(main_lines, main_copy)
53+
54+
list(
55+
workspace = workspace,
56+
code_dir = code_dir,
57+
results_dir = results_dir,
58+
repo_root = repo_root
59+
)
60+
}
61+
62+
expect_plot_created <- function(results_dir) {
63+
plot_path <- file.path(results_dir, "figures", "heatmap", "expr_heatmap.png")
64+
expect_true(file.exists(plot_path), info = "Heatmap plot should be created")
65+
expect_true(
66+
file.info(plot_path)$size > 0,
67+
info = "Heatmap plot should be non-empty"
68+
)
69+
}
70+
71+
common_cli_args <- c(
72+
"--count_type=clean",
73+
"--display_gene_names=FALSE",
74+
"--display_sample_names=TRUE"
75+
)

tests/testthat/test-main.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
test_that("main.R CLI creates expression heatmap plot", {
2+
setup <- setup_cli_workspace("mosuite_plot_expr_heatmap_test_")
3+
on.exit(unlink(setup$workspace, recursive = TRUE), add = TRUE)
4+
5+
old_wd <- getwd()
6+
setwd(setup$code_dir)
7+
on.exit(setwd(old_wd), add = TRUE)
8+
9+
exit_code <- system2("Rscript", args = c("main.R", common_cli_args))
10+
expect_equal(exit_code, 0, info = "main.R should execute without error")
11+
12+
expect_plot_created(setup$results_dir)
13+
})
14+
15+
test_that("run wrapper executes and creates expression heatmap plot", {
16+
setup <- setup_cli_workspace("mosuite_plot_expr_heatmap_run_test_")
17+
on.exit(unlink(setup$workspace, recursive = TRUE), add = TRUE)
18+
19+
file.copy(
20+
file.path(setup$repo_root, "code", "run"),
21+
file.path(setup$code_dir, "run"),
22+
overwrite = TRUE
23+
)
24+
25+
old_wd <- getwd()
26+
setwd(setup$code_dir)
27+
on.exit(setwd(old_wd), add = TRUE)
28+
29+
exit_code <- system2("bash", args = c("run", common_cli_args))
30+
expect_equal(exit_code, 0, info = "run script should execute without error")
31+
32+
expect_plot_created(setup$results_dir)
33+
})

0 commit comments

Comments
 (0)