def test_manual_overhead_simplebar_hard(): """ Test overhead of manual tqdm vs simple progress bar (hard) """ total = int(1e4) with closing(MockIO()) as our_file: t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) simplebar_update = simple_progress(total=total, file=our_file, leave=True, miniters=1, mininterval=0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i simplebar_update(10) # Compute relative overhead of tqdm against native range() try: assert(time_tqdm() < 2 * time_bench()) except AssertionError: raise AssertionError('tqdm(%g): %f, simple_progress(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_manual_overhead_hard(): """ Test overhead of manual tqdm (hard) """ total = int(1e5) with closing(MockIO()) as our_file: t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) # Compute relative overhead of tqdm against native range() try: assert(time_tqdm() < 100 * time_bench()) except AssertionError: raise AssertionError('tqdm(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_manual_overhead_hard(): """Test overhead of manual tqdm (hard)""" total = int(1e5) with closing(MockIO()) as our_file: with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) assert_performance(130, 'tqdm', time_tqdm(), 'range', time_bench())
def test_manual_overhead_simplebar_hard(): """Test overhead of manual tqdm vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) simplebar_update = simple_progress( total=total, file=our_file, leave=True, miniters=1, mininterval=0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i simplebar_update(10) # Compute relative overhead of tqdm against native range() try: assert time_tqdm() < 2.5 * time_bench() except AssertionError: raise AssertionError('tqdm(%g): %f, simple_progress(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_manual_overhead(): """ Test overhead of manual tqdm """ try: assert checkCpuTime() except: raise SkipTest total = int(1e6) with closing(MockIO()) as our_file: with tqdm(total=total * 10, file=our_file, leave=True) as t: a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) # Compute relative overhead of tqdm against native range() if time_tqdm() > 10 * time_bench(): raise AssertionError('tqdm(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_manual_overhead_simplebar_hard(): """Test overhead of manual tqdm vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: with tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) simplebar_update = simple_progress(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i simplebar_update(10) assert_performance(10, 'tqdm', time_tqdm(), 'simple_progress', time_bench())
def test_manual_overhead_hard(): """Test overhead of manual tqdm (hard)""" total = int(1e5) with closing(MockIO()) as our_file: t = tqdm(total=total * 10, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) # Compute relative overhead of tqdm against native range() try: assert time_tqdm() < 100 * time_bench() except AssertionError: raise AssertionError('tqdm(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm.cli import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI --delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write('\0'.join(map(str, _range(int(123))))) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) main() sys.stdin = IN_DATA_LIST sys.argv = [ '', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA with closing(StringIO()) as sys.stdin: IN_DATA = '\0'.join(IN_DATA_LIST) sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes=True', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(123))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() sys.stdin = IN_DATA_LIST # clean up sys.stdin, sys.argv = _SYS
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = ['', '--desc', 'Test CLI --delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64'] sys.stdin.write('\0'.join(map(str, _range(int(123))))) #sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) main() sys.stdin = IN_DATA_LIST sys.argv = ['', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True'] import tqdm.__main__ # NOQA with closing(StringIO()) as sys.stdin: IN_DATA = '\0'.join(IN_DATA_LIST) sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(123))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() sys.stdin = IN_DATA_LIST # clean up sys.stdin, sys.argv = _SYS
def test_iter_overhead_hard(): """Test overhead of iteration based tqdm (hard)""" total = int(1e5) with closing(MockIO()) as our_file: a = 0 with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm: for i in t: a += i assert a == (total * total - total) / 2.0 a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) assert_performance(130, 'trange', time_tqdm(), 'range', time_bench())
def test_iter_overhead_simplebar_hard(): """ Test overhead of iteration based tqdm vs simple progress bar (hard) """ total = int(1e4) with closing(MockIO()) as our_file: a = 0 with relative_timer() as time_tqdm: for i in trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0): a += i assert(a == (total * total - total) / 2.0) a = 0 with relative_timer() as time_bench: for i in simple_progress(_range(total), file=our_file, leave=True, miniters=1, mininterval=0): a += i # Compute relative overhead of tqdm against native range() try: assert(time_tqdm() < 2 * time_bench()) except AssertionError: raise AssertionError('trange(%g): %f, simple_progress(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_iter_overhead_hard(): """Test overhead of iteration based tqdm (hard)""" total = int(1e5) with closing(MockIO()) as our_file: a = 0 with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm: for i in t: a += i assert a == (total * total - total) / 2.0 a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) # Compute relative overhead of tqdm against native range() try: assert time_tqdm() < 60 * time_bench() except AssertionError: raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_main(): """ Test command line pipes """ ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen(('ls'), stdout=subprocess.PIPE) res = _sh('python', '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert (ls_out in res.replace('\r\n', '\n')) # semi-fake test which gets coverage: try: _SYS = (deepcopy(sys.stdin), deepcopy(sys.argv)) except: pass sys.stdin = map(str, _range(int(1e3))) sys.argv = ['', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True'] import tqdm.__main__ # NOQA sys.argv = ['', '--ascii', '--unit_scale', 'False', '--desc', 'Test CLI errors'] main() sys.argv = ['', '--bad_arg_u_ment', 'foo', '--ascii', '--unit_scale'] try: main() except KeyError as e: if 'bad_arg_u_ment' not in str(e): raise sys.argv = ['', '--ascii', '--unit_scale', 'invalid_bool_value'] try: main() except ValueError as e: if 'invalid_bool_value' not in str(e): raise sys.argv = ['', '--ascii', '--total', 'invalid_int_value'] try: main() except ValueError as e: if 'invalid_int_value' not in str(e): raise for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main() except SystemExit: pass # clean up try: sys.stdin, sys.argv = _SYS except: pass
def test_iter_overhead(): """ Test overhead of iteration based tqdm """ try: assert checkCpuTime() except: raise SkipTest total = int(1e6) with closing(MockIO()) as our_file: a = 0 with relative_timer() as time_tqdm: for i in trange(total, file=our_file): a += i assert (a == (total * total - total) / 2.0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) # Compute relative overhead of tqdm against native range() if time_tqdm() > 9 * time_bench(): raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_iter_overhead_simplebar_hard(): """Test overhead of iteration based tqdm vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: a = 0 with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm: for i in t: a += i assert a == (total * total - total) / 2.0 a = 0 s = simple_progress(_range(total), file=our_file, leave=True, miniters=1, mininterval=0) with relative_timer() as time_bench: for i in s: a += i assert_performance(10, 'trange', time_tqdm(), 'simple_progress', time_bench())
def test_iter_overhead_hard(): """ Test overhead of iteration based tqdm (hard) """ total = int(1e5) with closing(MockIO()) as our_file: a = 0 with relative_timer() as time_tqdm: for i in trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0): a += i assert(a == (total * total - total) / 2.0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(("%i" % a) * 40) # Compute relative overhead of tqdm against native range() try: assert(time_tqdm() < 60 * time_bench()) except AssertionError: raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_iter_overhead_simplebar_hard(): """Test overhead of iteration based tqdm vs simple progress bar (hard)""" total = int(1e4) with closing(MockIO()) as our_file: a = 0 with trange(total, file=our_file, leave=True, miniters=1, mininterval=0, maxinterval=0) as t: with relative_timer() as time_tqdm: for i in t: a += i assert a == (total * total - total) / 2.0 a = 0 s = simple_progress(_range(total), file=our_file, leave=True, miniters=1, mininterval=0) with relative_timer() as time_bench: for i in s: a += i # Compute relative overhead of tqdm against native range() try: assert time_tqdm() < 2.5 * time_bench() except AssertionError: raise AssertionError('trange(%g): %f, simple_progress(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_iter_overhead(): """ Test overhead of iteration based tqdm """ try: assert checkCpuTime() except: raise SkipTest total = int(1e6) with closing(MockIO()) as our_file: a = 0 with relative_timer() as time_tqdm: for i in trange(total, file=our_file): a += i assert(a == (total * total - total) / 2.0) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) # Compute relative overhead of tqdm against native range() if time_tqdm() > 9 * time_bench(): raise AssertionError('trange(%g): %f, range(%g): %f' % (total, time_tqdm(), total, time_bench()))
def test_manual_basic_overhead(): """Test overhead of manual tqdm""" total = int(1e6) with closing(MockIO()) as our_file: with tqdm(total=total * 10, file=our_file, leave=True) as t: a = 0 with relative_timer() as time_tqdm: for i in _range(total): a += i t.update(10) a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) assert_performance(5, 'tqdm', time_tqdm(), 'range', time_bench())
def test_exceptions(): """Test CLI Exceptions""" _SYS = sys.stdin, sys.argv sys.stdin = map(str, _range(123)) sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main(fp=NULL) except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main(fp=NULL) except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') sys.argv = ['', '--update', '--update_to'] try: main(fp=NULL) except TqdmKeyError as e: if 'Can only have one of --' not in str(e): raise else: raise TqdmKeyError('Cannot have both --update --update_to') # test SystemExits for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main(fp=NULL) except SystemExit: pass else: raise ValueError('expected SystemExit') # clean up sys.stdin, sys.argv = _SYS
def test_iter_basic_overhead(): """Test overhead of iteration based tqdm""" total = int(1e6) with closing(MockIO()) as our_file: a = 0 with trange(total, file=our_file) as t: with relative_timer() as time_tqdm: for i in t: a += i assert a == (total * total - total) / 2.0 a = 0 with relative_timer() as time_bench: for i in _range(total): a += i our_file.write(a) assert_performance(3, 'trange', time_tqdm(), 'range', time_bench())
def test_main(): """ Test command line pipes """ ls_out = _sh('ls') ls = subprocess.Popen(('ls'), stdout=subprocess.PIPE) res = _sh('python', '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert (ls_out in res) # semi-fake test which gets coverage: try: _SYS = (deepcopy(sys.stdin), deepcopy(sys.argv)) except: pass sys.stdin = map(str, _range(int(1e3))) sys.argv = [ '', '--desc', 'Test command line pipes', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA sys.argv = ['', '--bad', 'arg', '--ascii', 'True', '--unit_scale', 'True'] try: main() except DocoptExit as e: if """Usage: tqdm [--help | options]""" not in str(e): raise try: sys.stdin, sys.argv = _SYS except: pass
def test_main(): """ Test command line pipes """ ls_out = subprocess.Popen(('ls'), stdout=subprocess.PIPE).communicate()[0] ls = subprocess.Popen(('ls'), stdout=subprocess.PIPE) res = subprocess.Popen(('python', '-c', 'from tqdm import main; main()'), stdout=subprocess.PIPE, stdin=ls.stdout, stderr=subprocess.STDOUT).communicate()[0] ls.wait() # actual test: assert (ls_out in res) # semi-fake test which gets coverage: try: _SYS = (deepcopy(sys.stdin), deepcopy(sys.argv)) except: pass sys.stdin = map(str, _range(int(1e3))) sys.argv = ['', '--desc', 'Test command line pipes', '--ascii', 'True', '--unit_scale', 'True'] import tqdm.__main__ # NOQA sys.argv = ['', '--bad', 'arg', '--ascii', 'True', '--unit_scale', 'True'] try: main() except DocoptExit as e: if """Usage: tqdm [--help | options]""" not in str(e): raise try: sys.stdin, sys.argv = _SYS except: pass
def test_main(): """ Test command line pipes """ ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen(('ls'), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh('python', '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert (ls_out in res.replace('\r\n', '\n')) # semi-fake test which gets coverage: try: _SYS = (deepcopy(sys.stdin), deepcopy(sys.argv)) except: pass with closing(UnicodeIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI delims', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write('\0'.join(map(str, _range(int(1e3))))) sys.stdin.seek(0) main() sys.stdin = map(str, _range(int(1e3))) sys.argv = [ '', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA sys.argv = [ '', '-ascii', '--unit_scale', 'False', '--desc', 'Test CLI errors' ] main() sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main() except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main() except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main() except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main() except SystemExit: pass # clean up try: sys.stdin, sys.argv = _SYS except: pass
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = ['', '--desc', 'Test CLI-delims', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64'] sys.stdin.write('\0'.join(map(str, _range(int(1e3))))) sys.stdin.seek(0) main() IN_DATA_LIST = map(str, _range(int(1e3))) sys.stdin = IN_DATA_LIST sys.argv = ['', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True'] import tqdm.__main__ # NOQA IN_DATA = '\0'.join(IN_DATA_LIST) with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST sys.argv = ['', '-ascii', '--unit_scale', 'False', '--desc', 'Test CLI errors'] main() sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main() except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main() except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main() except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') # test SystemExits for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main() except SystemExit: pass # test --manpath tmp = mkdtemp() man = path.join(tmp, "tqdm.1") assert not path.exists(man) try: main(argv=['--manpath', tmp]) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) rmtree(tmp, True) # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(1e3))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG']) # assert "DEBUG:" in sys.stdout.getvalue() # clean up sys.stdin, sys.argv = _SYS
def _sh(*cmd, **kwargs): return subprocess.Popen(cmd, stdout=subprocess.PIPE, **kwargs).communicate()[0].decode('utf-8') class Null(object): def __call__(self, *_, **__): return self def __getattr__(self, _): return self IN_DATA_LIST = map(str, _range(int(123))) NULL = Null() # WARNING: this should be the last test as it messes with sys.stdin, argv @with_setup(pretest, posttest) def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test:
def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace('\r\n', '\n') # semi-fake test which gets coverage: _SYS = sys.stdin, sys.argv with closing(StringIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI-delims', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write('\0'.join(map(str, _range(int(1e3))))) sys.stdin.seek(0) main() IN_DATA_LIST = map(str, _range(int(1e3))) sys.stdin = IN_DATA_LIST sys.argv = [ '', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA IN_DATA = '\0'.join(IN_DATA_LIST) with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() sys.stdin = IN_DATA_LIST sys.argv = [ '', '-ascii', '--unit_scale', 'False', '--desc', 'Test CLI errors' ] main() sys.argv = ['', '-ascii', '-unit_scale', '--bad_arg_u_ment', 'foo'] try: main() except TqdmKeyError as e: if 'bad_arg_u_ment' not in str(e): raise else: raise TqdmKeyError('bad_arg_u_ment') sys.argv = ['', '-ascii', '-unit_scale', 'invalid_bool_value'] try: main() except TqdmTypeError as e: if 'invalid_bool_value' not in str(e): raise else: raise TqdmTypeError('invalid_bool_value') sys.argv = ['', '-ascii', '--total', 'invalid_int_value'] try: main() except TqdmTypeError as e: if 'invalid_int_value' not in str(e): raise else: raise TqdmTypeError('invalid_int_value') # test SystemExits for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: main() except SystemExit: pass # test --manpath tmp = mkdtemp() man = path.join(tmp, "tqdm.1") assert not path.exists(man) try: main(argv=['--manpath', tmp]) except SystemExit: pass else: raise SystemExit("Expected system exit") assert path.exists(man) rmtree(tmp, True) # test --log with closing(StringIO()) as sys.stdin: sys.stdin.write('\0'.join(map(str, _range(int(1e3))))) sys.stdin.seek(0) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG']) # assert "DEBUG:" in sys.stdout.getvalue() # clean up sys.stdin, sys.argv = _SYS
def test_main(): """ Test command line pipes """ ls_out = _sh("ls").replace("\r\n", "\n") ls = subprocess.Popen(("ls"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, "-c", "from tqdm import main; main()", stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: assert ls_out in res.replace("\r\n", "\n") # semi-fake test which gets coverage: try: _SYS = (deepcopy(sys.stdin), deepcopy(sys.argv)) except: pass with closing(UnicodeIO()) as sys.stdin: sys.argv = ["", "--desc", "Test CLI delims", "--ascii", "True", "--delim", r"\0", "--buf_size", "64"] sys.stdin.write("\0".join(map(str, _range(int(1e3))))) sys.stdin.seek(0) main() sys.stdin = map(str, _range(int(1e3))) sys.argv = ["", "--desc", "Test CLI pipes", "--ascii", "True", "--unit_scale", "True"] import tqdm.__main__ # NOQA sys.argv = ["", "-ascii", "--unit_scale", "False", "--desc", "Test CLI errors"] main() sys.argv = ["", "-ascii", "-unit_scale", "--bad_arg_u_ment", "foo"] try: main() except TqdmKeyError as e: if "bad_arg_u_ment" not in str(e): raise else: raise TqdmKeyError("bad_arg_u_ment") sys.argv = ["", "-ascii", "-unit_scale", "invalid_bool_value"] try: main() except TqdmTypeError as e: if "invalid_bool_value" not in str(e): raise else: raise TqdmTypeError("invalid_bool_value") sys.argv = ["", "-ascii", "--total", "invalid_int_value"] try: main() except TqdmTypeError as e: if "invalid_int_value" not in str(e): raise else: raise TqdmTypeError("invalid_int_value") for i in ("-h", "--help", "-v", "--version"): sys.argv = ["", i] try: main() except SystemExit: pass # clean up try: sys.stdin, sys.argv = _SYS except: pass
def test_main(): """Test misc CLI options""" _SYS = sys.stdin, sys.argv N = 123 # test direct import sys.stdin = map(str, _range(N)) sys.argv = [ '', '--desc', 'Test CLI import', '--ascii', 'True', '--unit_scale', 'True' ] import tqdm.__main__ # NOQA sys.stderr.write("Test misc CLI options ... ") # test --delim IN_DATA = '\0'.join(map(str, _range(N))) with closing(StringIO()) as sys.stdin: sys.argv = [ '', '--desc', 'Test CLI delim', '--ascii', 'True', '--delim', r'\0', '--buf_size', '64' ] sys.stdin.write(IN_DATA) # sys.stdin.write(b'\xff') # TODO sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(fp=fp) assert str(N) + "it" in fp.getvalue() # test --bytes IN_DATA = IN_DATA.replace('\0', '\n') with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) sys.argv = ['', '--ascii', '--bytes=True', '--unit_scale', 'False'] with closing(UnicodeIO()) as fp: main(fp=fp) assert str(len(IN_DATA)) in fp.getvalue() # test --log sys.stdin = map(str, _range(N)) # with closing(UnicodeIO()) as fp: main(argv=['--log', 'DEBUG'], fp=NULL) # assert "DEBUG:" in sys.stdout.getvalue() # test --tee with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--mininterval', '0', '--miniters', '1'], fp=fp) res = len(fp.getvalue()) # assert len(fp.getvalue()) < len(sys.stdout.getvalue()) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--tee', '--mininterval', '0', '--miniters', '1'], fp=fp) # spaces to clear intermediate lines could increase length assert len(fp.getvalue()) >= res + len(IN_DATA) # test --null _STDOUT = sys.stdout try: with closing(StringIO()) as sys.stdout: with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--null'], fp=fp) assert not sys.stdout.getvalue() with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=[], fp=fp) assert sys.stdout.getvalue() except: sys.stdout = _STDOUT raise else: sys.stdout = _STDOUT # test integer --update with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update'], fp=fp) res = fp.getvalue() assert str(N // 2 * N) + "it" in res # arithmetic sum formula # test integer --update --delim with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA.replace('\n', 'D')) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update', '--delim', 'D'], fp=fp) res = fp.getvalue() assert str(N // 2 * N) + "it" in res # arithmetic sum formula # test integer --update_to with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to'], fp=fp) res = fp.getvalue() assert str(N - 1) + "it" in res assert str(N) + "it" not in res # test integer --update_to --delim with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA.replace('\n', 'D')) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to', '--delim', 'D'], fp=fp) res = fp.getvalue() assert str(N - 1) + "it" in res assert str(N) + "it" not in res # test float --update_to IN_DATA = '\n'.join((str(i / 2.0) for i in _range(N))) with closing(StringIO()) as sys.stdin: sys.stdin.write(IN_DATA) sys.stdin.seek(0) with closing(UnicodeIO()) as fp: main(argv=['--update-to'], fp=fp) res = fp.getvalue() assert str((N - 1) / 2.0) + "it" in res assert str(N / 2.0) + "it" not in res # clean up sys.stdin, sys.argv = _SYS
def _sh(*cmd, **kwargs): return subprocess.Popen(cmd, stdout=subprocess.PIPE, **kwargs).communicate()[0].decode('utf-8') class Null(object): def __call__(self, *_, **__): return self def __getattr__(self, _): return self IN_DATA_LIST = map(str, _range(int(123))) NULL = Null() # WARNING: this should be the last test as it messes with sys.stdin, argv @with_setup(pretest, posttest) def test_main(): """Test command line pipes""" ls_out = _sh('ls').replace('\r\n', '\n') ls = subprocess.Popen('ls', stdout=subprocess.PIPE, stderr=subprocess.STDOUT) res = _sh(sys.executable, '-c', 'from tqdm.cli import main; main()', stdin=ls.stdout, stderr=subprocess.STDOUT) ls.wait() # actual test: