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