|
2 | 2 |
|
3 | 3 | # This script updates the JavaScript test data used by the endpoint CodeQL tests.
|
4 | 4 |
|
5 |
| -import glob |
| 5 | +import git |
6 | 6 | import logging
|
7 |
| -import os |
8 | 7 | import shutil
|
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +# Get relevant paths |
| 11 | +script_path = Path(__file__).absolute() |
| 12 | +git_repo = git.Repo(__file__, search_parent_directories=True) |
| 13 | +git_root = Path(git_repo.git.rev_parse('--show-toplevel')) |
| 14 | +autogenerated_dest_path = script_path.parent.joinpath('endpoint_large_scale', |
| 15 | + 'autogenerated') |
9 | 16 |
|
10 | 17 | # File extensions that should be copied to the endpoint tests. This should include source code files
|
11 | 18 | # e.g. .js, but not the tests themselves e.g. .expected, .ql, .qlref, etc.
|
|
15 | 22 | # path of that test relative to a checkout of github/codeql.
|
16 | 23 | test_root_relative_paths = {
|
17 | 24 | 'NosqlAndSqlInjection': 'javascript/ql/test/query-tests/Security/CWE-089',
|
18 |
| - 'TaintedPath': 'javascript/ql/test/query-tests/Security/CWE-022/TaintedPath', |
| 25 | + 'TaintedPath': |
| 26 | + 'javascript/ql/test/query-tests/Security/CWE-022/TaintedPath', |
19 | 27 | 'Xss': 'javascript/ql/test/query-tests/Security/CWE-079',
|
| 28 | + 'XssThroughDom': 'javascript/ql/test/query-tests/Security/CWE-116' |
20 | 29 | }
|
21 |
| -# The path of the endpoint tests, relative to a checkout of github/codeql |
22 |
| -test_path = 'javascript/ql/experimental/adaptivethreatmodeling/test/endpoint_large_scale' |
23 | 30 |
|
24 | 31 | logging.basicConfig(level=logging.INFO)
|
25 | 32 |
|
26 |
| -codeql_lib_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))) |
27 |
| - |
28 |
| -autogenerated_dest_path = os.path.join(codeql_lib_path, test_path, 'autogenerated') |
29 |
| -if os.path.exists(autogenerated_dest_path): |
| 33 | +if autogenerated_dest_path.exists(): |
30 | 34 | logging.info(f'Deleting existing autogenerated test files...')
|
31 | 35 | shutil.rmtree(autogenerated_dest_path)
|
32 | 36 |
|
33 | 37 | for key, rel_path in test_root_relative_paths.items():
|
34 |
| - test_files_path = os.path.join(codeql_lib_path, rel_path) |
| 38 | + test_files_path = git_root.joinpath(rel_path) |
35 | 39 | logging.info(f'Copying test files for {key}...')
|
36 |
| - |
37 |
| - for file in glob.glob(test_files_path + '/**', recursive=True): |
38 |
| - # Ignore .testproj directories |
39 |
| - if '.testproj' in file: |
| 40 | + counter = 0 |
| 41 | + for file in Path(test_files_path).glob('**/*'): |
| 42 | + if file.is_dir() or '.test_proj' in str(file): |
40 | 43 | continue
|
41 |
| - |
42 |
| - file_extension = os.path.splitext(file)[1] |
43 |
| - if file_extension in file_extensions_to_copy: |
44 |
| - dest_path = os.path.normpath( |
45 |
| - os.path.join(autogenerated_dest_path, key, os.path.relpath(file, test_files_path)) |
46 |
| - ) |
| 44 | + if file.suffix in file_extensions_to_copy: |
| 45 | + autogenerated_dest_path.joinpath(key, ) |
| 46 | + dest_path = autogenerated_dest_path.joinpath( |
| 47 | + key, file.relative_to(test_files_path)) |
47 | 48 | logging.debug(f'Copying {file} to {dest_path}')
|
48 |
| - os.makedirs(os.path.dirname(dest_path), exist_ok=True) |
| 49 | + dest_path.parent.mkdir(parents=True, exist_ok=True) |
49 | 50 | shutil.copyfile(file, dest_path)
|
| 51 | + counter += 1 |
| 52 | + logging.info(f'copied {counter} files') |
0 commit comments