Commit db9c6727 authored by Luke Mewburn's avatar Luke Mewburn
Browse files

testing: simplify ASN.1 configuration

Parse and lint all the files in test_config.json asn1_files {},
instead of globbing and excluding certain directories.

Compile HI1NotificationOperations and HI2Operations
separate to LI-PS-PDU. (They were previously just syntax checked).
parent a4e90a27
Loading
Loading
Loading
Loading
+12 −44
Changes for testing/asn/asn_process.py: 12 added lines, 44 removed lines.
Original line number Diff line number Diff line
@@ -31,23 +31,6 @@ asn1c_path = ""
change_path_to_unix = False


def filterIgnoredFiles(
    pathList: list[Path], ignoreList: test_config.PathList
) -> list[Path]:
    """Return pathList with paths removed that contain substrings in ignoreList."""
    ignoredFiles = set()

    for path in pathList:
        for ignorePath in ignoreList:
            if path == ignorePath or ignorePath in path.parents:
                logger.debug(f"Ignoring {path} due to {ignorePath}")
                ignoredFiles.add(path)

    logger.info(f"{len(ignoredFiles)} files ignored")

    return [file for file in pathList if file not in ignoredFiles]


def fix_path(path: str) -> str:
    """Rewrite a file path for UNIX.

@@ -77,11 +60,11 @@ def getDependencies(asnFiles: test_config.Asn1FilesDict, fileName: Path) -> list
    return fileNames


def syntaxCheckASN(fileList: list[Path]) -> dict[str, typing.Any]:
def syntaxCheckASN(asnFiles: test_config.Asn1FilesDict) -> dict[str, typing.Any]:
    """Performs ASN syntax checking on a list of pathlib Paths.

    Args:
        fileList: List of filenames (Pathlib Path)
        asnFiles: List of filenames (Pathlib Path)

    Returns:
        A dict of outcome against the first filename of each compile target.
@@ -91,7 +74,7 @@ def syntaxCheckASN(fileList: list[Path]) -> dict[str, typing.Any]:
    As a result, `asn1c` must be available to run.
    """
    results = {}
    for file in fileList:
    for file in asnFiles:
        try:
            p = subprocess.run(  # noqa: S603 (subprocess-without-shell-equals-true)
                [str(asn1c_path), "-E", fix_path(str(file))],
@@ -370,36 +353,25 @@ def main() -> int: # noqa: C901, PLR0912, PLR0915 (complex-structure, too-many-
        global change_path_to_unix  # noqa: PLW0603 (global-statement)
        change_path_to_unix = True

    logger.info("Loading test_config.json")
    logger.info(f"Loading {args.config}")
    testConfig = test_config.parse_json_file(Path(args.config))
    asn1_files = testConfig.asn1_files
    logger.info(f"{len(asn1_files)} ASN.1 files found")

    logger.info("Searching for ASN.1 files")
    fileList = sorted(list(Path().rglob("*.asn1")) + list(Path().rglob("*.asn")))
    logger.info(f"{len(fileList)} ASN.1 files found")
    for file in fileList:
        logger.debug(f"  {file}")

    parseFileList = filterIgnoredFiles(fileList, testConfig.asn1_parse_ignore)
    logger.info(f"{len(parseFileList)} files to parse")
    for file in parseFileList:
        logger.debug(f"  {file}")

    if len(parseFileList) == 0:
    if len(testConfig.asn1_files) == 0:
        logger.warning("No files specified")
        return 1

    logger.info("Parsing ASN.1 files")
    start_time = perf_counter()
    parseResults = syntaxCheckASN(parseFileList)
    parseResults = syntaxCheckASN(asn1_files)
    times.append(("Parsing", perf_counter() - start_time))
    if processResults(parseResults, "Parsing") > 0:
        return 1

    logger.info("Compiling ASN.1 files")
    asnFiles = testConfig.asn1_files
    logger.info(f"{len(asnFiles)} compile targets found")
    start_time = perf_counter()
    compileResults = compileAllTargets(asnFiles)
    compileResults = compileAllTargets(asn1_files)
    times.append(("Compiling", perf_counter() - start_time))
    if processResults(compileResults, "Compiling") > 0:
        return 1
@@ -407,12 +379,8 @@ def main() -> int: # noqa: C901, PLR0912, PLR0915 (complex-structure, too-many-
    retval = 0  # Checks now don't prevent subsequent checks from running

    logger.info("Linting files")
    lintFileList = filterIgnoredFiles(fileList, testConfig.asn1_lint_ignore)
    logger.info(f"{len(lintFileList)} files to lint")
    for file in lintFileList:
        logger.debug(f"  {file}")
    start_time = perf_counter()
    lintResults = lint_asn1.lintASN1Files(lintFileList, testConfig)
    lintResults = lint_asn1.lintASN1Files(asn1_files)
    times.append(("Linting", perf_counter() - start_time))
    if processResults(lintResults, "Linting") > 0:
        retval = 1
@@ -422,7 +390,7 @@ def main() -> int: # noqa: C901, PLR0912, PLR0915 (complex-structure, too-many-

    logger.info("Checking ASN.1 backwards compatibility between files")
    start_time = perf_counter()
    compatResults = compatCheckFiles(asnFiles)
    compatResults = compatCheckFiles(asn1_files)
    times.append(("Compat checking (files)", perf_counter() - start_time))
    if processResults(compatResults, "Compatibility checking files") > 0:
        retval = 1
@@ -433,7 +401,7 @@ def main() -> int: # noqa: C901, PLR0912, PLR0915 (complex-structure, too-many-
    else:
        logger.info("Checking ASN.1 backwards compatibility between commits")
        start_time = perf_counter()
        commitResults = compatCheckCommits(asnFiles)
        commitResults = compatCheckCommits(asn1_files)
        times.append(("Compat checking (commits)", perf_counter() - start_time))
        if processResults(commitResults, "Compatibility checking commits") > 0:
            retval = 1
+3 −9
Changes for testing/asn/lint_asn1.py: 3 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -7,23 +7,17 @@ NOTE: ASN.1 linting is not yet implemented.
# ruff: noqa: INP001 (implicit-namespace-package)
# ruff: noqa: N802 (invalid-function-name)
# ruff: noqa: TC002 (typing-only-third-party-import)
# ruff: noqa: TC003 (typing-only-standard-library-import)

import pathlib

import test_config


def lintASN1Files(
    file_list: list[pathlib.Path],
    test_config: test_config.TestConfig,
    asn1_files: test_config.Asn1FilesDict,
) -> dict[str, str]:
    """Lint all files in `file_list` per `test_config` configuration.
    """Lint all files in `asn1_files`.

    Args:
        file_list: List of files to lint.
        test_config: dict with optional nested keys:
            asn1_files.FILENAME.lint_exceptions: list of expected errors.
        asn1_files: Asn1FilesDict to lint, with lint_exceptions overrides.

    Returns:
        Error map, key is filename, value is object with describing fault.
−10
Changes for testing/asn/test_config.py: 0 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -61,16 +61,6 @@ class TestConfig(pydantic.BaseModel):
        description="Dict of ASN.1 filenames to per-file configuration.",
    )

    asn1_lint_ignore: PathList = pydantic.Field(
        default_factory=list,
        description="Paths in the repository to exclude from lint_asn1.py.",
    )

    asn1_parse_ignore: PathList = pydantic.Field(
        default_factory=list,
        description="Paths in the repository to exclude from checking with asn1c.",
    )


def parse_json_file(path: pathlib.Path) -> TestConfig:
    """Parse JSON file into internal data structures.
+18 −7
Changes for testing/test_config.json: 18 added lines, 7 removed lines.
Original line number Diff line number Diff line
{
  "asn1_files": {
    "102232-1/HI1NotificationOperations.asn": {
      "dependencies": [
        "102232-1/HI2Operations.asn",
        "testing/deps/33108/UMTSHI2Operations.asn",
        "testing/deps/101909/TS101909201.asn",
        "testing/deps/301040/06132v203_C01.asn",
        "testing/deps/101909/PCESP.asn"
      ]
    },
    "102232-1/HI2Operations.asn": {
      "dependencies": [
        "testing/deps/33108/UMTSHI2Operations.asn",
        "testing/deps/101909/TS101909201.asn",
        "testing/deps/301040/06132v203_C01.asn",
        "testing/deps/101909/PCESP.asn"
      ]
    },
    "102232-1/LI-PS-PDU.asn": {
      "dependencies": [
        "103280/TS_103_280.asn1",
@@ -61,11 +78,5 @@
        "103280/TS_103_280.asn1"
      ]
    }
  },
  "asn1_lint_ignore": [
    "testing/deps/"
  ],
  "asn1_parse_ignore": [
    "testing/deps/"
  ]
  }
}
+18 −7
Changes for testing/test_config_full.json: 18 added lines, 7 removed lines.
Original line number Diff line number Diff line
{
  "asn1_files": {
    "102232-1/HI1NotificationOperations.asn": {
      "dependencies": [
        "102232-1/HI2Operations.asn",
        "testing/deps/33108/UMTSHI2Operations.asn",
        "testing/deps/101909/TS101909201.asn",
        "testing/deps/301040/06132v203_C01.asn",
        "testing/deps/101909/PCESP.asn"
      ]
    },
    "102232-1/HI2Operations.asn": {
      "dependencies": [
        "testing/deps/33108/UMTSHI2Operations.asn",
        "testing/deps/101909/TS101909201.asn",
        "testing/deps/301040/06132v203_C01.asn",
        "testing/deps/101909/PCESP.asn"
      ]
    },
    "102232-1/LI-PS-PDU.asn": {
      "compat_commits": {
        "spec/102232-1/2.3.1..spec/102232-1/2.8.1": [],
@@ -196,11 +213,5 @@
        "103280/TS_103_280.asn1"
      ]
    }
  },
  "asn1_lint_ignore": [
    "testing/deps/"
  ],
  "asn1_parse_ignore": [
    "testing/deps/"
  ]
  }
}