Skip to content

Commit

Permalink
feat: Ignore license header in .msg files
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Mar 17, 2024
1 parent 5f1988b commit 9958953
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions capella_ros_tools/.license_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-License-Identifier: Apache-2.0
6 changes: 6 additions & 0 deletions capella_ros_tools/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

from capellambse.filehandler import abc

LICENSE_HEADER = (
pathlib.Path(__file__)
.parent.joinpath(".license_header.txt")
.read_text(encoding="utf-8")
)
PACKAGE_NAME_MESSAGE_TYPE_SEPARATOR = "/"
COMMENT_DELIMITER = "#"
CONSTANT_SEPARATOR = "="
Expand Down Expand Up @@ -214,6 +219,7 @@ def from_file(
"""Create message definition from a .msg file."""
msg_name = file.stem
msg_string = file.read_text()
msg_string = msg_string.removeprefix(LICENSE_HEADER)
return cls.from_string(msg_name, msg_string)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-License-Identifier: Apache-2.0

# SampleClass.msg
# The first comment block at the top of the file
# is added to the class description of SampleClass.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-License-Identifier: Apache-2.0

# SampleEnum.msg
# This block comment is added to the
# enum description of SampleEnumValue.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: Copyright DB Netz AG
# SPDX-License-Identifier: Apache-2.0

# SampleClassEnum.msg
# Properties in SampleClassEnum can reference
# enums in the same file.
Expand Down

This file was deleted.

30 changes: 22 additions & 8 deletions tests/test_data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,22 @@ def test_extract_file_level_comments_no_comments():
@staticmethod
def test_extract_file_level_comments_no_newline():
msg_string = """# This is a comment
# This is another comment
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2"""
# This is another comment
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2"""
comments, _ = data_model._extract_file_level_comments(msg_string)

assert comments == ""

@staticmethod
def test_extract_file_level_comments():
msg_string = """# This is a comment
# This is another comment
# This is another comment
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2"""
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2"""
comments, _ = data_model._extract_file_level_comments(msg_string)
expected = "This is a comment This is another comment "

Expand All @@ -189,6 +189,20 @@ def test_extract_file_level_comments_with_newline():

assert comments == expected

@staticmethod
def test_extract_file_level_comments_strip_empty_lines_at_top():
msg_string = """
# This is a comment
uint8 OK = 0
uint8 WARN = 1
uint8 ERROR = 2"""
comments, _ = data_model._extract_file_level_comments(msg_string)
expected = "This is a comment "

assert comments == expected

@staticmethod
def test_parse_comments_no_comments():
msg_string = """uint8 field"""
Expand Down

0 comments on commit 9958953

Please sign in to comment.