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

lint_asn1: fix D.4.9, D.4.11, and ruff check

'ruff check' detected 3 copies of function D41(), so rename
the duplicates appropriate to their actual test.

Fix other 'ruff check' issues.

Rename XIriVsIri() to D415().
Rename checkD4holes() to D43_holes() and move to after D43().
Rename checkD45() to D45().

No functional change to expected failures.
parent a961853e
Loading
Loading
Loading
Loading
+49 −50
Original line number Diff line number Diff line
@@ -3,12 +3,10 @@
import logging
import os

from asn1tools import parse_files, compile_dict, ParseError, CompileError
from glob import glob
from asn1tools import parse_files, ParseError
from pathlib import Path
import string

from pprint import pprint
import functools


@@ -56,7 +54,7 @@ def membersAsTagDict(atype):
    for member in atype.get("members", {}):
        if not member:
            continue
        if not "tag" in member:
        if "tag" not in member:
            continue
        result[member["tag"]["number"]] = member
    return result
@@ -72,7 +70,7 @@ def membersAsTagDict(atype):
    testKind="file",
    testDescription="Fields, tags, types and flags are space aligned",
)
def D41(fileLines, context):
def D49(fileLines, context):
    errors = []
    for lineNumber, line in enumerate(fileLines):
        if "\t" in line:
@@ -92,7 +90,7 @@ def D41(fileLines, context):
    testKind="file",
    testDescription="Braces are given their own line",
)
def D41(fileLines, context):
def D411(fileLines, context):
    errors = []
    for lineNumber, line in enumerate(fileLines):
        if ("{" in line and line.strip().replace(",", "") != "{") or (
@@ -127,9 +125,7 @@ def D41(fileLines, context):
)
def D41(module, context):
    errors = []
    if not ("extensibility-implied" in module) or (
        module["extensibility-implied"] == False
    ):
    if "extensibility-implied" not in module or not module["extensibility-implied"]:
        appendFailure(
            errors,
            context,
@@ -151,11 +147,11 @@ def D42(module, context):


@lintingTest(
    testName="XIriVsIri",
    testName="D415",
    testKind="module",
    testDescription="XIRIEvent and IRIEvent use same tags",
    testDescription="XIRIEvent and IRIEvent use same names and tags",
)
def XIriVsIri(module, context):
def D415(module, context):
    errors = []
    xirievent = module["types"].get("XIRIEvent")
    irievent = module["types"].get("IRIEvent")
@@ -223,7 +219,7 @@ def XIriVsIri(module, context):
    testDescription="Field names only contain characters A-Z, a-z, 0-9",
)
def D34(t, context):
    if not "members" in t:
    if "members" not in t:
        logging.debug(
            f"      D34 ignoring {context['module']} '{context['type']}' as it has no members"
        )
@@ -265,7 +261,7 @@ def D34(t, context):
def D43(t, context):
    errors = []
    if (t["type"] == "SEQUENCE") or (t["type"] == "CHOICE"):
        if not "tag" in t["members"][0]:
        if "tag" not in t["members"][0]:
            return errors
        if t["members"][0]["tag"]["number"] != 1:
            appendFailure(
@@ -278,6 +274,40 @@ def D43(t, context):
    return errors


@lintingTest(testName="D.4.3holes", testKind="type", testDescription="No tag holes")
def D43_holes(t, context):
    if "members" not in t:
        logging.debug(f"      D4holes: No members in type {context['type']}, ignoring")
        return []
    errors = []
    wantTag = None
    for m in t["members"]:
        if not m:
            continue
        thisTag = m["tag"]["number"]
        if wantTag:
            if wantTag < thisTag - 1:
                appendFailure(
                    errors,
                    context,
                    {
                        "field": m["name"],
                        "message": f"Tags {wantTag}-{thisTag - 1} missing in {context['type']}",
                    },
                )
            elif wantTag == thisTag - 1:
                appendFailure(
                    errors,
                    context,
                    {
                        "field": m["name"],
                        "message": f"Tag {wantTag} missing in {context['type']}",
                    },
                )
        wantTag = thisTag + 1
    return errors


@lintingTest(
    testName="D.4.4",
    testKind="type",
@@ -298,8 +328,8 @@ def D44(t, context):


@lintingTest(testName="D.4.5", testKind="type", testDescription="No anonymous types")
def checkD45(t, context):
    if not "members" in t:
def D45(t, context):
    if "members" not in t:
        logging.debug(f"      D45: No members in type {context['type']}, ignoring")
        return []
    errors = []
@@ -318,39 +348,6 @@ def checkD45(t, context):
    return errors


@lintingTest(testName="D.4.holes", testKind="type", testDescription="No tag holes")
def checkD4holes(t, context):
    if not "members" in t:
        logging.debug(f"      D4holes: No members in type {context['type']}, ignoring")
        return []
    errors = []
    wantTag = None
    for m in t["members"]:
        if not m:
            continue
        thisTag = m["tag"]["number"]
        if wantTag:
            if wantTag < thisTag - 1:
                appendFailure(
                    errors,
                    context,
                    {
                        "field": m["name"],
                        "message": f"Tags {wantTag}-{thisTag - 1} missing in {context['type']}",
                    },
                )
            elif wantTag == thisTag - 1:
                appendFailure(
                    errors,
                    context,
                    {
                        "field": m["name"],
                        "message": f"Tag {wantTag} missing in {context['type']}",
                    },
                )
        wantTag = thisTag + 1
    return errors


def lintASN1File(asnFile, exceptions):
    errors = []
@@ -391,7 +388,9 @@ def lintASN1Files(fileList, asnConfig):
    logging.info("Checking files...")
    for f in fileList:
        unixf = str(f).replace("\\", "/")
        exceptions = asnConfig["asn_files"].get(unixf, {}).get("lint_exceptions", {})
        exceptions = (
            asnConfig.get("asn_files", {}).get(unixf, {}).get("lint_exceptions", {})
        )
        errorMap[str(f)] = lintASN1File(str(f), exceptions)

    return errorMap