Extract decode_typed build, put integration tests into bin/decode_typed_test
Closes #15.
This commit is contained in:
parent
ce4359d1e2
commit
23501315da
3 changed files with 128 additions and 58 deletions
57
bin/build_decode_typed
Executable file
57
bin/build_decode_typed
Executable file
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
REPO_DIR="$(cd $(dirname ${BASH_SOURCE[0]} )/..; pwd )"
|
||||
|
||||
DECODE_TYPE_CRATE_DIR="${DECODE_TYPE_CRATE_DIR?You must set this environment variable with the path to the crate containing your serialized type.
|
||||
E.g. \'framed-rs/test_type\'}"
|
||||
DECODE_TYPE_NAME="${DECODE_TYPE_NAME?You must set this environment variable with the full name of your serialized type including its module path.
|
||||
E.g. \'framed_test_type::Test\'}"
|
||||
|
||||
TC=${TC:stable}
|
||||
|
||||
echo "DECODE_TYPE_CRATE_DIR = '${DECODE_TYPE_CRATE_DIR}'"
|
||||
echo "DECODE_TYPE_NAME = '${DECODE_TYPE_NAME}'"
|
||||
|
||||
TYPE_CRATE_NAME="$(cargo read-manifest --manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" | jq '.name' -r)"
|
||||
|
||||
TMP_DIR="${REPO_DIR}/target/decode_typed/tmp"
|
||||
|
||||
mkdir -p "${TMP_DIR}";
|
||||
rm -f "${TMP_DIR}/"*;
|
||||
|
||||
export FRAMED_DECODE_DYNAMIC_RS="${TMP_DIR}/dynamic_${DECODE_TYPE_NAME}.rs";
|
||||
cat > "${FRAMED_DECODE_DYNAMIC_RS}" <<EOF
|
||||
// Generated by framed-rs/bin/decode_typed, your changes will be overwritten.
|
||||
extern crate ${TYPE_CRATE_NAME};
|
||||
|
||||
use ${DECODE_TYPE_NAME} as UserType;
|
||||
|
||||
#[allow(dead_code)]
|
||||
static USER_TYPE_NAME: &'static str = "${DECODE_TYPE_NAME}";
|
||||
EOF
|
||||
|
||||
|
||||
# Build user type crate.
|
||||
cargo +${TC} rustc \
|
||||
--manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" \
|
||||
--lib \
|
||||
${DECODE_TYPE_CARGO_FLAGS} \
|
||||
-- \
|
||||
-o "${TMP_DIR}/${TYPE_CRATE_NAME}.rlib" \
|
||||
${DECODE_TYPE_RUSTC_FLAGS} \
|
||||
< /dev/null;
|
||||
TYPE_CRATE_RLIB="$(ls "${TMP_DIR}"/lib${TYPE_CRATE_NAME}*.rlib)";
|
||||
|
||||
# Build decode bin.
|
||||
cargo +${TC} rustc \
|
||||
--manifest-path "${REPO_DIR}/decode/Cargo.toml" \
|
||||
--bin framed_decode \
|
||||
${DECODE_BIN_CARGO_FLAGS} \
|
||||
-- \
|
||||
-o "${TMP_DIR}/decode" \
|
||||
--extern ${TYPE_CRATE_NAME}="${TYPE_CRATE_RLIB}" \
|
||||
--emit obj \
|
||||
${DECODE_BIN_RUSTC_FLAGS} \
|
||||
< /dev/null;
|
||||
DECODE_PATH="$(find "${TMP_DIR}/decode"* -executable -type f)";
|
|
@ -3,58 +3,7 @@ set -e
|
|||
|
||||
REPO_DIR="$(cd $(dirname ${BASH_SOURCE[0]} )/..; pwd )"
|
||||
|
||||
DECODE_TYPE_CRATE_DIR="${DECODE_TYPE_CRATE_DIR?You must set this environment variable with the path to the crate containing your serialized type.
|
||||
E.g. \'framed-rs/test_type\'}"
|
||||
DECODE_TYPE_NAME="${DECODE_TYPE_NAME?You must set this environment variable with the full name of your serialized type including its module path.
|
||||
E.g. \'framed_test_type::Test\'}"
|
||||
|
||||
TC=${TC:stable}
|
||||
|
||||
echo "DECODE_TYPE_CRATE_DIR = '${DECODE_TYPE_CRATE_DIR}'"
|
||||
echo "DECODE_TYPE_NAME = '${DECODE_TYPE_NAME}'"
|
||||
|
||||
TYPE_CRATE_NAME="$(cargo read-manifest --manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" | jq '.name' -r)"
|
||||
|
||||
TMP_DIR="${REPO_DIR}/target/decode_typed/tmp"
|
||||
|
||||
mkdir -p "${TMP_DIR}";
|
||||
rm -f "${TMP_DIR}/"*;
|
||||
|
||||
export FRAMED_DECODE_DYNAMIC_RS="${TMP_DIR}/dynamic_${DECODE_TYPE_NAME}.rs";
|
||||
cat > "${FRAMED_DECODE_DYNAMIC_RS}" <<EOF
|
||||
// Generated by framed-rs/bin/decode_typed, your changes will be overwritten.
|
||||
extern crate ${TYPE_CRATE_NAME};
|
||||
|
||||
use ${DECODE_TYPE_NAME} as UserType;
|
||||
|
||||
#[allow(dead_code)]
|
||||
static USER_TYPE_NAME: &'static str = "${DECODE_TYPE_NAME}";
|
||||
EOF
|
||||
|
||||
|
||||
# Build user type crate.
|
||||
cargo +${TC} rustc \
|
||||
--manifest-path "${DECODE_TYPE_CRATE_DIR}/Cargo.toml" \
|
||||
--lib \
|
||||
${DECODE_TYPE_CARGO_FLAGS} \
|
||||
-- \
|
||||
-o "${TMP_DIR}/${TYPE_CRATE_NAME}.rlib" \
|
||||
${DECODE_TYPE_RUSTC_FLAGS} \
|
||||
< /dev/null;
|
||||
TYPE_CRATE_RLIB="$(ls "${TMP_DIR}"/lib${TYPE_CRATE_NAME}*.rlib)";
|
||||
|
||||
# Build decode bin.
|
||||
cargo +${TC} rustc \
|
||||
--manifest-path "${REPO_DIR}/decode/Cargo.toml" \
|
||||
--bin framed_decode \
|
||||
${DECODE_BIN_CARGO_FLAGS} \
|
||||
-- \
|
||||
-o "${TMP_DIR}/decode" \
|
||||
--extern ${TYPE_CRATE_NAME}="${TYPE_CRATE_RLIB}" \
|
||||
--emit obj \
|
||||
${DECODE_BIN_RUSTC_FLAGS} \
|
||||
< /dev/null;
|
||||
DECODE_PATH="$(find "${TMP_DIR}/decode"* -executable -type f)";
|
||||
source "${REPO_DIR}/bin/build_decode_typed"
|
||||
|
||||
echo "";
|
||||
echo "";
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
set -ex
|
||||
set -e
|
||||
|
||||
# A manual test for framed_decode.
|
||||
# Run tests for framed_decode.
|
||||
|
||||
REPO_DIR="$(cd $(dirname ${BASH_SOURCE[0]} )/..; pwd )"
|
||||
cd ${REPO_DIR};
|
||||
|
@ -12,7 +12,71 @@ export DECODE_TYPE_CRATE_DIR=${REPO_DIR}/test_type
|
|||
export DECODE_TYPE_NAME=framed_test_type::Test
|
||||
export TC
|
||||
|
||||
cargo +${TC} run -p framed_test_type > target/test_output;
|
||||
cat target/test_output | hd;
|
||||
cat target/test_output | ${REPO_DIR}/bin/decode_typed;
|
||||
seq 5 | xargs -I% cat target/test_output | ${REPO_DIR}/bin/decode_typed;
|
||||
TEST_TMP="${REPO_DIR}/target/decode_typed_test"
|
||||
|
||||
mkdir -p "${TEST_TMP}";
|
||||
|
||||
source "${REPO_DIR}/bin/build_decode_typed";
|
||||
|
||||
TEST_OUT="${TEST_TMP}/test_out";
|
||||
cargo +${TC} run -p framed_test_type > "${TEST_OUT}";
|
||||
|
||||
TEST_FRAME="$(cat "${TEST_OUT}";)";
|
||||
|
||||
function test_case() {
|
||||
flags="${flags?expected}";
|
||||
expect="${expect?expected}";
|
||||
name="${name?expected}";
|
||||
|
||||
IFS=' ' read -r -a flags_arr <<< "$flags";
|
||||
|
||||
out="$(${DECODE_PATH} "${flags_arr[@]}" 2>&1 < /dev/stdin)";
|
||||
if [[ "$out" == "$expect" ]]; then
|
||||
echo "Test '${name}' OK";
|
||||
else
|
||||
echo "Test '${name}' failed";
|
||||
exit 1;
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
< /dev/null \
|
||||
name="Empty input" flags="" \
|
||||
expect="" test_case;
|
||||
|
||||
( cat "${TEST_OUT}" ) | \
|
||||
name="1 frame --out-format=Debug" flags="" \
|
||||
expect="Test {
|
||||
a: 1,
|
||||
b: 2
|
||||
}" test_case;
|
||||
|
||||
( cat "${TEST_OUT}" ) | \
|
||||
name="1 frame --out-format=Csv" flags="--out-format=Csv" \
|
||||
expect="a,b
|
||||
1,2" test_case;
|
||||
|
||||
( cat "${TEST_OUT}" ) | \
|
||||
name="1 frame --out-format=Json" flags="--out-format=Json" \
|
||||
expect="{\"a\":1,\"b\":2}" test_case;
|
||||
|
||||
( cat "${TEST_OUT}" "${TEST_OUT}" ) | \
|
||||
name="2 frames --out-format=Csv" flags="--out-format=Csv" \
|
||||
expect="a,b
|
||||
1,2
|
||||
1,2" test_case;
|
||||
|
||||
( printf "\x1\x1\x1\x1\x1\x1\x1\x1\x1\x0" ) | \
|
||||
name="Checksum error" flags="" \
|
||||
expect="WARN: Corrupt frame, error: ChecksumError" test_case;
|
||||
|
||||
( printf "\x0" ) | \
|
||||
name="Frame too short" flags="" \
|
||||
expect="WARN: Corrupt frame, error: EofDuringFrame" test_case;
|
||||
|
||||
( cat "${TEST_OUT}"; printf "\x0"; cat "${TEST_OUT}" ) | \
|
||||
name="Output continues after corrupt frame" flags="--out-format=Csv" \
|
||||
expect="a,b
|
||||
1,2
|
||||
WARN: Corrupt frame, error: EofDuringFrame
|
||||
1,2" test_case;
|
||||
|
|
Loading…
Reference in a new issue