#!/bin/bash
set -exuo pipefail

tmpdir=$(mktemp -d)
cd "$tmpdir"

cargo new hello
cd hello

cat <<EOF > src/main.rs
use anyhow::Result;

fn main() -> Result<()> {
    println!("Hello, World!");
    Ok(())
}

#[test]
fn test() {
    assert_eq!(1 + 1, 2);
}
EOF

mkdir -p .cargo
cat <<EOF > .cargo/config.toml
[source.crates-io]
replace-with = "packaged-sources"

[source.packaged-sources]
directory = "vendor"
EOF

mkdir vendor
ln -s /usr/share/cargo/registry/anyhow* vendor/
ln -s /usr/src/rustc-*/library/vendor/* vendor/

cargo add 'anyhow@^1'

RUSTC_BOOTSTRAP=1 cargo check -Zbuild-std --offline
RUSTC_BOOTSTRAP=1 cargo build -Zbuild-std --offline
RUSTC_BOOTSTRAP=1 cargo run -Zbuild-std --offline
