|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import itertools |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +FEATURES = ['rand-std', 'recovery', 'endomorphism', 'lowmemory', 'serde'] |
| 10 | +MSRV = 1.22 |
| 11 | +RUSTC = None |
| 12 | + |
| 13 | + |
| 14 | +def call(command, cwd): |
| 15 | + print(command) |
| 16 | + try: |
| 17 | + assert subprocess.check_call(command, shell=True, cwd=str(cwd)) == 0 |
| 18 | + except subprocess.CalledProcessError as e: |
| 19 | + print('Execution failed, error:', e.returncode, file=sys.stderr) |
| 20 | + exit(e.returncode) |
| 21 | + |
| 22 | + |
| 23 | +def set_rustc(): |
| 24 | + global RUSTC |
| 25 | + try: |
| 26 | + RUSTC = str(subprocess.check_output(["rustc", "--version"])) |
| 27 | + except subprocess.CalledProcessError as e: |
| 28 | + print('Execution failed, error:', e, file=sys.stderr) |
| 29 | + exit(e.returncode) |
| 30 | + |
| 31 | + |
| 32 | +def is_nightly(): |
| 33 | + global RUSTC |
| 34 | + assert RUSTC |
| 35 | + return "nightly" in RUSTC |
| 36 | + |
| 37 | + |
| 38 | +def is_stable(): |
| 39 | + global RUSTC |
| 40 | + global MSRV |
| 41 | + assert RUSTC |
| 42 | + return "nightly" not in RUSTC \ |
| 43 | + and "beta" not in RUSTC \ |
| 44 | + and str(MSRV) not in RUSTC |
| 45 | + |
| 46 | + |
| 47 | +def is_linux(): |
| 48 | + return "linux" in sys.platform |
| 49 | + |
| 50 | + |
| 51 | +def test_features(features, cwd, release=""): |
| 52 | + assert type(features) is list |
| 53 | + print('Running Tests') |
| 54 | + # Get all feature combinations |
| 55 | + for i in range(len(features) + 1): |
| 56 | + for feature_set in itertools.combinations(features, i): |
| 57 | + feature_set = ', '.join(feature_set) |
| 58 | + # Check that all features work even without the std feature. |
| 59 | + call('cargo build {} --verbose --no-default-features --features="{}"'.format(release, feature_set), cwd) |
| 60 | + # Check that all tests pass with all features. |
| 61 | + call('cargo test {} --verbose --features="{}"'.format(release, feature_set), cwd) |
| 62 | + # Check that fuzztarget compiles + links (tests won't pass) with all features. |
| 63 | + call('cargo test {} --verbose --no-run --features="fuzztarget, {}"'.format(release, feature_set), cwd) |
| 64 | + print() |
| 65 | + |
| 66 | + |
| 67 | +def run_examples(cwd, features=None): |
| 68 | + print('Running Examples') |
| 69 | + features = ', '.join(features) |
| 70 | + # Get all examples in the examples dir |
| 71 | + for example in os.scandir(str(cwd.joinpath("examples").resolve())): |
| 72 | + if example.is_file() and example.path.endswith('.rs'): |
| 73 | + # Enable all features, as some examples need specific features(ie recovery and rand) |
| 74 | + call('cargo run --verbose --example {} --features="{}"'.format(Path(example.name).stem, features), cwd) |
| 75 | + |
| 76 | + |
| 77 | +def run_doc(cwd, features): |
| 78 | + features = ', '.join(features) |
| 79 | + call('cargo doc --verbose --features="{}"'.format(features), cwd) |
| 80 | + |
| 81 | + |
| 82 | +def install_web(cwd): |
| 83 | + call("CARGO_TARGET_DIR=cargo_web cargo install --verbose --force cargo-web", cwd) |
| 84 | + |
| 85 | + |
| 86 | +def test_web(cwd): |
| 87 | + call("cargo web build --verbose --target=asmjs-unknown-emscripten", cwd) |
| 88 | + call("cargo web test --verbose --target=asmjs-unknown-emscripten", cwd) |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + set_rustc() |
| 93 | + main_features = ['rand-std', 'recovery', 'endomorphism', 'lowmemory', 'serde'] |
| 94 | + sys_features = ['recovery', 'endomorphism', 'lowmemory'] |
| 95 | + dir_path = Path(__file__).absolute().parent |
| 96 | + main_path = dir_path.parent |
| 97 | + sys_path = main_path.joinpath("secp256k1-sys") |
| 98 | + |
| 99 | + test_features(main_features, main_path) |
| 100 | + test_features(main_features, main_path, '--release') |
| 101 | + test_features(sys_features, sys_path) |
| 102 | + test_features(sys_features, sys_path, '--release') |
| 103 | + run_examples(main_path, main_features) |
| 104 | + |
| 105 | + # test benchmarks |
| 106 | + if is_nightly(): |
| 107 | + call("cargo test --verbose --benches --features=unstable, recovery", main_path) |
| 108 | + # test cargo doc |
| 109 | + elif is_stable(): |
| 110 | + run_doc(main_path, main_features) |
| 111 | + run_doc(sys_path, sys_features) |
| 112 | + |
| 113 | + # test no-std |
| 114 | + if is_nightly() and is_linux(): |
| 115 | + path = main_path.joinpath("no_std_test") |
| 116 | + call('cargo run --verbose --release | grep -q "Verified Successfully"', path) |
| 117 | + |
| 118 | + # tes cargo web |
| 119 | + if is_stable() and is_linux(): |
| 120 | + install_web(main_path) |
| 121 | + test_web(main_path) |
| 122 | + test_web(sys_path) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + main() |
0 commit comments