#!/bin/bash
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

C_YELLOW='\033[1;33m'
C_RESET='\033[0m'

set -eux
cd smithy-rs

# Make aws-config (which depends on generated services) available to additional checks
mkdir -p aws/sdk/build
mv ../aws-sdk-smoketest aws/sdk/build/aws-sdk

echo -e "${C_YELLOW}# Testing aws-config...${C_RESET}"
pushd "aws/rust-runtime/aws-config" &>/dev/null

echo "${C_YELLOW}## Running 'cargo clippy'${C_RESET}"
cargo clippy --all-features

echo "${C_YELLOW}## Running 'cargo test'${C_RESET}"
cargo test --all-features

echo "${C_YELLOW}## Running 'cargo doc'${C_RESET}"
cargo doc --no-deps --document-private-items --all-features

echo "${C_YELLOW}## Running 'cargo minimal-versions check'${C_RESET}"
cargo +"${RUST_NIGHTLY_VERSION}" minimal-versions check --all-features

echo "${C_YELLOW}## Checking for external types in public API${C_RESET}"
cargo "+${RUST_NIGHTLY_VERSION:-nightly}" check-external-types --all-features --config external-types.toml

echo "${C_YELLOW}## Checking for duplicate dependency versions in the normal dependency graph with all features enabled${C_RESET}"
cargo tree -d --edges normal --all-features

# the crate has `allow-compilation` which needs to be deprecated

echo "${C_YELLOW}## Checking every combination of features${C_RESET}"
cargo hack check --feature-powerset --exclude-all-features --exclude-features allow-compilation

echo "${C_YELLOW}## Testing each feature in isolation${C_RESET}"
# these features are missed by the following check because they are grouped
cargo hack test --each-feature --include-features rt-tokio,client-hyper,rustls --exclude-no-default-features

# This check will check other individual features and no-default-features
# grouping these features because they don't interact
cargo hack test --feature-powerset --exclude-all-features --exclude-features allow-compilation --group-features rt-tokio,client-hyper,rustls

echo "${C_YELLOW}## Checking the wasm32-unknown-unknown and wasm32-wasi targets${C_RESET}"
cargo check --target wasm32-unknown-unknown --no-default-features
cargo check --target wasm32-wasi --no-default-features

# TODO(https://github.com/smithy-lang/smithy-rs/issues/2499): Uncomment the following once aws-config tests compile for WASM
# echo "${C_YELLOW}## Testing the wasm32-unknown-unknown and wasm32-wasi targets${C_RESET}"
# wasm-pack test --node -- --no-default-features
# cargo test --target wasm32-wasi --no-default-features

popd &>/dev/null
