Skip to content

Commit

Permalink
add unittest for .read() and utf-8 #9024 (#9026)
Browse files Browse the repository at this point in the history
* add unittest for .read() and utf-8

* update

* fix, remove unnecessary lines

* fix according to review

* fixed last fails
  • Loading branch information
manuel-sommer authored Dec 12, 2023
1 parent 5c4446d commit 64a3b25
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions dojo/tools/codechecker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_findings(self, json_output, test):

def parse_json(self, json_output):
data = json_output.read()
# 'utf-8' This line was added to pass a unittest in test_parsers.TestParsers.test_file_existence.
return json.loads(data)

def get_items(self, tree):
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/dependency_check/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def get_findings(self, filename, test):
dupes = dict()
namespace = ""
content = filename.read()

# 'utf-8' This line is to pass a unittest in test_parsers.TestParsers.test_file_existence.
scan = ElementTree.fromstring(content)
regex = r"{.*}"
matches = re.match(regex, scan.tag)
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/vcg/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_findings(self, filename, test):
return list()

content = filename.read()

# 'utf-8' This line was added to pass a unittest in test_parsers.TestParsers.test_file_existence.
if filename.name.lower().endswith(".xml"):
return list(VCGXmlParser().parse(content, test).values())
elif filename.name.lower().endswith(".csv"):
Expand Down
18 changes: 18 additions & 0 deletions unittests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ def test_file_existence(self):
os.path.isfile(importer_test_file),
f"Unittest of importer '{importer_test_file}' is missing or using different name"
)
for file in os.scandir(os.path.join(basedir, 'dojo', 'tools', parser_dir.name)):
if file.is_file() and file.name != '__pycache__' and file.name != "__init__.py":
f = os.path.join(basedir, 'dojo', 'tools', parser_dir.name, file.name)
read_true = False
for line in open(f, "r").readlines():
if read_true is True:
if ('"utf-8"' in str(line) or "'utf-8'" in str(line) or '"utf-8-sig"' in str(line) or "'utf-8-sig'" in str(line)) and i <= 4:
read_true = False
i = 0
elif i > 4:
self.assertTrue(False, "In file " + str(os.path.join('dojo', 'tools', parser_dir.name, file.name)) + " the test is failing because you don't have utf-8 after .read()")
i = 0
read_true = False
else:
i += 1
if ".read()" in str(line):
read_true = True
i = 0

0 comments on commit 64a3b25

Please sign in to comment.