#!/usr/bin/env vpython3
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Runs all python3-compatible tests in devil."""

import os
import sys
import unittest

_DEVIL_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
_SIX_PATH = os.path.join(_DEVIL_PATH, '..', 'third_party', 'six')

sys.path.append(_DEVIL_PATH)
sys.path.append(_SIX_PATH)
# Import compatible tests here by full module path
import devil.android.apk_helper_test
import devil.android.app_ui_test
import devil.android.battery_utils_test
import devil.android.cpu_temperature_test
import devil.android.decorators_test
import devil.android.device_denylist_test
import devil.android.device_errors_test
import devil.android.device_utils_test
import devil.android.fastboot_utils_test
import devil.android.flag_changer_test
import devil.android.logcat_monitor_test
import devil.android.md5sum_test
import devil.android.perf.perf_control_test
import devil.android.perf.surface_stats_collector_test
import devil.android.sdk.adb_wrapper_test
import devil.android.sdk.shared_prefs_test
import devil.android.tools.device_monitor_test
import devil.android.tools.script_common_test
import devil.android.tools.system_app_test
import devil.devil_env_test
import devil.utils.cmd_helper_test
import devil.utils.decorators_test
import devil.utils.find_usb_devices_test
import devil.utils.geometry_test
import devil.utils.lazy.weak_constant_test
import devil.utils.lsusb_test
import devil.utils.markdown_test
import devil.utils.mock_calls_test
import devil.utils.parallelizer_test
import devil.utils.reraiser_thread_unittest
import devil.utils.timeout_retry_unittest
import devil.utils.zip_utils_test

PY3_COMPATIBLE_TESTS = [
    # Add full test module path here
    devil.android.apk_helper_test,
    devil.android.app_ui_test,
    devil.android.battery_utils_test,
    devil.android.cpu_temperature_test,
    devil.android.decorators_test,
    devil.android.device_denylist_test,
    devil.android.device_errors_test,
    devil.android.device_utils_test,
    devil.android.fastboot_utils_test,
    devil.android.flag_changer_test,
    devil.android.logcat_monitor_test,
    devil.android.md5sum_test,
    devil.android.perf.perf_control_test,
    devil.android.perf.surface_stats_collector_test,
    devil.android.sdk.adb_wrapper_test,
    devil.android.sdk.shared_prefs_test,
    devil.android.tools.device_monitor_test,
    devil.android.tools.script_common_test,
    devil.android.tools.system_app_test,
    devil.devil_env_test,
    devil.utils.cmd_helper_test,
    devil.utils.decorators_test,
    devil.utils.find_usb_devices_test,
    devil.utils.geometry_test,
    devil.utils.lazy.weak_constant_test,
    devil.utils.lsusb_test,
    devil.utils.markdown_test,
    devil.utils.mock_calls_test,
    devil.utils.parallelizer_test,
    devil.utils.reraiser_thread_unittest,
    devil.utils.timeout_retry_unittest,
    devil.utils.zip_utils_test,
]


def main():
  # TODO(crbug.com/1007101): Use six.PY2 directly once we're using six via
  # vpython.
  if sys.version_info[0] == 2:
    print('Somehow running under python2.')
    return 1

  # Tests mock out internal details of methods, and the ANDROID_SERIAL can
  # change which internal methods are called. Since tests don't actually use
  # devices, it should be fine to delete the variable.
  if 'ANDROID_SERIAL' in os.environ:
    del os.environ['ANDROID_SERIAL']

  # This does not use typ for now, as typ has vpython dependencies that haven't
  # yet been updated for python3.
  result = unittest.TextTestRunner().run(unittest.TestSuite(
      unittest.defaultTestLoader.loadTestsFromModule(test_module)
      for test_module in PY3_COMPATIBLE_TESTS
  ))

  return 0 if result.wasSuccessful() else 1



if __name__ == '__main__':
  sys.exit(main())
