def test_windows_screenshot(colors, light_bg): """Test script on Windows in a new console window. Take a screenshot to verify colors work. :param bool colors: Enable, disable, or omit color arguments (default has colors). :param bool light_bg: Create console with white background color. """ screenshot = PROJECT_ROOT.join('test_example_test_windows_screenshot.png') if screenshot.check(): screenshot.remove() command = [sys.executable, str(PROJECT_ROOT.join('example.py')), 'print', '-w', str(screenshot)] # Set options. if colors is True: command.append('--colors') elif colors is False: command.append('--no-colors') # Setup expected. if colors is False: candidates = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_sans_*.bmp')] expected_count = 27 elif light_bg: candidates = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_dark_fg_*.bmp')] expected_count = 2 else: candidates = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_light_fg_*.bmp')] expected_count = 2 assert candidates # Run. with RunNewConsole(command, maximized=True, white_bg=light_bg) as gen: screenshot_until_match(str(screenshot), 15, candidates, expected_count, gen)
def create_command_line(args, sample_type): python_path = PROJECT_ROOT.as_posix() executable = EXAMPLES_DIR.joinpath(sample_type, 'main.py').as_posix() cli_args = " ".join(key if val is None else "{} {}".format(key, val) for key, val in args.items()) return "PYTHONPATH={path} {python_exe} {main_py} {args}".format( path=python_path, main_py=executable, args=cli_args, python_exe=sys.executable )
def test_enable_disable(tmpdir): """Test enabling, disabling, repeat. Make sure colors still work. :param tmpdir: pytest fixture. """ screenshot = PROJECT_ROOT.join('test_windows_test_enable_disable.png') if screenshot.check(): screenshot.remove() script = tmpdir.join('script.py') command = [sys.executable, str(script)] script.write( dedent("""\ from __future__ import print_function import os, time from colorclass import Color, Windows with Windows(auto_colors=True): print(Color('{autored}Red{/autored}')) print('Red') with Windows(auto_colors=True): print(Color('{autored}Red{/autored}')) print('Red') stop_after = time.time() + 20 while not os.path.exists(r'%s') and time.time() < stop_after: time.sleep(0.5) """) % str(screenshot)) # Setup expected. with_colors = [ str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_light_fg_*.bmp') ] sans_colors = [ str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_sans_*.bmp') ] assert with_colors assert sans_colors # Run. with RunNewConsole(command) as gen: screenshot_until_match(str(screenshot), 15, with_colors, 2, gen) screenshot_until_match(str(screenshot), 15, sans_colors, 2, gen)
def test_enable_disable(tmpdir): """Test enabling, disabling, repeat. Make sure colors still work. :param tmpdir: pytest fixture. """ screenshot = PROJECT_ROOT.join('test_windows_test_enable_disable.png') if screenshot.check(): screenshot.remove() script = tmpdir.join('script.py') command = [sys.executable, str(script)] script.write(dedent("""\ from __future__ import print_function import os, time from colorclass import Color, Windows with Windows(auto_colors=True): print(Color('{autored}Red{/autored}')) print('Red') with Windows(auto_colors=True): print(Color('{autored}Red{/autored}')) print('Red') stop_after = time.time() + 20 while not os.path.exists(r'%s') and time.time() < stop_after: time.sleep(0.5) """) % str(screenshot)) # Setup expected. with_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_light_fg_*.bmp')] sans_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_red_sans_*.bmp')] assert with_colors assert sans_colors # Run. with RunNewConsole(command) as gen: screenshot_until_match(str(screenshot), 15, with_colors, 2, gen) screenshot_until_match(str(screenshot), 15, sans_colors, 2, gen)
def test_piped(colors, light_bg): """Test script with output piped to non-tty (this pytest process). :param bool colors: Enable, disable, or omit color arguments (default is no colors due to no tty). :param bool light_bg: Enable light, dark, or omit light/dark arguments. """ command = [sys.executable, str(PROJECT_ROOT.join('example.py')), 'print'] # Set options. if colors is True: command.append('--colors') elif colors is False: command.append('--no-colors') if light_bg is True: command.append('--light-bg') elif light_bg is False: command.append('--dark-bg') # Run. proc = subprocess.Popen(command, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) output = proc.communicate()[0].decode() assert proc.poll() == 0 assert 'Autocolors for all backgrounds' in output assert 'Red' in output # Verify colors. Output is always stripped of all colors on Windows when piped to non-console (e.g. pytest). if colors is False or IS_WINDOWS: assert '\033[' not in output assert 'Black Red Green Yellow Blue Magenta Cyan White' in output return assert '\033[' in output # Verify light bg. count_dark_fg = output.count('\033[31mRed') count_light_fg = output.count('\033[91mRed') if light_bg: assert count_dark_fg == 2 assert count_light_fg == 1 else: assert count_dark_fg == 1 assert count_light_fg == 2
def create_command_line(args, venv, python=sys.executable, cuda_string=""): python_path = PROJECT_ROOT.as_posix() line = "PYTHONPATH={path} {venv_activate}; {cuda} {python_exe} {args}"\ .format(path=python_path, venv_activate=venv, cuda=cuda_string, args=args, python_exe=python) return line
def test_box_characters(tmpdir): """Test for unicode errors with special characters. :param tmpdir: pytest fixture. """ screenshot = PROJECT_ROOT.join('test_windows_test_box_characters.png') if screenshot.check(): screenshot.remove() script = tmpdir.join('script.py') command = [sys.executable, str(script)] script.write( dedent("""\ from __future__ import print_function import os, time from colorclass import Color, Windows Windows.enable(auto_colors=True) chars = [ '+', '-', '|', b'\\xb3'.decode('ibm437'), b'\\xb4'.decode('ibm437'), b'\\xb9'.decode('ibm437'), b'\\xba'.decode('ibm437'), b'\\xbb'.decode('ibm437'), b'\\xbc'.decode('ibm437'), b'\\xbf'.decode('ibm437'), b'\\xc0'.decode('ibm437'), b'\\xc1'.decode('ibm437'), b'\\xc2'.decode('ibm437'), b'\\xc3'.decode('ibm437'), b'\\xc4'.decode('ibm437'), b'\\xc5'.decode('ibm437'), b'\\xc8'.decode('ibm437'), b'\\xc9'.decode('ibm437'), b'\\xca'.decode('ibm437'), b'\\xcb'.decode('ibm437'), b'\\xcc'.decode('ibm437'), b'\\xcd'.decode('ibm437'), b'\\xce'.decode('ibm437'), b'\\xd9'.decode('ibm437'), b'\\xda'.decode('ibm437'), ] for c in chars: print(c, end='') print() for c in chars: print(Color.green(c, auto=True), end='') print() stop_after = time.time() + 20 while not os.path.exists(r'%s') and time.time() < stop_after: time.sleep(0.5) """) % str(screenshot)) # Setup expected. with_colors = [ str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_green_*.bmp') ] sans_colors = [ str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_sans_*.bmp') ] assert with_colors assert sans_colors # Run. with RunNewConsole(command) as gen: screenshot_until_match(str(screenshot), 15, with_colors, 1, gen) screenshot_until_match(str(screenshot), 15, sans_colors, 1, gen)
def test_box_characters(tmpdir): """Test for unicode errors with special characters. :param tmpdir: pytest fixture. """ screenshot = PROJECT_ROOT.join('test_windows_test_box_characters.png') if screenshot.check(): screenshot.remove() script = tmpdir.join('script.py') command = [sys.executable, str(script)] script.write(dedent("""\ from __future__ import print_function import os, time from colorclass import Color, Windows Windows.enable(auto_colors=True) chars = [ '+', '-', '|', b'\\xb3'.decode('ibm437'), b'\\xb4'.decode('ibm437'), b'\\xb9'.decode('ibm437'), b'\\xba'.decode('ibm437'), b'\\xbb'.decode('ibm437'), b'\\xbc'.decode('ibm437'), b'\\xbf'.decode('ibm437'), b'\\xc0'.decode('ibm437'), b'\\xc1'.decode('ibm437'), b'\\xc2'.decode('ibm437'), b'\\xc3'.decode('ibm437'), b'\\xc4'.decode('ibm437'), b'\\xc5'.decode('ibm437'), b'\\xc8'.decode('ibm437'), b'\\xc9'.decode('ibm437'), b'\\xca'.decode('ibm437'), b'\\xcb'.decode('ibm437'), b'\\xcc'.decode('ibm437'), b'\\xcd'.decode('ibm437'), b'\\xce'.decode('ibm437'), b'\\xd9'.decode('ibm437'), b'\\xda'.decode('ibm437'), ] for c in chars: print(c, end='') print() for c in chars: print(Color.green(c, auto=True), end='') print() stop_after = time.time() + 20 while not os.path.exists(r'%s') and time.time() < stop_after: time.sleep(0.5) """) % str(screenshot)) # Setup expected. with_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_green_*.bmp')] sans_colors = [str(p) for p in PROJECT_ROOT.join('tests').listdir('sub_box_sans_*.bmp')] assert with_colors assert sans_colors # Run. with RunNewConsole(command) as gen: screenshot_until_match(str(screenshot), 15, with_colors, 1, gen) screenshot_until_match(str(screenshot), 15, sans_colors, 1, gen)