test_script_example.py
1 import os 2 import re 3 from tempfile import mkstemp 4 5 from .common import check_assert, check_command, check_returncode 6 7 8 def test_example(): 9 result = check_command('./example', '-h') 10 check_returncode(result) 11 check_assert(result, '{config,dashed-hello,hello,goodbye,config-file}' in result.stdout) 12 13 14 def test_example_version(): 15 result = check_command('./example', '--version') 16 check_returncode(result) 17 check_assert(result, re.match(r'[0-9]*\.[0-9]*\.[0-9]*', result.stdout)) 18 19 20 def test_example_config(): 21 fd, tempfile = mkstemp() 22 23 try: 24 os.close(fd) 25 26 # Check initial state 27 result = check_command('./example', '--no-color', '--config-file', tempfile, 'config') 28 check_returncode(result) 29 check_assert(result, result.stdout == '') 30 31 # Set some values in the configuration 32 result = check_command('./example', '--no-color', '--config-file', tempfile, 'config', 'general.name=Test', 'user.comma=true') 33 check_returncode(result) 34 check_assert(result, 'general.name: World -> Test' in result.stdout) 35 check_assert(result, 'user.comma: None -> true' in result.stdout) 36 37 # Make sure we get them back 38 result = check_command('./example', '--no-color', '--config-file', tempfile, 'config') 39 check_returncode(result) 40 check_assert(result, result.stdout == 'general.name=Test\nhello.comma=True\nuser.comma=True\n') 41 42 finally: 43 os.remove(tempfile) 44 45 46 def test_numeric_arguments(): 47 fd, tempfile = mkstemp() 48 49 try: 50 os.close(fd) 51 52 # Set the count 53 result = check_command('./example', '--no-color', '--config-file', tempfile, 'config', 'hello.count=2') 54 check_returncode(result) 55 check_assert(result, 'hello.count: 1 -> 2' in result.stdout) 56 57 # Make sure we get 2 prints 58 result = check_command('./example', '--no-color', '--config-file', tempfile, 'hello') 59 check_returncode(result) 60 print(repr(result.stdout)) 61 check_assert(result, result.stdout == 'Hello, World!\nHello, World!\n') 62 63 # Make sure we get 1 prints 64 result = check_command('./example', '--no-color', '--config-file', tempfile, 'hello', '--count', '1') 65 check_returncode(result) 66 print(repr(result.stdout)) 67 check_assert(result, result.stdout == 'Hello, World!\n') 68 69 finally: 70 os.remove(tempfile) 71 72 73 def test_example_hello(): 74 result = check_command('./example', '--config-file', '/dev/null', 'hello') 75 check_returncode(result) 76 check_assert(result, result.stdout == 'Hello, World!\n') 77 78 79 def test_example_hello_help(): 80 result = check_command('./example', '--config-file', '/dev/null', 'hello', '--help') 81 check_returncode(result) 82 check_assert(result, '[--no-comma]' in result.stdout) 83 check_assert(result, '[--print-help]' in result.stdout) 84 check_assert(result, '[--print-usage]' in result.stdout) 85 86 87 def test_example_hello_no_color(): 88 result = check_command('./example', '--no-color', '--config-file', '/dev/null', 'hello') 89 check_returncode(result) 90 check_assert(result, result.stdout == 'Hello, World!\n') 91 92 93 def test_example_hello_no_color_no_unicode(): 94 result = check_command('./example', '--no-color', '--no-unicode', '--config-file', '/dev/null', 'hello') 95 check_returncode(result) 96 check_assert(result, result.stdout == 'Hello, World!\n') 97 98 99 def test_example_hello_no_color_no_comma(): 100 result = check_command('./example', '--no-color', '--config-file', '/dev/null', 'hello', '--no-comma') 101 check_returncode(result) 102 check_assert(result, result.stdout == 'Hello World!\n') 103 104 105 def test_example_hello_no_color_no_unicode_no_comma(): 106 result = check_command('./example', '--no-color', '--no-unicode', '--config-file', '/dev/null', 'hello', '--no-comma') 107 check_returncode(result) 108 check_assert(result, result.stdout == 'Hello World!\n') 109 110 111 def test_example_dashed_hello(): 112 result = check_command('./example', '--config-file', '/dev/null', 'dashed-hello') 113 check_returncode(result) 114 check_assert(result, result.stdout == 'Hello, dashed-subcommand World!\n') 115 116 117 def test_example_dashed_hello_dashed_name(): 118 result = check_command('./example', '--config-file', '/dev/null', 'dashed-hello', '--dashed-name', 'Tester') 119 check_returncode(result) 120 check_assert(result, result.stdout == 'Hello, dashed-subcommand Tester!\n') 121 122 123 def test_example_config_file(): 124 result = check_command('./example', 'config-file') 125 check_returncode(result) 126 lines = result.stdout.split('\n') 127 filename = lines[0].split('=', 1)[1] 128 filedir = lines[1].split('=', 1)[1] 129 check_assert(result, len(lines) == 3) 130 check_assert(result, filename.endswith('example.ini')) 131 check_assert(result, filename.startswith(filedir))