1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env bash 2*c2e18aaaSAndroid Build Coastguard Worker 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Workerset -e 18*c2e18aaaSAndroid Build Coastguard Workershopt -s extglob 19*c2e18aaaSAndroid Build Coastguard Worker 20*c2e18aaaSAndroid Build Coastguard Worker# TODO(rbraunstein): This probably drops the line number we fail at. 21*c2e18aaaSAndroid Build Coastguard Worker# Find a better way. 22*c2e18aaaSAndroid Build Coastguard Workerfail_with_message() { 23*c2e18aaaSAndroid Build Coastguard Worker echo "$1" 24*c2e18aaaSAndroid Build Coastguard Worker exit 1 25*c2e18aaaSAndroid Build Coastguard Worker} 26*c2e18aaaSAndroid Build Coastguard Worker 27*c2e18aaaSAndroid Build Coastguard Worker# NOTE: if we want to discern stdout from stderr see: https://stackoverflow.com/questions/962255/how-to-store-standard-error- 28*c2e18aaaSAndroid Build Coastguard Worker# TODO(rbraunstein): Do matrix testing for failures 29*c2e18aaaSAndroid Build Coastguard Worker# similar to: frameworks/native/libs/binder/tests/parcel_fuzzer/test_fuzzer/run_fuzz_service_test.sh 30*c2e18aaaSAndroid Build Coastguard Worker 31*c2e18aaaSAndroid Build Coastguard Workerassert_fails_with_output() { 32*c2e18aaaSAndroid Build Coastguard Worker local command="$1" 33*c2e18aaaSAndroid Build Coastguard Worker local expected_output="$2" 34*c2e18aaaSAndroid Build Coastguard Worker if OUTPUT="$($command 2>&1)" 35*c2e18aaaSAndroid Build Coastguard Worker then 36*c2e18aaaSAndroid Build Coastguard Worker fail_with_message "COMMAND should have failed" 37*c2e18aaaSAndroid Build Coastguard Worker else 38*c2e18aaaSAndroid Build Coastguard Worker echo "$OUTPUT" | grep -q -F "$expected_output" || 39*c2e18aaaSAndroid Build Coastguard Worker (echo "actual output doesn't match expectation:\nactual [$OUTPUT]" && exit 1) 40*c2e18aaaSAndroid Build Coastguard Worker fi 41*c2e18aaaSAndroid Build Coastguard Worker} 42*c2e18aaaSAndroid Build Coastguard Worker 43*c2e18aaaSAndroid Build Coastguard Workerassert_ok_with_output() { 44*c2e18aaaSAndroid Build Coastguard Worker local command="$1" 45*c2e18aaaSAndroid Build Coastguard Worker local expected_output="$2" 46*c2e18aaaSAndroid Build Coastguard Worker if OUTPUT="$($command 2>&1)" 47*c2e18aaaSAndroid Build Coastguard Worker then 48*c2e18aaaSAndroid Build Coastguard Worker echo "$OUTPUT" | grep -q -F "$expected_output" || 49*c2e18aaaSAndroid Build Coastguard Worker (echo "actual output doesn't match expectation:\nactual [$OUTPUT]" && exit 1) 50*c2e18aaaSAndroid Build Coastguard Worker else 51*c2e18aaaSAndroid Build Coastguard Worker fail_with_message "COMMAND should have passed, not exit with exit code: $?" 52*c2e18aaaSAndroid Build Coastguard Worker fi 53*c2e18aaaSAndroid Build Coastguard Worker} 54*c2e18aaaSAndroid Build Coastguard Worker 55*c2e18aaaSAndroid Build Coastguard Worker# test bad option 56*c2e18aaaSAndroid Build Coastguard Workerassert_fails_with_output \ 57*c2e18aaaSAndroid Build Coastguard Worker "./adevice --should-fail" \ 58*c2e18aaaSAndroid Build Coastguard Worker "unexpected argument '--should-fail'" 59*c2e18aaaSAndroid Build Coastguard Worker 60*c2e18aaaSAndroid Build Coastguard Worker 61*c2e18aaaSAndroid Build Coastguard Worker# test help 62*c2e18aaaSAndroid Build Coastguard Workerassert_ok_with_output \ 63*c2e18aaaSAndroid Build Coastguard Worker "./adevice --help" \ 64*c2e18aaaSAndroid Build Coastguard Worker "Usage: adevice [OPTIONS] <COMMAND>" \ 65*c2e18aaaSAndroid Build Coastguard Worker 66*c2e18aaaSAndroid Build Coastguard Worker# test bare command is help 67*c2e18aaaSAndroid Build Coastguard Worker# no subcommand is like --help, but exits non-zero 68*c2e18aaaSAndroid Build Coastguard Workerassert_fails_with_output \ 69*c2e18aaaSAndroid Build Coastguard Worker "./adevice" \ 70*c2e18aaaSAndroid Build Coastguard Worker "Usage: adevice [OPTIONS] <COMMAND>" \ 71*c2e18aaaSAndroid Build Coastguard Worker 72*c2e18aaaSAndroid Build Coastguard Worker 73*c2e18aaaSAndroid Build Coastguard Worker# test help with PRODUCT_OUTPUT 74*c2e18aaaSAndroid Build Coastguard Worker(export ANDROID_PRODUCT_OUT=something 75*c2e18aaaSAndroid Build Coastguard Worker assert_ok_with_output \ 76*c2e18aaaSAndroid Build Coastguard Worker "./adevice --help" \ 77*c2e18aaaSAndroid Build Coastguard Worker "Usage: adevice [OPTIONS] <COMMAND>") 78*c2e18aaaSAndroid Build Coastguard Worker 79*c2e18aaaSAndroid Build Coastguard Worker# Test --help without PRODUCT_OUTPUT set 80*c2e18aaaSAndroid Build Coastguard Worker# TODO(rbraunstein): matrix test across env variables, don't replicate ugly test code. 81*c2e18aaaSAndroid Build Coastguard Worker(export ANDROID_PRODUCT_OUT= 82*c2e18aaaSAndroid Build Coastguard Workerassert_ok_with_output \ 83*c2e18aaaSAndroid Build Coastguard Worker "./adevice --help" \ 84*c2e18aaaSAndroid Build Coastguard Worker "Usage: adevice [OPTIONS] <COMMAND>") 85*c2e18aaaSAndroid Build Coastguard Worker 86*c2e18aaaSAndroid Build Coastguard Worker 87*c2e18aaaSAndroid Build Coastguard Worker# TODO(rbraunstein): Add more tests, like passing a needed subcommand. 88*c2e18aaaSAndroid Build Coastguard Worker# TODO(rbraunstein): Find a framework so each test case reports pass. 89