Skip to content

Commit

Permalink
Updated test script, fixed resulting warning errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mich101mich committed Jul 21, 2024
1 parent d0677b6 commit cd822ea
Show file tree
Hide file tree
Showing 16 changed files with 640 additions and 357 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude = ["/.vscode/*", "/.gitignore", "/.github/*", "/*.bat", "/*.sh"]
[dependencies]
sscanf_macro = { path = "sscanf_macro", version = "=0.4.2"}
regex = "1.6.0"
const_format = "0.2.26,<0.2.32" # FIXME: 0.2.32 bumps the msrv
const_format = "0.2.26"
lazy_static = "1.4.0"

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions src/from_scanf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ use crate::errors::FromStrFailedError;
/// The contract is:
/// - `NUM_CAPTURES` **IS EQUAL TO**
/// - the number of consumed elements from the iterator passed to [`from_matches`](FromScanf::from_matches) **IS EQUAL TO**
/// - 1 + the number of unescaped capture groups in [`RegexRepresentation`](crate::RegexRepresentation) (or `{:/.../}`).
/// The 1 is for the whole match, which is a capture group added by `sscanf`.
/// - 1 + the number of unescaped capture groups in [`RegexRepresentation`](crate::RegexRepresentation) (or `{:/.../}`).
/// The 1 is for the whole match, which is a capture group added by `sscanf`.
///
/// All of these are automatically enforced by the derive macro or the [`FromStr`] implementation,
/// which is why they should be preferred over this option.
Expand Down Expand Up @@ -240,6 +240,8 @@ where
Self: crate::RegexRepresentation,
{
let regex = format!("^{}$", Self::REGEX);
#[allow(unused_qualifications)] // would complain about the `crate::` prefix, but we
// specifically want the bundled regex rather than whatever a user has renamed to `regex`
let regex = crate::regex::Regex::new(&regex).unwrap_or_else(|err| {
panic!(
"sscanf: Type {} has invalid RegexRepresentation `{}`: {}",
Expand Down
8 changes: 4 additions & 4 deletions src/regex_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use const_format::formatcp;
///
/// If you do need to implement this trait yourself, note the following:
/// - The regex cannot contain any capture groups (round brackets). If you need to use `( )` in your
/// regex, use `(?: )` instead to make it non-capturing.
/// regex, use `(?: )` instead to make it non-capturing.
/// - Using a raw string literal (`r"..."`) is recommended to avoid having to escape backslashes.
/// - The [`const_format`] crate can be used to combine multiple
/// strings into one, which is useful for complex regexes. This can also be used to combine the
/// existing regex implementation of other types. `sscanf` internally uses `const_format` as well,
/// so a version of it is re-exported under `sscanf::const_format`.
/// strings into one, which is useful for complex regexes. This can also be used to combine the
/// existing regex implementation of other types. `sscanf` internally uses `const_format` as well,
/// so a version of it is re-exported under `sscanf::const_format`.
///
/// ## Example
/// Let's say we want to add a Fraction parser
Expand Down
4 changes: 0 additions & 4 deletions sscanf_macro/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub use r#variant::*;

pub trait Attr: Debug + Display + Copy + Ord + Hash + 'static {
fn all() -> &'static [Self];
fn all_names() -> &'static [&'static str];
fn context() -> Context;

fn as_str(&self) -> &'static str;
Expand Down Expand Up @@ -89,9 +88,6 @@ macro_rules! declare_attr {
fn all() -> &'static [Self] {
Self::ALL
}
fn all_names() -> &'static [&'static str] {
Self::ALL_NAMES
}
fn context() -> super::$context_enum {
super::$context_enum::$context
}
Expand Down
2 changes: 0 additions & 2 deletions sscanf_macro/src/format_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::*;

pub struct FormatString<'a> {
pub src: StrLitSlice<'a>,
pub placeholders: Vec<Placeholder<'a>>,
pub parts: Vec<String>, // contains placeholders.len() + 1 escaped parts
}
Expand Down Expand Up @@ -44,7 +43,6 @@ impl<'a> FormatString<'a> {

parts.push(current_part);
Ok(Self {
src,
placeholders,
parts,
})
Expand Down
2 changes: 2 additions & 0 deletions sscanf_macro/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused)]

use crate::*;

/// A workaround for Spans on stable Rust.
Expand Down
156 changes: 97 additions & 59 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,96 +1,134 @@
#!/bin/bash

OVERWRITE=0
if [[ "$1" == "overwrite" ]]; then
OVERWRITE=1
elif [[ -n "$1" ]]; then
echo "Usage: $0 [overwrite]"
exit 1
fi
set -e

TMP_FILE="/tmp/sscanf_test_out.txt"
########
# functions
########

TMP_FILE="/tmp/sscanf_test_out.txt"
function handle_output {
rm -f "${TMP_FILE}"
while read -r line
while IFS='' read -r line
do
echo "${line}" >> "${TMP_FILE}"

# make sure line is not longer than the terminal width
WIDTH=$(tput cols) # read this again in case the terminal was resized
WIDTH=$((WIDTH - 3)) # leave space for the "..."
TRIMMED_LINE=$(echo "> ${line}" | sed "s/\(.\{${WIDTH}\}\).*/\1.../")
echo -en "\033[2K\r${TRIMMED_LINE}"
tput init # trimmed line may have messed up coloring
echo -en "\033[2K\r"
echo -n "$(cut -c "1-$(tput cols)" <<< "> ${line}")"
done
echo -ne "\033[2K\r";
echo -en "\033[2K\r";
tput init # Reset any coloring
}

function try_silent {
echo "Running $*"
unbuffer "$@" | handle_output
unbuffer "$@" 2>&1 | handle_output
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
cat "${TMP_FILE}"
return 1
fi
}

function assert_no_change {
DIR="$1"
if ! git diff-files --quiet --ignore-cr-at-eol "${DIR}"; then
>&2 echo "Changes in ${DIR} detected, aborting"
exit 1
fi
if [[ -n "$(git ls-files --exclude-standard --others "${DIR}")" ]]; then
>&2 echo "Untracked files in ${DIR} detected, aborting"
exit 1
fi
}

########
# setup
########
BASE_DIR="$(realpath "$(dirname "$0")")"

OVERWRITE=0
if [[ "$1" == "overwrite" ]]; then
OVERWRITE=1
elif [[ -n "$1" ]]; then
echo "Usage: $0 [overwrite]"
exit 1
fi

MSRV=$(grep '^rust-version = ".*"$' "${BASE_DIR}/Cargo.toml" | sed -E 's/^rust-version = "(.*)"$/\1/')
[[ -n "${MSRV}" ]]
echo "Minimum supported Rust version: ${MSRV}"

OUT_DIRS="${BASE_DIR}/test_dirs"
MSRV_DIR="${OUT_DIRS}/msrv"
MSRV_DIR="${OUT_DIRS}/msrv_${MSRV}"
MIN_VERSIONS_DIR="${OUT_DIRS}/min_versions"

for dir in "${MSRV_DIR}" "${MIN_VERSIONS_DIR}"; do
[[ -d "${dir}" ]] && continue
mkdir -p "${dir}"
ln -s "${BASE_DIR}/Cargo.toml" "${dir}/Cargo.toml"
ln -s "${BASE_DIR}/src" "${dir}/src"
ln -s "${BASE_DIR}/tests" "${dir}/tests"
ln -s "${BASE_DIR}/sscanf_macro" "${dir}/sscanf_macro"
ln -s "../../Cargo.toml" "${dir}/Cargo.toml"
ln -s "../../src" "${dir}/src"
ln -s "../../tests" "${dir}/tests"
ln -s "../../sscanf_macro" "${dir}/sscanf_macro"
done

export RUSTFLAGS="-D warnings"
export RUSTDOCFLAGS="-D warnings"

########
# main tests
(
cd "${BASE_DIR}" || (echo "Failed to cd to ${BASE_DIR}"; exit 1)
try_silent cargo update || exit 1
try_silent cargo +stable test || exit 1
try_silent cargo +nightly test || exit 1

if [[ OVERWRITE -eq 1 ]]; then
echo "Trybuild overwrite mode enabled"
export TRYBUILD=overwrite
try_silent cargo +stable test error_message_tests -- --ignored || exit 1
git add tests/fail # stage overwrite changes first, in case `nightly` would undo them
try_silent cargo +nightly test error_message_tests -- --ignored || exit 1
else
try_silent cargo +stable test error_message_tests -- --ignored || exit 1
try_silent cargo +nightly test error_message_tests -- --ignored || exit 1
fi
export RUSTDOCFLAGS="-D warnings"
try_silent cargo +nightly doc --no-deps || exit 1
try_silent cargo +nightly clippy -- -D warnings || exit 1
try_silent cargo +stable fmt --check || exit 1
) || exit 1

(
cd "${BASE_DIR}/sscanf_macro" || (echo "Failed to cd to ${BASE_DIR}/sscanf_macro"; exit 1)
try_silent cargo +nightly clippy -- -D warnings || exit 1
try_silent cargo +stable fmt --check || exit 1
) || exit 1
########
cd "${BASE_DIR}"
try_silent rustup update
try_silent cargo update
try_silent cargo +stable test
try_silent cargo +nightly test

if [[ OVERWRITE -eq 1 ]]; then
echo "Trybuild overwrite mode enabled"
export TRYBUILD=overwrite

# "overwrite" will (as the name implies) overwrite any incorrect output files in the error_message_tests.
# There is however the problem that the stable and nightly versions might have different outputs. If they
# are simply run one after the other, then the second one will overwrite the first one. To avoid this, we
# use git to check if the files have changed after every step.
assert_no_change "tests/fail/**/*.stderr" # Check for initial changes that would skew the later checks

try_silent cargo +stable test error_message_tests -- --ignored
assert_no_change "tests/fail/**/*.stderr"

try_silent cargo +nightly test error_message_tests -- --ignored
assert_no_change "tests/fail/**/*.stderr"
else
try_silent cargo +stable test error_message_tests -- --ignored
try_silent cargo +nightly test error_message_tests -- --ignored
fi
try_silent cargo +nightly doc --no-deps
try_silent cargo +nightly clippy -- -D warnings
try_silent cargo +stable fmt --check


########
# sscanf_macro subdirectory
########
cd "${BASE_DIR}/sscanf_macro"
try_silent cargo +nightly clippy -- -D warnings
try_silent cargo +stable fmt --check

########
# minimum supported rust version
(
cd "${MSRV_DIR}" || (echo "Failed to cd to ${MSRV_DIR}"; exit 1)
try_silent cargo +1.56.0 test || exit 1
) || exit 1
########
cd "${MSRV_DIR}"
try_silent rustup install "${MSRV}"
try_silent cargo "+${MSRV}" test --tests # only run --tests, which excludes the doctests from Readme.md

########
# minimum versions
(
cd "${MIN_VERSIONS_DIR}" || (echo "Failed to cd to ${MIN_VERSIONS_DIR}"; exit 1)
try_silent cargo +nightly -Z minimal-versions update || exit 1
########
cd "${MIN_VERSIONS_DIR}"
try_silent cargo +nightly -Z minimal-versions update

try_silent cargo +stable test || exit 1
try_silent cargo +nightly test || exit 1
) || exit 1
try_silent cargo +stable test
try_silent cargo +nightly test

########
echo "All tests passed!"
File renamed without changes.
Loading

0 comments on commit cd822ea

Please sign in to comment.