def test_version(self, versionarg, capsys): with raises(SystemExit) as exc_info: main(['progname', versionarg]) captured = capsys.readouterr() # Should print out version. assert captured.err == '' assert captured.out == '{0} {1}\n'. \ format(metadata.project, metadata.version) # Should exit with zero return code. assert exc_info.value.code == 0
def test_help(self, helparg, capsys): with raises(SystemExit) as exc_info: main(['progname', helparg]) out, err = capsys.readouterr() # Should have printed some sort of usage message. We don't # need to explicitly test the content of the message. assert 'usage' in out # Should have used the program name from the argument # vector. assert 'progname' in out # Should exit with zero return code. assert exc_info.value.code == 0
def execute_test(file_to_test, capsys, path_of_files, include_dirs=None): """Execute the tests for the preprocessor. Args: file_to_test (str): the file to test capsys (method): the capsys fixture from pytest path_of_files (str): the path of the include_dirs (Union[str, None]): the include command line argument if needed """ input_path = 'input' output_path = 'output' input_path = join(path_of_files, input_path) output_path = join(path_of_files, output_path) file_to_preprocess = file_to_test input_file_with_path = join(input_path, file_to_preprocess) output_file_with_path = join(output_path, file_to_preprocess) # this test will not raise SystemExit argsv = list(['progname']) argsv.append('-E') if include_dirs: argsv.extend(include_dirs) argsv.append(input_file_with_path) main(argsv) out, err = capsys.readouterr() with open(output_file_with_path, 'r') as fileToRead: output_file_as_string = fileToRead.read() # the outputted file needs to match exactly output_file_as_string = output_file_as_string.replace('\r', '') output_list = out.split('\n') output_file_as_list = output_file_as_string.split('\n') output_list_size = len(output_list) output_file_as_list_size = len(output_file_as_list) assert output_list_size == output_file_as_list_size, \ 'for file %s, size %d(got) != %d(req), got <%s> should be <%s>' % \ (file_to_preprocess, output_list_size, output_file_as_list_size, out, output_file_as_string) for i in range(output_file_as_list_size): assert output_list[i] == output_file_as_list[i], \ 'for file %s line %d, <%s> != <%s>' % \ (file_to_preprocess, i, output_list, output_file_as_list) # there should be no error assert err == ''
def run(args): """Run the package's main script. All arguments are passed to it.""" # The main script expects to get the called executable's name as # argv[0]. However, paver doesn't provide that in args. Even if it did (or # we dove into sys.argv), it wouldn't be useful because it would be paver's # executable. So we just pass the package name in as the executable name, # since it's close enough. This should never be seen by an end user # installing through Setuptools anyway. from pcc.main import main raise SystemExit(main([CODE_DIRECTORY] + args))
def test_include(self, file_to_test, capsys): include_dirs = ['-Itests/preprocessor/include/input'] input_path = 'input' path_of_this_file = abspath(dirname(__file__)) input_path = join(path_of_this_file, input_path) file_to_preprocess = file_to_test input_file_with_path = join(input_path, file_to_preprocess) # this test will not raise SystemExit argsv = list(['progname']) argsv.append('-E') argsv.extend(include_dirs) argsv.append(input_file_with_path) main(argsv) out, err = capsys.readouterr() # there should be an error assert err is not None assert "ERROR:" in err assert "file " in err assert "line " in err
def execute_test(self, input_file, helper_file, output_file, capsys, path_of_files): """Execute the tests for the preprocessor. Args: input_file (str): the file to test helper_file (str): the helper file to test output_file (str): the output file of the test capsys (method): the capsys fixture from pytest path_of_files (str): the path of the """ input_path = 'input' output_path = 'output' pcc_output_file_name = 'test.out' input_path = join(path_of_files, input_path) helper_path = join(path_of_files, 'input', helper_file) output_path = join(path_of_files, output_path) pcc_output_file_path = join(path_of_files, pcc_output_file_name) input_file_with_path = join(input_path, input_file) output_file_with_path = join(output_path, output_file) # this test will not raise SystemExit argsv = list(['progname']) argsv.append('-c') argsv.append('-o' + str(pcc_output_file_path)) argsv.append(input_file_with_path) main(argsv) out, err = capsys.readouterr() assert out == '' # there should be no error assert err == '' gcc_exe = 'test' command = 'gcc %s -o %s %s' % \ (helper_path, gcc_exe, pcc_output_file_path) response = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) assert response.returncode == 0, response.stderr command = './' + gcc_exe response = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) assert response.returncode == 0, response.stderr # decode the outputted string to ascii and split the line while # keeping the new line character(s) captured_result = response.stdout.decode('ascii').splitlines(True) output_file_as_list, output_file_as_list_size = \ self.extract_file_contents(output_file_with_path) captured_result = ''.join(captured_result) output_list = captured_result.split('\n') output_list_size = len(output_list) # the outputted file needs to match exactly assert output_list_size == output_file_as_list_size, \ 'for file %s, size %d != %d \n(%s)' % \ (output_file, output_list_size, output_file_as_list_size, out) for i in range(output_file_as_list_size): assert output_list[i] == output_file_as_list[i], \ 'for file %s line %d, <%s> != <%s>' % \ (output_file, i, output_list[i], output_file_as_list[i]) os.remove(pcc_output_file_path) os.remove(gcc_exe)