]> git.proxmox.com Git - dh-cargo.git/blame - cargo-auto-test
Bump debhelper from old 10 to 13.
[dh-cargo.git] / cargo-auto-test
CommitLineData
7cfab449
XL
1#!/bin/bash
2# Run `cargo test` on an installed Debian crate, e.g. in an autopkgtest.
3#
4# Requires dev-dependencies to be installed. If you give extra flags such as
5# "--all-features", then these features' dependencies must also be installed.
0e351150
XL
6#
7# This script also swallows some known false-positive test failures, so you
8# don't have to set test_is_broken everywhere in your debcargo.toml configs.
7cfab449
XL
9
10set -e
11
12pkg="$1"
13ver="$2"
14
15if [ -z "$pkg" -o -z "$ver" ]; then
16 echo >&2 "Usage: $0 <crate> <version> [<extra test flags, e.g. --all-features>]"
17 exit 2
18fi
19shift 2
20
21pkgdir="/usr/share/cargo/registry/${pkg}-${ver}"
22if [ ! -d "$pkgdir" ]; then
23 echo >&2 "crate directory not found: $pkgdir"
24 exit 1
25fi
26cd "$pkgdir"
27
28rundir=$(mktemp -d)
29# https://stackoverflow.com/a/14812383 inside "trap" avoids running handler twice
30trap 'excode=$?; rm -rf "'"$rundir"'"; trap - EXIT' EXIT HUP INT QUIT PIPE TERM
31
32# set envvars necessary for cargo-debian-wrapper
33
34export DEB_CARGO_CRATE="${pkg}_${ver}"
35export CARGO_HOME="$rundir/debian/cargo_home"
36mkdir -p "$CARGO_HOME"
37mkdir -p "$rundir/registry"
38
39. <(dpkg-buildflags --export)
40. <(dpkg-architecture -s)
41RUST_TYPE="$(rustc --version --verbose | sed -ne 's/^host: //p')"
42export DEB_HOST_RUST_TYPE="$RUST_TYPE"
43export DEB_BUILD_RUST_TYPE="$RUST_TYPE"
44export DEB_TARGET_RUST_TYPE="$RUST_TYPE"
45
46export CARGO_TARGET_DIR="$rundir/target"
47export PATH="/usr/share/cargo/bin:$PATH"
48
49# run the test!
50
0e351150
XL
51if [ -t 2 ]; then flags="--color=always"; export RUSTDOCFLAGS="--color=always"; fi
52
53run_test() {
54 local n="$1"
55 shift
56 { cargo test $flags "$@" 2>&1; echo "$?" > "$rundir/run-$n.ec"; } | tee "$rundir/run-$n.log"
57 sed -i -e 's/\x1b\[[0-9;]*m//g' "$rundir/run-$n.log" # rm ansi colour codes
58 local x="$(< "$rundir/run-$n.ec")"
59 if [ "$x" = "0" ]; then
60 return 0
61 fi
62 local e="$(egrep -o '^error\[E[0-9]+\]' "$rundir/run-$n.log" | sort -u | tr '\n' ',')"
63 # some crates like to update their old versions to depend on their new
64 # versions, e.g. rand-core. unfortunately this breaks cargo test, see
65 # https://github.com/rust-lang/cargo/issues/6819. so just ignore them.
66 if [ "$e" = "error[E0463],error[E0465]," ]; then
67 echo "cargo-auto-test: ignore test failure due to upstream #6819: $e"
68 return 0
69 fi
70 return "$x"
71}
72
73set +e
74
7cfab449 75cargo prepare-debian "$rundir/registry" --link-from-system
0e351150
XL
76
77run_test 0 "$@"
78x="$?"
79if [ "$x" = "0" ]; then exit 0; fi
80if ! egrep -q '^error\[E0554\]' "$rundir/run-0.log"; then exit "$x"; fi
81
82echo
83echo "----------------------------------------------------------------"
84echo "cargo-auto-test: re-running test with RUSTC_BOOTSTRAP due to E0554"
85RUSTC_BOOTSTRAP=1 run_test 1 "$@"