change tests to use subprocess.check_output instead of mock

This commit is contained in:
Emil Vanherp 2021-01-20 11:48:20 +01:00
parent a158fd2ac9
commit 6996435f80
No known key found for this signature in database
GPG Key ID: 880E6DDF9C2DC66D
5 changed files with 123 additions and 240 deletions

View File

@ -1,11 +1,9 @@
import pathlib
import subprocess
import shutil
import sys
import os
import unittest
from unittest import mock
from vsg import __main__
from vsg import rule_list
@ -34,19 +32,11 @@ class test_context_using_main(unittest.TestCase):
def tearDown(self):
os.remove(sFileName)
@mock.patch('sys.stdout')
def test_classification_file(self, mock_stdout):
def test_classification_file(self):
self.maxDiff = None
try:
sys.argv = ['vsg', '-f', sFileName, '--fix']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 0)
subprocess.check_output(['bin/vsg', '-f', sFileName, '--fix']).decode('utf-8').split('\n')
lActual = []
utils.read_file(sFileName, lActual)
lExpected = []
utils.read_file(sFixedFile, lExpected)
lActual = pathlib.Path(sFileName).read_text().split('\n')
lExpected = pathlib.Path(sFixedFile).read_text().split('\n')
self.assertEqual(lExpected, lActual)

View File

@ -1,13 +1,11 @@
import shutil
import sys
import os
import subprocess
import pathlib
import unittest
from unittest import mock
from vsg import __main__
from vsg.tests import utils
sEntityFileName = 'entity.vhd'
@ -25,6 +23,7 @@ sArchitectureOutputFileWoConfig = os.path.join(os.path.dirname(__file__),'output
sArchitectureOutputFileWithConfig = os.path.join(os.path.dirname(__file__),'output_w_config.architecture.txt')
sArchitectureOutputFileWithConfigFixed = os.path.join(os.path.dirname(__file__),'output_w_config_fixed.architecture.txt')
class test_severity_using_main(unittest.TestCase):
def setUp(self):
@ -43,196 +42,109 @@ class test_severity_using_main(unittest.TestCase):
os.remove(sArchitectureFileName)
os.remove(sJUnitFileName)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_entity_without_configuration(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
def test_entity_without_configuration(self):
try:
sys.argv = ['vsg', '-f', sEntityFileName]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg', '-f', sEntityFileName])
except subprocess.CalledProcessError as e:
lActual = e.output.decode('utf-8').split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputFileWoConfig, lOutputFile)
lExpected = pathlib.Path(sOutputFileWoConfig).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_entity_with_configuration(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
def test_entity_with_configuration(self):
try:
sys.argv = ['vsg', '-f', sEntityFileName, '-c', sConfigFile]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg', '-f', sEntityFileName, '-c', sConfigFile])
except subprocess.CalledProcessError as e:
lActual = e.output.decode('utf-8').split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputFileWithConfig, lOutputFile)
lExpected = pathlib.Path(sOutputFileWithConfig).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
def test_entity_with_configuration_and_fixed(self):
lActual = subprocess.check_output(
['bin/vsg', '-f', sEntityFileName, '-c', sConfigFile, '--fix']
).decode('utf-8').split('\n')
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_entity_with_configuration_and_fixed(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
lExpected = pathlib.Path(sOutputFileWithConfigFixed).read_text().split('\n')
self.assertEqual(utils.replace_total_count(lActual), lExpected)
def test_architecture_without_configuration(self):
try:
sys.argv = ['vsg', '-f', sEntityFileName, '-c', sConfigFile, '--fix']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 0)
subprocess.check_output(['bin/vsg', '-f', sArchitectureFileName])
except subprocess.CalledProcessError as e:
lActual = e.output.decode('utf-8').split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputFileWithConfigFixed, lOutputFile)
lExpected = pathlib.Path(sArchitectureOutputFileWoConfig).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_architecture_without_configuration(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
def test_architecture_with_configuration(self):
try:
sys.argv = ['vsg', '-f', sArchitectureFileName]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg','-f', sArchitectureFileName, '-c', sConfigFile])
except subprocess.CalledProcessError as e:
lActual = e.output.decode('utf-8').split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sArchitectureOutputFileWoConfig, lOutputFile)
lExpected = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
def test_architecture_with_configuration_and_fixed(self):
lActual = subprocess.check_output(
['bin/vsg','-f', sArchitectureFileName, '-c', sConfigFile, '--fix']
).decode('utf-8').split('\n')
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_architecture_with_configuration(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
lExpected = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().split('\n')
self.assertEqual(utils.replace_total_count(lActual), lExpected)
def test_both_with_configuration(self):
try:
sys.argv = ['vsg', '-f', sArchitectureFileName, '-c', sConfigFile]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg', '-f', sEntityFileName, sArchitectureFileName, '-c', sConfigFile])
except subprocess.CalledProcessError as e:
lActual = e.output.decode('utf-8').split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sArchitectureOutputFileWithConfig, lOutputFile)
lExpected1 = pathlib.Path(sOutputFileWithConfig).read_text().rstrip('\n').split('\n')
lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfig).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected1 + lExpected2)
mock_stdout.write.assert_has_calls(lExpected)
def test_both_with_configuration_and_fixed(self):
lActual = subprocess.check_output(
['bin/vsg', '-f', sEntityFileName, sArchitectureFileName, '-c', sConfigFile, '--fix']
).decode('utf-8').split('\n')
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_architecture_with_configuration_and_fixed(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
lExpected1 = pathlib.Path(sOutputFileWithConfigFixed).read_text().rstrip('\n').split('\n')
lExpected2 = pathlib.Path(sArchitectureOutputFileWithConfigFixed).read_text().split('\n')
self.assertEqual(utils.replace_total_count(lActual), lExpected1 + lExpected2)
def test_oc_option(self):
lActual = subprocess.check_output(['bin/vsg', '-oc', 'blah.json']).decode('utf-8').split('\n')
self.assertEqual(lActual, [''])
def test_junit_output(self):
try:
sys.argv = ['vsg', '-f', sArchitectureFileName, '-c', sConfigFile, '--fix']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 0)
subprocess.check_output(['bin/vsg', '-f', sEntityFileName, '-c', sConfigFile, '-j', sJUnitFileName])
except subprocess.CalledProcessError as e:
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sArchitectureOutputFileWithConfigFixed, lOutputFile)
lActual = pathlib.Path(sJUnitFileName).read_text().split('\n')
lExpected = pathlib.Path(sJUnitFile).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_both_with_configuration(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
try:
sys.argv = ['vsg', '-f', sEntityFileName, sArchitectureFileName, '-c', sConfigFile]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
lOutputFile = []
utils.read_file(sOutputFileWithConfig, lOutputFile)
utils.read_file(sArchitectureOutputFileWithConfig, lOutputFile)
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_both_with_configuration_and_fixed(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
try:
sys.argv = ['vsg', '-f', sEntityFileName, sArchitectureFileName, '-c', sConfigFile, '--fix']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 0)
lOutputFile = []
utils.read_file(sOutputFileWithConfigFixed, lOutputFile)
utils.read_file(sArchitectureOutputFileWithConfigFixed, lOutputFile)
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
def test_oc_option(self, mock_stdout):
try:
sys.argv = ['vsg', '-oc', 'blah.json']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 0)
lExpected = []
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
def test_junit_output(self,mock_stdout):
try:
sys.argv = ['vsg', '-f', sEntityFileName, '-c', sConfigFile, '-j', sJUnitFileName]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
lActual = []
utils.read_file(sJUnitFileName, lActual)
lExpected = []
utils.read_file(sJUnitFile, lExpected)
self.assertEqual(iExitStatus, 1)
self.assertEqual(len(lActual), len(lExpected))

View File

@ -24,67 +24,40 @@ class testOSError(unittest.TestCase):
if os.path.isfile(sNoPermissionFile):
os.remove(sNoPermissionFile)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_file_not_found(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
def test_file_not_found(self):
try:
sys.argv = ['vsg', '-f', 'no_file.vhd']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg','-f', 'no_file.vhd'])
except subprocess.CalledProcessError as e:
lActual = str(e.output.decode('utf-8')).split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputNoFile, lOutputFile)
lExpected = pathlib.Path(sOutputNoFile).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_file_no_permission(self, mock_rule_ran, mock_stdout):
def test_file_no_permission(self):
pathlib.Path(sNoPermissionFile).touch(mode=0o222, exist_ok=True)
# exists = pathlib.Path('vsg/tests/source_file/'+sNoPermissionFile).exists()
# self.assertEqual(exists, "1")
mock_rule_ran.return_value = 200
try:
sys.argv = ['vsg', '-f', sNoPermissionFile]
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg', '-f', sNoPermissionFile])
except subprocess.CalledProcessError as e:
lActual = str(e.output.decode('utf-8')).split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputNoPermission, lOutputFile)
lExpected = pathlib.Path(sOutputNoPermission).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)
mock_stdout.write.assert_has_calls(lExpected)
@mock.patch('sys.stdout')
@mock.patch('vsg.__main__.rule_list.rule_list.get_number_of_rules_ran')
def test_file_empty(self, mock_rule_ran, mock_stdout):
mock_rule_ran.return_value = 200
def test_file_empty(self):
try:
sys.argv = ['vsg', '-f', 'vsg/tests/source_file/empty_file.vhd']
__main__.main()
except SystemExit as e:
self.assertEqual(e.code, 1)
subprocess.check_output(['bin/vsg', '-f', 'vsg/tests/source_file/' + sEmptyFile])
except subprocess.CalledProcessError as e:
lActual = str(e.output.decode('utf-8')).split('\n')
iExitStatus = e.returncode
lOutputFile = []
utils.read_file(sOutputEmptyFile, lOutputFile)
lExpected = pathlib.Path(sOutputEmptyFile).read_text().split('\n')
lExpected = []
for sLine in lOutputFile:
lExpected.append(mock.call(sLine))
lExpected.append(mock.call('\n'))
mock_stdout.write.assert_has_calls(lExpected)
self.assertEqual(iExitStatus, 1)
self.assertEqual(utils.replace_total_count(lActual), lExpected)

View File

@ -1,4 +1,4 @@
import re
import os
import pprint
import yaml
@ -114,3 +114,6 @@ def print_objects(oFile, bIgnoreWhiteSpace=False):
def extract_lrm_unit_name(sName):
return sName.split('.')[-1][5:]
def replace_total_count(lOutput):
return [re.sub(r'^Total Rules Checked: [1-9][0-9]*$', 'Total Rules Checked: 200', line) for line in lOutput]

View File

@ -142,10 +142,15 @@ class testVsg(unittest.TestCase):
self.assertEqual(lActual, lExpected)
def test_invalid_local_rule_directory(self):
lExpected = ['ERROR: specified local rules directory vsg/tests/vsg/invalid_local_rule_directory could not be found.']
lExpected.append('')
lActual = subprocess.check_output(['bin/vsg', '-f', 'vsg/tests/vsg/entity_architecture.vhd', '-of', 'syntastic', '-lr', 'vsg/tests/vsg/invalid_local_rule_directory'])
lActual = str(lActual.decode('utf-8')).split('\n')
lExpected = ['ERROR: specified local rules directory vsg/tests/vsg/invalid_local_rule_directory could not be found.', '']
try:
lActual = subprocess.check_output(['bin/vsg', '-f', 'vsg/tests/vsg/entity_architecture.vhd', '-of', 'syntastic', '-lr', 'vsg/tests/vsg/invalid_local_rule_directory'])
except subprocess.CalledProcessError as e:
lActual = str(e.output.decode('utf-8')).split('\n')
iExitStatus = e.returncode
self.assertEqual(iExitStatus, 1)
self.assertEqual(lActual, lExpected)
def test_globbing_filenames_in_configuration(self):