Skip to content

Commit

Permalink
Removed the code which added an extra new line at the end of output f…
Browse files Browse the repository at this point in the history
…iles

Previously make test-ci command added two empty lines at the end of the content in output files now you will see only see one empty line at the end of the content in those output files
I have also done some linting changes
  • Loading branch information
mugdhadhole1 committed Oct 11, 2024
1 parent c04be9c commit 108e683
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 65 deletions.
1 change: 0 additions & 1 deletion lobster/tools/python/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ def main():
if options.out:
with open(options.out, "w", encoding="UTF-8") as fd:
lobster_write(fd, schema, "lobster_python", items)
fd.write("\n")
print("Written output for %u items to %s" % (len(items),
options.out))
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,3 @@ Written output for 6 items to multiple_identical_function_names.lobster
"schema": "lobster-imp-trace",
"version": 3
}

125 changes: 62 additions & 63 deletions test-unit/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from hashlib import sha1
from lobster.location import Location

class SetUp(unittest.TestCase):
class SetUp(unittest.TestCase):
mock_namespace = "mock_namespace"
mock_tag = "mock_tag"
mock_framework = "mock_framework"
Expand All @@ -14,12 +14,12 @@ class SetUp(unittest.TestCase):
mock_status = "active"
mock_language = "mock_language"
mock_location = create_autospec(Location, instance=True)
tracing_tag = Tracing_Tag(mock_namespace, mock_tag)
tracing_tag = Tracing_Tag(mock_namespace, mock_tag)
item = Item(tracing_tag, mock_location)
requirement = Requirement(tracing_tag, mock_location, mock_framework, mock_kind, mock_name, mock_text, mock_status)
implementation = Implementation(tracing_tag, mock_location, mock_language, mock_kind, mock_name)
activity = Activity(tracing_tag, mock_location, mock_framework, mock_kind)

def set_location_data(self, location_type):
if location_type == "file":
location_data = {
Expand All @@ -45,18 +45,18 @@ def set_location_data(self, location_type):
}
elif location_type == "void":
location_data = {
"kind": location_type
"kind": location_type
}
return location_data

class TestTracingTag(unittest.TestCase):
class TestTracingTag(unittest.TestCase):
def test_key(self):
expected_key = "mock_namespace mock_tag"
actual_key = SetUp.tracing_tag.key()
self.assertEqual(expected_key, actual_key)

def test_to_json(self):
expected_json = "mock_namespace mock_tag"
expected_json = "mock_namespace mock_tag"
actual_json = SetUp.tracing_tag.to_json()
self.assertEqual(expected_json, actual_json)

Expand All @@ -66,34 +66,33 @@ def test_from_json(self, mock_from_text):
expected_namespace = "namespace"
expected_rest_value = "string"
expected_result = SetUp.tracing_tag

mock_from_text.return_value = expected_result

result = SetUp.tracing_tag.from_json(json_input)

mock_from_text.assert_called_once_with(expected_namespace, expected_rest_value)
self.assertEqual(result, expected_result)

def test_from_text_with_version(self):
text = "mock_tag@version"
def test_from_text_with_version(self):
text = "mock_tag@version"
expected_namespace = "mock_namespace"
expected_tag = "mock_tag"
expected_version = "version"

result = SetUp.tracing_tag.from_text(SetUp.tracing_tag.namespace, text)

self.assertEqual(result.namespace, expected_namespace)
self.assertEqual(result.tag, expected_tag)
self.assertEqual(result.version, expected_version)

def test_from_text_without_version(self):
namespace = "namespace"
text = "tag"
expected_tag = "tag"
expected_version = None

result = SetUp.tracing_tag.from_text(namespace, text)

self.assertEqual(result.namespace, namespace)
self.assertEqual(result.tag, expected_tag)
self.assertEqual(result.version, expected_version)
Expand All @@ -106,59 +105,59 @@ def test_from_text_invalid_text(self):
with self.assertRaises(AssertionError):
SetUp.tracing_tag.from_text("namespace", 123)

def test_hash(self):
def test_hash(self):
hash_val = SetUp.tracing_tag.hash()
hfunc = sha1()
hfunc.update(SetUp.tracing_tag.key().encode("UTF-8"))
expected_hash = hfunc.hexdigest()
self.assertEqual(hash_val, expected_hash)

class TestItem(unittest.TestCase):
def test_set_level_valid_string(self):
mock_level = "mock_level"
class TestItem(unittest.TestCase):
def test_set_level_valid_string(self):
mock_level = "mock_level"

SetUp.item.set_level(mock_level)

self.assertEqual(SetUp.item.level, mock_level)

def test_set_level_invalid_string(self):
invalid_level = 10
with self.assertRaises(AssertionError):

with self.assertRaises(AssertionError):
result = SetUp.item.set_level(invalid_level)

def test_error(self):
mock_message = "mock_message"
def test_error(self):
mock_message = "mock_message"

SetUp.item.error(mock_message)

self.assertIn(mock_message, SetUp.item.messages)
self.assertTrue(SetUp.item.messages)

@patch("lobster.items.Tracing_Tag.key")
def test_add_tracing_target(self, mock_key):
mock_target = SetUp.tracing_tag
def test_add_tracing_target(self, mock_key):
mock_target = SetUp.tracing_tag
expected_result = "mock_namespace mock_tag"

mock_key.return_value = expected_result

SetUp.item.add_tracing_target(mock_target)
self.assertIn(mock_target, SetUp.item.unresolved_references)
self.assertIn(expected_result, SetUp.item.unresolved_references_cache)

def test_perform_source_checks(self):
mock_valid_source_info = {"key": "value"}
def test_perform_source_checks(self):
mock_valid_source_info = {"key": "value"}

try:
SetUp.item.perform_source_checks(mock_valid_source_info)
try:
SetUp.item.perform_source_checks(mock_valid_source_info)
except AssertionError:
self.fail("perform_source_checks() raised AssertionError unexpectedly!")

def test_perform_source_checks_with_invalid_type(self):
def test_perform_source_checks_with_invalid_type(self):
mock_invalid_source_info = ["not", "a", "dict"]

with self.assertRaises(AssertionError):
SetUp.item.perform_source_checks(mock_invalid_source_info)
SetUp.item.perform_source_checks(mock_invalid_source_info)

@patch("lobster.items.Tracing_Tag.key")
def test_determine_status_ok(self, mock_key):
Expand All @@ -168,7 +167,7 @@ def test_determine_status_ok(self, mock_key):
SetUp.item.just_down = []
SetUp.item.just_global = []
SetUp.item.messages = []
SetUp.item.has_error = False
SetUp.item.has_error = False
SetUp.item.level = "level1"
expected_result = "mock_namespace mock_tag"
mock_key.return_value = expected_result
Expand All @@ -181,18 +180,18 @@ def test_determine_status_ok(self, mock_key):
}
}
stab = {
mock_key() : SetUp.item
mock_key() : SetUp.item
}

SetUp.item.determine_status(config, stab)
self.assertEqual(SetUp.item.tracing_status, Tracing_Status.PARTIAL)

def test_determine_status_missing_up_reference(self):
mock_namespace = "mock_namespace"
mock_tag = "mock_tag"
mock_tag = "mock_tag"
SetUp.item.level = "level1"
SetUp.item.just_up = []
SetUp.item.just_global = []
SetUp.item.just_global = []
SetUp.item.ref_up = []
config = {
"level1": {
Expand All @@ -212,7 +211,7 @@ def test_determine_status_missing_up_reference(self):

def test_determine_status_missing_down_reference(self):
mock_namespace = "mock_namespace"
mock_tag = "mock_tag"
mock_tag = "mock_tag"
SetUp.item.level = "level1"
SetUp.item.just_down = []
SetUp.item.just_global = []
Expand All @@ -233,10 +232,10 @@ def test_determine_status_missing_down_reference(self):
self.assertEqual(SetUp.item.tracing_status, Tracing_Status.MISSING)
self.assertIn("missing reference to level1", SetUp.item.messages)

@patch("lobster.items.Item.set_level")
def test_additional_data_from_json_valid_data(self, mock_set_level):
@patch("lobster.items.Item.set_level")
def test_additional_data_from_json_valid_data(self, mock_set_level):
mock_level = "mock_level"
mock_set_level.return_value = "mock_level"
mock_set_level.return_value = "mock_level"
mock_data = {
"refs": ["mock_namespace mock_tag"],
"ref_up": ["mock refup"],
Expand All @@ -259,7 +258,7 @@ def test_additional_data_from_json_valid_data(self, mock_set_level):
self.assertEqual(SetUp.item.just_down, ["down1"])
self.assertEqual(SetUp.item.just_global, ["global1"])
self.assertEqual(SetUp.item.tracing_status, Tracing_Status.OK)

def test_additional_data_from_json_invalid_level(self):
level = 123
data = {}
Expand All @@ -283,7 +282,7 @@ def test_additional_data_from_json_invalid_schema_version(self):

with self.assertRaises(AssertionError):
SetUp.item.additional_data_from_json(level, data, schema_version)

@patch("lobster.items.Tracing_Tag.to_json")
def test_to_json(self, mock_to_json):
mock_to_json.return_value = "mock_value"
Expand All @@ -296,7 +295,7 @@ def test_to_json(self, mock_to_json):
SetUp.item.ref_up = [SetUp.tracing_tag]
SetUp.item.ref_down = [SetUp.tracing_tag]
SetUp.item.tracing_status = MagicMock()
SetUp.item.tracing_status.name = "mock_status"
SetUp.item.tracing_status.name = "mock_status"
expected_json = {
"tag": "mock_value",
"location": SetUp.mock_location.to_json(),
Expand All @@ -317,7 +316,7 @@ def test_to_json(self, mock_to_json):

class TestRequirement(unittest.TestCase):
@patch("lobster.items.Item.to_json")
def test_to_json(self, mock_super_to_json):
def test_to_json(self, mock_super_to_json):
mock_super_to_json.return_value = {
"item_property": "item_value"
}
Expand All @@ -327,22 +326,22 @@ def test_to_json(self, mock_super_to_json):
"kind": "mock_kind",
"text": "mock_text",
"status": "active"
}
}
result = SetUp.requirement.to_json()

mock_super_to_json.assert_called_once_with()
self.assertEqual(result, expected_result)

@patch("lobster.items.Item.error")
def test_perform_source_checks_valid_status(self, mock_super_error):
def test_perform_source_checks_valid_status(self, mock_super_error):
mock_error_message = None
mock_super_error.return_value = mock_error_message
source_info = {
"valid_status": ["active", "inactive"]
}
}

SetUp.requirement.perform_source_checks(source_info)

self.assertIsNone(mock_super_error())

@patch("lobster.items.Item.error")
Expand All @@ -351,19 +350,19 @@ def test_perform_source_checks_invalid_status(self, mock_super_error):
mock_super_error.return_value = mock_error_message
source_info = {
"valid_status": ["inactive", "closed"]
}
}

SetUp.requirement.perform_source_checks(source_info)

expected_error_message = "status is active, expected closed or inactive"
self.assertEqual(mock_super_error(), expected_error_message)

def test_perform_source_checks_invalid_source_info(self):
invalid_source_info = ["invalid", "list"]
invalid_source_info = ["invalid", "list"]

with self.assertRaises(AssertionError):
SetUp.requirement.perform_source_checks(invalid_source_info)

@patch("lobster.items.Tracing_Tag.from_json")
def test_from_json(self, mock_from_json):
mock_level = "mock_level"
Expand Down Expand Up @@ -412,29 +411,29 @@ def test_to_json(self, mock_super_to_json):
expected_result = {
"item_property": "item_value",
"language": "mock_language",
"kind": "mock_kind"
"kind": "mock_kind"
}

result = SetUp.implementation.to_json()

mock_super_to_json.assert_called_once_with()
self.assertEqual(result, expected_result)

@patch("lobster.items.Tracing_Tag.from_json")
def test_from_json(self, mock_from_json):
mock_level = "mock_level"
mock_schema_version = 3
setup = SetUp()
mock_from_json.return_value = SetUp.tracing_tag
for location_type in ["file", "github", "codebeamer", "void"]:
with self.subTest(location_type):
with self.subTest(location_type):
location_data = setup.set_location_data(location_type)
mock_data = {
"tag": SetUp.tracing_tag,
"location": location_data,
"language": "Python",
"kind": "kind_data",
"name": "name_data",
"name": "name_data",
}
result = setup.implementation.from_json(mock_level, mock_data, mock_schema_version)
self.assertEqual(result.tag, SetUp.tracing_tag)
Expand Down Expand Up @@ -478,7 +477,7 @@ def test_from_json(self, mock_from_json):
mock_level = "mock_level"
mock_schema_version = 3
setup = SetUp()
mock_from_json.return_value = setup.tracing_tag
mock_from_json.return_value = setup.tracing_tag
for location_type in ["file", "github", "codebeamer", "void"]:
with self.subTest(location_type):
location_data = setup.set_location_data(location_type)
Expand Down

0 comments on commit 108e683

Please sign in to comment.