Skip to content

Commit

Permalink
feat(meta): stabalize tests, use nextest for workflows (#171)
Browse files Browse the repository at this point in the history
* feat(cache): import/export feature to stabilize tests

* fix(meta): `--input` instead of `-i` flag

* fix(testdata): test_cache was corrupted

* fix(cache): remove import/export

* perf(meta): use nextest for workflows
  • Loading branch information
Jon-Becker authored Nov 12, 2023
1 parent bb66ca0 commit 52fff73
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 387 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ jobs:

steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: taiki-e/install-action@nextest
- name: Run Tests
run: |
cargo test --release -- test_ --nocapture
cargo nextest r --no-fail-fast
doctest:
runs-on: ubuntu-latest
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ heimdall/scripts/update
heimdall/scripts/git

coverage.xml
*.bin
*.o
false
false/*
Expand Down
119 changes: 10 additions & 109 deletions bifrost/bifrost
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ main() {
;;
-v|--version) shift; TARGET_VERSION=$1 ;;
-B|--binary|--bin) shift; USE_BINARY=true ;;
-b|--bench)
benchmark
exit 0
;;
-t|--test)
test
;;
-h|--help)
usage
exit 0
Expand Down Expand Up @@ -59,15 +52,14 @@ main() {
# remove the source directory if it exists
ensure rm -rf "$BUILD_PATH/heimdall-rs"

# clone heimdall-rs and cd into it
cd $BUILD_PATH
echo "bifrost: cloning 'Jon-Becker/heimdall-rs'."
ensure git clone "https://github.com/Jon-Becker/heimdall-rs" > /dev/null 2>&1

cd "heimdall-rs"

ensure git fetch origin

# if they specified a version, use that
# if they specified a version, checkout that tag or branch
if [ -n "$TARGET_VERSION" ]; then
echo "bifrost: installing version $TARGET_VERSION."

Expand All @@ -83,11 +75,13 @@ main() {
ensure git checkout $tag -b latest > /dev/null 2>&1
fi

# if the user wants to use the precompiled binary, download it
if [ -n "$USE_BINARY" ]; then

# cd into the binary directory
ensure cd $BIFROST_BIN_DIR
echo "bifrost: fetching binary."

# download the binary
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
ensure curl -k -L -s --compressed "https://github.com/Jon-Becker/heimdall-rs/releases/download/$TARGET_VERSION/heimdall-linux-amd64" -o heimdall
elif [[ "$OSTYPE" == "darwin"* ]]; then
Expand All @@ -96,7 +90,6 @@ main() {
echo "bifrost: unsupported operating system: $OSTYPE"
exit 1
fi

echo "bifrost: installing binary."

# make the binary executable
Expand All @@ -113,6 +106,7 @@ main() {
if [ $VERSION -ge 60 ]; then
RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true ensure cargo install --path ./cli --bins --locked --force --root $BIFROST_PATH
else
# using legacy build system
echo "bifrost: installing with old build system."
RUSTFLAGS="-C target-cpu=native -C codegen-units=1" CARGO_PROFILE_RELEASE_LTO=true ensure cargo install --path ./heimdall --locked --force --root $BIFROST_PATH
fi
Expand Down Expand Up @@ -150,120 +144,27 @@ OPTIONS:
-B, --binary Install a precompiled binary instead of building from source
-v, --version Install a specific version
-l, --list List all available versions
-b, --bench Install and run benchmarks
-t, --test Install and run tests
EOF
}

# ensure runs a command and exits if it fails
ensure() {
if ! "$@"; then echo "bifrost: required command '$*' failed."; exit 1; fi
}

# command_exists checks if a command exists
command_exists() {
command -v "$1" > /dev/null 2>&1
}

# requires_cmd checks if a command exists and exits if it doesn't
requires_cmd() {
if ! command_exists "$1"; then
echo "bifrost: '$1' is required but not installed on this system"
exit 1
fi
}

benchmark() {
echo "bifrost: fetching..."
# remove the current heimdall installation if it exists
ensure rm -f "$BIFROST_BIN_DIR/heimdall"

# make the build path if it doesn't exist
BUILD_PATH="${BIFROST_PATH}/build"
if [ ! -d $BUILD_PATH ]; then
ensure mkdir -p $BUILD_PATH
fi

# remove the source directory if it exists
ensure rm -rf "$BUILD_PATH/heimdall-rs"

cd $BUILD_PATH

ensure git clone "https://github.com/Jon-Becker/heimdall-rs" > /dev/null 2>&1

cd "heimdall-rs"

ensure git fetch origin > /dev/null 2>&1

# if they specified a version, use that
if [ -n "$TARGET_VERSION" ]; then
ensure git checkout $TARGET_VERSION > /dev/null 2>&1
else

# checkout the latest tag
tag=$(git describe --tags `git rev-list --tags --max-count=1`)
ensure git checkout $tag -b latest > /dev/null 2>&1
fi

echo "bifrost: compiling..."
ensure cd $BUILD_PATH/heimdall-rs/heimdall
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall > /dev/null 2>&1
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall-config > /dev/null 2>&1
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall-common > /dev/null 2>&1
echo "bifrost: running benchmarks..."

RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall -- benchmark_ | grep -E "±|benchmark_([a-zA-Z_])*:" >> stdout
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall-config -- benchmark_ | grep -E "±|benchmark_([a-zA-Z_])*:" >> stdout
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall-common -- benchmark_ | grep -E "±|benchmark_([a-zA-Z_])*:" >> stdout
clear
echo "bifrost: benchmark results:\n"
cat stdout
rm stdout
exit 0
}

test() {
echo "bifrost: fetching..."

# remove the current heimdall installation if it exists
ensure rm -f "$BIFROST_BIN_DIR/heimdall"

# make the build path if it doesn't exist
BUILD_PATH="${BIFROST_PATH}/build"
if [ ! -d $BUILD_PATH ]; then
ensure mkdir -p $BUILD_PATH
fi

# remove the source directory if it exists
ensure rm -rf "$BUILD_PATH/heimdall-rs"

cd $BUILD_PATH

ensure git clone "https://github.com/Jon-Becker/heimdall-rs" > /dev/null 2>&1

cd "heimdall-rs"

ensure git fetch origin > /dev/null 2>&1

# if they specified a version, use that
if [ -n "$TARGET_VERSION" ]; then
ensure git checkout $TARGET_VERSION > /dev/null 2>&1
else

# checkout the latest tag
tag=$(git describe --tags `git rev-list --tags --max-count=1`)
ensure git checkout $tag -b latest > /dev/null 2>&1
fi

echo "bifrost: compiling..."
ensure cd $BUILD_PATH/heimdall-rs/heimdall
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall > /dev/null 2>&1
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall-config > /dev/null 2>&1
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true ensure cargo build --release --package heimdall-common > /dev/null 2>&1
echo "bifrost: running tests..."

RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall -- test_ --nocapture
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall-config -- test_ --nocapture
RUSTFLAGS="-C target-cpu=native" CARGO_PROFILE_RELEASE_LTO=true cargo test --release --package heimdall-common -- test_ --nocapture
exit 0
}

# run main
main "$@" || exit 1
107 changes: 103 additions & 4 deletions cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::env::home_dir;

use util::*;

pub mod tests;
pub mod util;

#[derive(Debug, Clone, Parser)]
Expand All @@ -20,6 +19,11 @@ pub struct CacheArgs {
pub sub: Subcommands,
}

/// A simple clap subcommand with no arguments
#[derive(Debug, Clone, Parser)]
pub struct NoArguments {}

/// Clap subcommand parser for the cache subcommand
#[derive(Debug, Clone, Parser)]
#[clap(
about = "Manage heimdall-rs' cached objects",
Expand All @@ -37,9 +41,6 @@ pub enum Subcommands {
Size(NoArguments),
}

#[derive(Debug, Clone, Parser)]
pub struct NoArguments {}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Cache<T> {
pub value: T,
Expand Down Expand Up @@ -218,3 +219,101 @@ pub fn cache(args: CacheArgs) -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[allow(deprecated)]
#[cfg(test)]
mod tests {
use crate::{delete_cache, exists, keys, read_cache, store_cache};
use serde::{Deserialize, Serialize};
use std::env::home_dir;

#[test]
fn test_store_cache() {
store_cache("key", "value".to_string(), None);

// assert cached file exists
let home = home_dir().unwrap();
let cache_dir = home.join(".bifrost").join("cache");
let cache_file = cache_dir.join("key.bin");
assert!(cache_file.exists());
}

#[test]
fn test_get_cache() {
store_cache("key3", "value".to_string(), None);
let value = read_cache("key3");
let value: String = value.unwrap();

// assert stored value matches
assert_eq!(value, "value");
}

#[test]
fn test_store_struct() {
#[derive(Serialize, Deserialize, Debug)]
struct TestStruct {
name: String,
age: u8,
}

let test_struct = TestStruct { name: "test".to_string(), age: 1 };

store_cache("struct", test_struct, None);

// assert cached file exists
let home = home_dir().unwrap();
let cache_dir = home.join(".bifrost").join("cache");
let cache_file = cache_dir.join("struct.bin");
assert!(cache_file.exists());
}

#[test]
fn test_get_struct() {
#[derive(Serialize, Deserialize, Debug)]
struct TestStruct {
name: String,
age: u8,
}

let test_struct = TestStruct { name: "test".to_string(), age: 1 };

store_cache("struct2", test_struct, None);
let value = read_cache("struct2");
let value: TestStruct = value.unwrap();

// assert stored value matches
assert_eq!(value.name, "test");
assert_eq!(value.age, 1);
}

#[test]
fn test_keys() {
store_cache("some_key", "some_value", None);
store_cache("some_other_key", "some_value", None);
store_cache("not_a_key", "some_value", None);

assert_eq!(keys("some_"), vec!["some_key", "some_other_key"]);
}

#[test]
fn test_keys_wildcard() {
store_cache("a", "some_value", None);
store_cache("b", "some_value", None);
store_cache("c", "some_value", None);
store_cache("d", "some_value", None);
store_cache("e", "some_value", None);
store_cache("f", "some_value", None);

assert!(["a", "b", "c", "d", "e", "f"]
.iter()
.all(|key| { keys("*").contains(&key.to_string()) }));
}

#[test]
fn test_exists() {
assert!(!exists("does_not_exist"));
store_cache("does_not_exist", "some_value", None);
assert!(exists("does_not_exist"));
delete_cache("does_not_exist");
}
}
Loading

0 comments on commit 52fff73

Please sign in to comment.