Skip to content

Commit

Permalink
Merge pull request #220 from rgalonso/fix/reset-environment-after-cus…
Browse files Browse the repository at this point in the history
…tom-language-tests

fix: reset modified environment variables after test
  • Loading branch information
alstr authored Oct 28, 2024
2 parents 2e89575 + 67fd1f4 commit b9c9d19
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions tests/test_todo_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,46 +256,59 @@ def test_simple_escape(self):
self.assertEqual(issue.body[1], '\\<AnotherTag\\>')


class CustomLanguageTest(unittest.TestCase):
def test_custom_lang_load(self):
class BaseCustomLanguageTests:
class BaseTest(unittest.TestCase):
@staticmethod
def count_syntax(parser: TodoParser, name: str):
counter = 0

for syntax in parser.syntax_dict:
if syntax['language'] == name:
counter = counter + 1

return counter


class CustomLanguageFileTest(BaseCustomLanguageTests.BaseTest):
def setUp(self):
os.environ['INPUT_LANGUAGES'] = 'tests/custom_languages.json'
parser = TodoParser()
self.parser = TodoParser()

def test_custom_lang_load(self):
# Test if the custom language ILS is actually loaded into the system
self.assertIsNotNone(parser.languages_dict['ILS'])
self.assertEqual(self.count_syntax(parser, 'ILS'), 1)
self.assertIsNotNone(self.parser.languages_dict['ILS'])
self.assertEqual(self.count_syntax(self.parser, 'ILS'), 1)

def test_custom_lang_not_dupplicate(self):
os.environ['INPUT_LANGUAGES'] = 'tests/custom_languages.json'
parser = TodoParser()

# Test if a custom language can overwrite the rules of an existing one
self.assertEqual(self.count_syntax(parser, 'Java'), 1)
for syntax in parser.syntax_dict:
self.assertEqual(self.count_syntax(self.parser, 'Java'), 1)
for syntax in self.parser.syntax_dict:
if syntax['language'] == 'Java':
self.assertEqual(len(syntax['markers']), 2)
self.assertEqual(syntax['markers'][0]['pattern'], "////")
self.assertEqual(syntax['markers'][1]['pattern']['start'], '+=')
self.assertEqual(syntax['markers'][1]['pattern']['end'], '=+')
break

self.assertIsNotNone(parser.languages_dict['Java'])
self.assertEqual(len(parser.languages_dict['Java']['extensions']), 1)
self.assertEqual(parser.languages_dict['Java']['extensions'][0], ".java2")
self.assertIsNotNone(self.parser.languages_dict['Java'])
self.assertEqual(len(self.parser.languages_dict['Java']['extensions']), 1)
self.assertEqual(self.parser.languages_dict['Java']['extensions'][0], ".java2")

def test_url_load(self):
os.environ['INPUT_LANGUAGES'] = 'https://raw.githubusercontent.com/alstr/todo-to-issue-action/master/tests/custom_languages.json'
os.environ['INPUT_NO_STANDARD'] = 'true'
parser = TodoParser()
def tearDown(self):
del os.environ['INPUT_LANGUAGES']

self.assertEqual(len(parser.languages_dict), 2)
self.assertEqual(len(parser.syntax_dict), 2)

@staticmethod
def count_syntax(parser: TodoParser, name: str):
counter = 0
class CustomLanguageUrlTest(BaseCustomLanguageTests.BaseTest):
def setUp(self):
os.environ['INPUT_LANGUAGES'] = 'https://raw.githubusercontent.com/alstr/todo-to-issue-action/master/tests/custom_languages.json'
os.environ['INPUT_NO_STANDARD'] = 'true'
self.parser = TodoParser()

for syntax in parser.syntax_dict:
if syntax['language'] == name:
counter = counter + 1
def test_url_load(self):
self.assertEqual(len(self.parser.languages_dict), 2)
self.assertEqual(len(self.parser.syntax_dict), 2)

return counter
def tearDown(self):
del os.environ['INPUT_LANGUAGES']
del os.environ['INPUT_NO_STANDARD']

0 comments on commit b9c9d19

Please sign in to comment.