def check_pycolor_main(self, args: typing.List[str], mocked_data_dir: str, test_name: str, **kwargs): stdin: typing.TextIO = kwargs.get('stdin', sys.stdin) patch_stdout: bool = kwargs.get('patch_stdout', False) patch_stderr: bool = kwargs.get('patch_stderr', False) print_output: bool = kwargs.get('print_output', False) write_output: bool = kwargs.get('write_output', False) no_load_args: bool = kwargs.get('no_load_args', False) patch_sample_config_dir: bool = kwargs.get('patch_sample_config_dir', True) filename_prefix = os.path.join(mocked_data_dir, test_name) stdout = textstream() stderr = textstream() if not no_load_args: args = ['--load-file', filename_prefix + '.json'] + args args = ['--color', 'always'] + args stdout_in = open_fstream(filename_prefix + '.txt') stderr_in = open_fstream(filename_prefix + '.err.txt') with ExitStack() as stack: stack.enter_context( execute_patch(pycolor_class.execute, stdout_in, stderr_in)) if patch_stdout: stack.enter_context(patch(sys, 'stdout', stdout)) if patch_stderr: stack.enter_context(patch(sys, 'stderr', stderr)) if patch_sample_config_dir: stack.enter_context( patch(pycolor.config, 'SAMPLE_CONFIG_DIR', None)) try: pycolor.main(args, stdout_stream=stdout, stderr_stream=stderr, stdin_stream=stdin) except SystemExit as sexc: if sexc.code != 0: raise sexc finally: if stdout_in is not None: stdout_in.close() if stderr_in is not None: stderr_in.close() output_expected = read_file(filename_prefix + '.out.txt') output_expected_err = read_file(filename_prefix + '.out.err.txt') test_stream(self, stdout, filename_prefix + '.out.txt', output_expected, print_output, write_output) test_stream(self, stderr, filename_prefix + '.out.err.txt', output_expected_err, print_output, write_output)
def set_stream(name, stream): res = kwargs.get(name) if isinstance(res, int) and res != -1: if stream is not None: os.write(res, stream.read()) else: res = stream if stream else textstream() return res
def test_copy_sample_config(self): self.assertTrue(os.path.isdir(SAMPLE_CONFIG_DIR)) with tempfile.TemporaryDirectory() as tmpdir: tmpconfig = os.path.join(tmpdir, 'config') with patch(sys, 'stdout', textstream()), patch(pycolor, 'CONFIG_DIR', tmpconfig): check_pycolor_main(self, ['--version'], MOCKED_DATA, 'empty', patch_sample_config_dir=False) self.assertListEqual(sorted(os.listdir(SAMPLE_CONFIG_DIR)), sorted(os.listdir(tmpconfig)))
def test_stdin_config_invalid_profile(self): with patch_stderr() as stream: stdin = textstream() check_pycolor_main(self, ['--stdin', 'rsync'], MOCKED_DATA, 'invalid_profile', stdin=stdin) stream.seek(0) result = stream.read() self.assertTrue( result.startswith( '\x1b[91merror\x1b[0m: \x1b[93m%s\x1b[0m: ' % (os.path.join(MOCKED_DATA, 'invalid_profile.json'))))
def test_stdin_config_invalid_pattern(self): with patch_stderr() as stream: stdin = textstream() with self.assertRaises(SystemExit): check_pycolor_main(self, ['--stdin', 'rsync'], MOCKED_DATA, 'invalid_pattern', stdin=stdin) stream.seek(0) result = stream.read() self.assertEqual( result, '\x1b[91merror\x1b[0m: "expression": regex nothing to repeat at position 0\n' )
def test_load_sample_config(self): self.assertTrue(os.path.isdir(SAMPLE_CONFIG_DIR)) with patch(pycolor, 'CONFIG_DIR', SAMPLE_CONFIG_DIR),\ patch(pycolor, 'CONFIG_DEFAULT', os.path.join(SAMPLE_CONFIG_DIR, 'rsync.json')): stdin = textstream() with open(os.path.join(MOCKED_DATA, 'load_sample_config.txt'), 'r') as file: stdin.write(file.read()) stdin.seek(0) check_pycolor_main(self, ['--stdin', 'rsync'], MOCKED_DATA, 'load_sample_config', stdin=stdin, no_load_args=True)
def create_pycolor_object(debug: int = 0) -> Pycolor: return Pycolor(color_mode='always', debug=debug, stdout=textstream(), stderr=textstream())