Skip to content

Commit

Permalink
[test] Test Smatch Output with sample output file
Browse files Browse the repository at this point in the history
- Smatch Parser is tested with sample.c and sample.out files
  • Loading branch information
jay24rajput committed Oct 11, 2020
1 parent 3b99c8c commit da1c08a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static struct parse_smatch *check_sample(){
int res;
res = 0;
return ERR_PTR(res);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>diagnostics</key>
<array>
<dict>
<key>category</key>
<string>unknown</string>
<key>check_name</key>
<string>smatch</string>
<key>description</key>
<string>check_sample() warn: passing zero to 'ERR_PTR'</string>
<key>issue_hash_content_of_line_in_context</key>
<string>4b7ca471f46a9a22d294b3e6e3676131</string>
<key>location</key>
<dict>
<key>col</key>
<integer>0</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>path</key>
<array>
<dict>
<key>depth</key>
<integer>0</integer>
<key>kind</key>
<string>event</string>
<key>location</key>
<dict>
<key>col</key>
<integer>0</integer>
<key>file</key>
<integer>0</integer>
<key>line</key>
<integer>4</integer>
</dict>
<key>message</key>
<string>check_sample() warn: passing zero to 'ERR_PTR'</string>
</dict>
</array>
<key>type</key>
<string>smatch</string>
</dict>
</array>
<key>files</key>
<array>
<string>files/sample.c</string>
</array>
<key>metadata</key>
<dict>
<key>analyzer</key>
<dict>
<key>name</key>
<string>smatch</string>
</dict>
<key>generated_by</key>
<dict>
<key>name</key>
<string>report-converter</string>
<key>version</key>
<string>x.y.z</string>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
files/sample.c:4 check_sample() warn: passing zero to 'ERR_PTR'
79 changes: 79 additions & 0 deletions tools/report-converter/tests/unit/test_smatch_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------

"""
This module tests the correctness of the SmatchAnalyzerResult, which
used in sequence transform Smatch output to a plist file.
"""


import os
import plistlib
import shutil
import tempfile
import unittest


from codechecker_report_converter.smatch.analyzer_result import \
SmatchAnalyzerResult


class SmatchAnalyzerResultTestCase(unittest.TestCase):
""" Test the output of the SmatchAnalyzerResult. """

def setUp(self):
""" Setup the test. """
self.analyzer_result = SmatchAnalyzerResult()
self.cc_result_dir = tempfile.mkdtemp()
self.test_files = os.path.join(os.path.dirname(__file__),
'smatch_output_test_files')

def tearDown(self):
""" Clean temporary directory. """
shutil.rmtree(self.cc_result_dir)

def test_no_smatch_output_file(self):
""" Test transforming single C file. """
analyzer_result = os.path.join(self.test_files, 'files',
'sample.c')

ret = self.analyzer_result.transform(analyzer_result,
self.cc_result_dir)
self.assertFalse(ret)

def test_transform_dir(self):
""" Test transforming a directory. """
analyzer_result = os.path.join(self.test_files)

ret = self.analyzer_result.transform(analyzer_result,
self.cc_result_dir)
self.assertFalse(ret)

def test_transform_single_file(self):
""" Test transforming single output file. """
analyzer_result = os.path.join(self.test_files, 'sample.out')
self.analyzer_result.transform(analyzer_result, self.cc_result_dir)

plist_file = os.path.join(self.cc_result_dir,
'sample.c_smatch.plist')

with open(plist_file, mode='rb') as pfile:
res = plistlib.load(pfile)

# Use relative path for this test.
res['files'][0] = os.path.join('files', 'sample.c')

self.assertTrue(res['metadata']['generated_by']['version'])
res['metadata']['generated_by']['version'] = "x.y.z"

plist_file = os.path.join(self.test_files,
'sample.expected.plist')
with open(plist_file, mode='rb') as pfile:
exp = plistlib.load(pfile)

self.assertEqual(res, exp)

0 comments on commit da1c08a

Please sign in to comment.