#!/usr/bin/env python3

# This script updates the JavaScript test data used by the endpoint CodeQL tests.

import glob
import logging
import os
import shutil

# File extensions that should be copied to the endpoint tests. This should include source code files
# e.g. .js, but not the tests themselves e.g. .expected, .ql, .qlref, etc.
file_extensions_to_copy = ['.js', '.ts']

# Maps each security query to the test root path for that security query. Each test root path is the
# path of that test relative to a checkout of github/codeql.
test_root_relative_paths = {
    'NosqlAndSqlInjection': 'javascript/ql/test/query-tests/Security/CWE-089',
    'TaintedPath': 'javascript/ql/test/query-tests/Security/CWE-022/TaintedPath',
    'Xss': 'javascript/ql/test/query-tests/Security/CWE-079',
}
# The path of the endpoint tests, relative to a checkout of github/codeql
test_path = 'javascript/ql/experimental/adaptivethreatmodeling/test/endpoint_large_scale'

logging.basicConfig(level=logging.INFO)

codeql_lib_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))

autogenerated_dest_path = os.path.join(codeql_lib_path, test_path, 'autogenerated')
if os.path.exists(autogenerated_dest_path):
    logging.info(f'Deleting existing autogenerated test files...')
    shutil.rmtree(autogenerated_dest_path)

for key, rel_path in test_root_relative_paths.items():
    test_files_path = os.path.join(codeql_lib_path, rel_path)
    logging.info(f'Copying test files for {key}...')

    for file in glob.glob(test_files_path + '/**', recursive=True):
        # Ignore .testproj directories
        if '.testproj' in file:
            continue

        file_extension = os.path.splitext(file)[1]
        if file_extension in file_extensions_to_copy:
            dest_path = os.path.normpath(
                os.path.join(autogenerated_dest_path, key, os.path.relpath(file, test_files_path))
            )
            logging.debug(f'Copying {file} to {dest_path}')
            os.makedirs(os.path.dirname(dest_path), exist_ok=True)
            shutil.copyfile(file, dest_path)
