#!/usr/bin/env python
# Copyright (c) 2015 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.

import argparse
import os
import sys

hooks_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if hooks_path not in sys.path:
  sys.path.append(hooks_path)

from hooks import install


from tracing import tracing_project
import vinn


def Main(args):
  project = tracing_project.TracingProject()
  d8_test_module_resources = project.FindAllD8TestModuleResources()
  d8_test_module_filenames = [x.unix_style_relative_path
                              for x in d8_test_module_resources]
  d8_test_module_filenames.sort()

  cmd = """
  loadHTML('/base/d8_tests.html');
  """
  res = vinn.RunJsString(
    cmd, source_paths=list(project.source_paths),
    js_args=d8_test_module_filenames, stdout=sys.stdout, stdin=sys.stdin)
  return res.returncode

if __name__ == '__main__':
  parser = argparse.ArgumentParser(
      description='Run d8 tests.')
  parser.add_argument(
    '--no-install-hooks', dest='install_hooks', action='store_false')
  parser.set_defaults(install_hooks=True)
  args = parser.parse_args()
  if args.install_hooks:
    install.InstallHooks()

  sys.exit(Main(sys.argv[1:]))