Skip to content

Commit

Permalink
Merge pull request #1 from pluots/fix-ci
Browse files Browse the repository at this point in the history
Fix setup failures in CI
  • Loading branch information
tgross35 authored Oct 23, 2023
2 parents 21bc8e2 + 3f18398 commit 185a497
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 82 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: List files
run: |
pwd
ls
with:
submodules: true
- uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview
Expand All @@ -70,8 +68,8 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
key: ${{ matrix.os }}
- run: cargo build # tests need prebuild so
- run: cargo llvm-cov nextest --lcov --output-path lcov.info
- run: cargo build # tests need prebuild .so for tests
- run: cargo llvm-cov nextest --no-fail-fast --lcov --output-path lcov.info
- run: cargo test --doc
- name: Upload coverage data to codecov
uses: codecov/codecov-action@v3
Expand Down
1 change: 0 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[submodule "rcrypto-sys/tests/c/unity"]
path = rcrypto-sys/tests/c/unity
url = https://github.com/ThrowTheSwitch/Unity.git
shallow = true
8 changes: 8 additions & 0 deletions rcrypto-sys/.clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CompileFlags:
Add:
- "-I../"
- "-I."
- "-Itests/c"
- "-Ic"
- "-Itests/c/unity"
- "-Ic/unity"
2 changes: 1 addition & 1 deletion rcrypto-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[lib]
# crate-type = ["cdylib"]
# FIXME: how do we make this cdylib only?
# https://github.com/rust-lang/cargo/issues/9096
crate-type = ["cdylib", "lib"]

[dependencies]
Expand Down
72 changes: 0 additions & 72 deletions rcrypto-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fn main() {
const TEST_PREFIX: &str = "// autogenerated file, do not edit manually
// one test is generated for each `.c` file
";

#[cfg(windows)]
Expand All @@ -26,81 +25,10 @@ fn update_tests() {
let out_dir = PathBuf::from(&env::var("OUT_DIR").unwrap());
let out_file = out_dir.join("auto_suite.rs");
let test_dir = crate_dir.join("tests");
// println!("cargo:rerun-if-changed={}", suite_dir.to_string_lossy());
let test_paths = fs::read_dir(&test_dir).unwrap();

let mut to_write = TEST_PREFIX.to_owned();

indoc::writedoc!(
to_write,
"
// use std::io::prelude::*;
use std::process::Command;
use std::sync::Once;
static MAKE: Once = Once::new();
fn run_setup() {{
MAKE.call_once(|| {{
let mut cmd = Command::new(\"make\");
cmd.args([\"-C\", \"{test_dir}\"].as_slice());
cmd.env(\"OUT_DIR\", \"{out_dir}\");
run_cmd_else_quit(&mut cmd, \"makefile\");
}})
}}
/// Run a command, if it fails print its output
fn run_cmd_else_quit(cmd: &mut Command, fname: &str) {{
let out = cmd
.output()
.unwrap_or_else(|e| panic!(\"failed to run command {{cmd:?}}: {{e}}\"));
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
let combined = format!(\"{{stdout}}{{stderr}}\");
let mut has_failures = false;
if combined.contains(\"IGNORE\") ||
combined.contains(\"FAIL\") ||
combined.contains(\"PASS\")
{{
// using output from unity, format it nicely
println!(\" -------- unity ignores --------\");
combined.lines()
.filter(|line| line.contains(\"IGNORE\"))
.for_each(|line| println!(\" {{line}}\"));
println!(\n\" -------- unity failures --------\");
combined.lines()
.filter(|line| line.contains(\"FAIL\"))
.for_each(|line| {{
println!(\" {{line}}\");
has_failures = true;
}});
println!(\n\" -------- unity passed --------\");
combined.lines()
.filter(|line| line.contains(\"PASS\"))
.for_each(|line| println!(\" {{line}}\"));
}} else {{
// For other failures, just propegate the output
println!(\"{{stdout}}\");
eprintln!(\"{{stderr}}\");
}}
println!();
if !out.status.success() || has_failures {{
panic!(\"{{fname}} failed with status {{}}\", out.status);
}}
}}
",
test_dir = test_dir.to_string_lossy(),
out_dir = out_dir.to_string_lossy(),
)
.unwrap();

for path in test_paths {
let path = path.unwrap().path();
let fname = path.file_name().unwrap().to_string_lossy();
Expand Down
14 changes: 12 additions & 2 deletions rcrypto-sys/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ else
target_extension = out
endif

.PHONY: check-submodules
.PHONY: clean
.PHONY: build

Expand Down Expand Up @@ -56,13 +57,22 @@ ldflags = -L$(dir_sharedlib) -l$(lib_name) $(LDFLAGS)
test_exe_files = $(patsubst $(dir_tests)%.c,$(dir_testout)test_%.$(target_extension),$(test_src_files))
build_paths = $(dir_build) $(dir_testout)

build: $(build_paths) $(test_exe_files)
.check-submodules:
@if git submodule status | egrep -q '^[-+]' ; then \
echo "ERROR: submodules need to be initialized"; \
else \
echo "submodules up to date"; \
fi

build: .check-submodules $(build_paths) $(test_exe_files)
@echo generating C output to $(outdir)

# make all test executables
$(dir_testout)test_%.$(target_extension): $(dir_build)unity.o \
$(dir_build)test_framework.o $(dir_build)test_%.o
$(link) $(ldflags) -o $@ $^
pwd
ls -l $(dir_sharedlib)
$(link) -o $@ $^ $(ldflags)

# make all test object files
$(dir_build)test_%.o:: $(dir_tests)%.c
Expand Down
80 changes: 80 additions & 0 deletions rcrypto-sys/tests/c_runner.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,82 @@
// Setup lives in this file

use std::env;
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::Once;

static MAKE: Once = Once::new();

fn run_setup() {
let out_dir = env!("OUT_DIR");
let crate_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let test_dir = crate_dir.join("tests");

MAKE.call_once(|| {
println!("using output directory {out_dir}");
let mut cmd = Command::new("make");
cmd.args(
[
"-C",
&test_dir.to_string_lossy(),
&format!("OUT_DIR={out_dir}"),
]
.as_slice(),
);
cmd.stdout(Stdio::inherit());
cmd.stderr(Stdio::inherit());
let out = cmd.output().expect("failed to run Makefile");
assert!(out.stdout.is_empty());
assert!(out.stderr.is_empty());
})
}

/// Run a command, if it fails print its output
fn run_cmd_else_quit(cmd: &mut Command, fname: &str) {
let out = cmd
.output()
.unwrap_or_else(|e| panic!("failed to run command {cmd:?}: {e}"));

let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);

let combined = format!("{stdout}{stderr}");
let mut has_failures = false;

if combined.contains("IGNORE") || combined.contains("FAIL") || combined.contains("PASS") {
// using output from unity, format it nicely
println!("> -------- unity ignored --------");
combined
.lines()
.filter(|line| line.contains("IGNORE"))
.for_each(|line| println!("> {line}"));

println!("\n> -------- unity failures --------");
combined
.lines()
.filter(|line| line.contains("FAIL"))
.for_each(|line| {
println!("> {line}");
has_failures = true;
});

println!("\n> -------- unity passed --------");
combined
.lines()
.filter(|line| line.contains("PASS"))
.for_each(|line| println!("> {line}"));
} else {
// For other failures, just propegate the output
println!("{stdout}");
eprintln!("{stderr}");
}

println!();

if !out.status.success() || has_failures {
panic!("{fname} failed with status {}", out.status);
}
}

// include tests generated by `build.rs`, C test suite runner
include!(concat!(env!("OUT_DIR"), "/auto_suite.rs"));

0 comments on commit 185a497

Please sign in to comment.