#!/usr/bin/env python
#
# Copyright (C) 2025 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import subprocess
import sys

"""
This script runs all of Torq's unit tests specified in
either the BUILD or Android.bp file.

All tests can be run with:
    ./torq_test

By default the Bazel build system is used. To use
Android's Soong build system use '--android' flag:
    ./torq_test --android

You can filter to only run specific tests with:
    ./torq_test torq_unit_test device_unit_test
"""
if __name__ == "__main__":
  # Determine which build system to use
  use_android = '--android' in sys.argv

  # Get test filters, excluding the script name and the --android flag
  args = sys.argv[1:]
  if use_android:
      args.remove('--android')
  filter_tests = args

  if not use_android:
    # Default to using the Bazel build system
    cmd = ['bazel', 'test', '--test_output=errors']

    # Apply filters or run all tests
    if len(filter_tests) > 0:
        cmd += [f"//:{test}" for test in filter_tests]
    else:
        cmd.append(':all')

    print(f"Command: {" ".join(cmd)}")
    subprocess.run(cmd)
    sys.exit(0)

  # Find the Android.bp file
  script_dir = os.path.dirname(os.path.abspath(__file__))
  android_bp_path = os.path.join(script_dir, '..', 'Android.bp')

  # Get all test names
  tests = []
  has_pending_test = False
  try:
    with open(android_bp_path, 'r') as f:
      for line in f:
        text = line.strip()
        if 'python_test_host' in text:
            has_pending_test = True
        elif has_pending_test and 'name' in text:
            # parse the test host name
            tests.append(text.split(':')[-1].strip()[1:-2])
            has_pending_test = False
  except FileNotFoundError:
    print(f"Error: Android.bp file not found at: {android_bp_path}")
    sys.exit(1)
  except Exception as e:
    print(f"Error reading Android.bp: {e}")
    sys.exit(1)

  # Filter out unwanted tests
  if len(filter_tests) > 0:
    tests = list(filter(lambda e: e in filter_tests, tests))

  cmd = ['atest'] + tests
  # Run atest for all the tests
  subprocess.run(cmd)
