Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
HeaderFieldError,
InvalidNameError,
)
except:
except ImportError:
from .parser.parse import parse
from .parser.file import file, open
from .parser.errors import (
Expand Down
3 changes: 2 additions & 1 deletion parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def parse(
) -> ParseResult:
error_collector = ErrorCollector()
if filename:
assert not filecontent
if filecontent:
raise ValueError("Cannot specify both filename and filecontent")
filecontent = builtins.open(filename, encoding=None).read()

# Match and remove the comments
Expand Down
1 change: 0 additions & 1 deletion parser/transformer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from lark import Transformer
from dataclasses import dataclass
import numbers
from lark import Lark, Transformer, Tree, Token
Expand Down
40 changes: 37 additions & 3 deletions test_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import glob
import json
import os
import subprocess
import pytest

try:
Expand All @@ -10,7 +13,7 @@
DuplicateNameError,
HeaderFieldError,
)
except:
except ImportError:
from __init__ import (
parse,
open,
Expand All @@ -25,7 +28,7 @@

def create_context(fn):
if "fail_" in fn:
return pytest.raises(_ValidationError)
return pytest.raises(CollectedValidationErrors)
else:
return nullcontext()

Expand Down Expand Up @@ -186,7 +189,7 @@ def test_header_entity_fields_whole_file():
parse(filename="fixtures/fail_too_many_header_entity_fields.ifc")


def test_header_entity_fields_whole_file():
def test_multiple_duplicate_ids():
with pytest.raises(CollectedValidationErrors) as exc_info:
parse(filename="fixtures/fail_multiple_duplicate_ids.ifc")

Expand All @@ -204,3 +207,34 @@ def test_multiple_wrong_header_fields():

assert len(errors) == 2
assert all(isinstance(e, HeaderFieldError) for e in errors)


REPO_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(REPO_DIR)
MODULE_NAME = os.path.basename(REPO_DIR)


def run_cli(*args):
return subprocess.run(
["python", "-m", MODULE_NAME, *args],
capture_output=True, text=True, cwd=PARENT_DIR,
)


def test_cli_valid_file():
result = run_cli("step-file-parser/fixtures/passing_header.ifc")
assert result.returncode == 0
assert "Valid" in result.stderr


def test_cli_invalid_file():
result = run_cli("step-file-parser/fixtures/fail_no_header.ifc")
assert result.returncode == 1


def test_cli_json_output():
result = run_cli("--json", "step-file-parser/fixtures/fail_no_header.ifc")
assert result.returncode == 1
errors = json.loads(result.stdout)
assert isinstance(errors, list)
assert len(errors) > 0
Loading