def test_setup_with_counters(capfd): """Make sure we have 0/count when the bar is setup.""" pbar = ProgressBar(10, {"show_counters": True, "width": 25}) pbar.setup() stdout, _ = capfd.readouterr() assert stdout == "[ ] 0/10"
def test_clear(capfd): """Ensure we print whitespace over the bar and carriage return.""" pbar = ProgressBar(10, {"width": 20}) pbar.clear() stdout, _ = capfd.readouterr() assert " " in stdout
def test_empty_setup(capfd): """Ensure we create the initial empty bar correctly.""" pbar = ProgressBar(10, {"width": 20, "style": 1}) pbar.setup() stdout, _ = capfd.readouterr() assert stdout == "{ }"
def test_setup(capfd): """Ensure we are setting up the pbar correctly.""" pbar = ProgressBar(10, {"left_padding": "ok then"}) assert pbar.total == 10 pbar.setup() out, _ = capfd.readouterr() assert out.startswith("ok then")
def test_width_is_none(options): """Regression test for 4.0.2 progressbar display bug.""" width_patch = patch( "bladerunner.progressbar.get_term_width", return_value=123, ) with width_patch as patched_width: pbar = ProgressBar(10, options) assert patched_width.called assert pbar.total_width == 123
def run(self, commands=None, servers=None, commands_on_servers=None): """Executes commands on servers. Args:: commands: a list of strings of commands to run servers: a list of strings of hostnames commands_on_servers: an optional dictionary used when providing unique lists of commands per server Returns: a list of dictionaries with two keys: name, and results. results is a list of tuples of commands issued and their replies. """ if not isinstance(servers, (list, tuple)): servers = [servers] if not isinstance(commands, (list, tuple)): commands = [commands] servers = self._prep_servers(commands, servers, commands_on_servers) if self.options["progressbar"]: self.progress = ProgressBar(len(servers), self.options) self.progress.setup() if self.options["jump_host"]: jumpuser = self.options["jump_user"] or self.options["username"] (self.sshc, error_code) = self.connect( self.options["jump_host"], jumpuser, self.options["jump_pass"], self.options["jump_port"], ) if error_code < 0: message = int(math.fabs(error_code)) - 1 raise SystemExit("Jumpbox Error: {0}".format( self.errors[message])) if self.options["delay"] or self.options["jump_host"]: results = self._run_serial(servers) else: results = self._run_parallel(servers) if self.options["jump_host"]: self.close(self.sshc, True) if self.options["progressbar"]: self.progress.clear() return results
def test_update_quarters(capfd): """Ensure the progressbar updates on the quarter completion of a char.""" pbar = ProgressBar(5, {"width": 3}) pbar.setup() stdout, _ = capfd.readouterr() assert "[ ]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[/]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[-]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[-]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[\\]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[=]" in stdout
def test_update(capfd): """Verify that the progressbar is updating correctly.""" pbar = ProgressBar(4, {"width": 8}) pbar.setup() stdout, _ = capfd.readouterr() assert "[ ]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[=- ]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[=== ]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[====- ]" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "[======]" in stdout # over-updating should do nothing pbar.update() stdout, _ = capfd.readouterr() assert stdout == ""
def test_update_with_counters(capfd): """Test updating the progressbar with counters.""" pbar = ProgressBar(4, {"style": 1, "width": 12, "show_counters": True}) pbar.setup() stdout, _ = capfd.readouterr() assert "{ } 0/4" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "{*- } 1/4" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "{*** } 2/4" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "{****- } 3/4" in stdout pbar.update() stdout, _ = capfd.readouterr() assert "{******} 4/4" in stdout # over-updating should do nothing pbar.update() stdout, _ = capfd.readouterr() assert stdout == ""
def test_counters_reduce_width(updates): """When show counters is used, the total width is reduced.""" pbar = ProgressBar(updates, {"show_counters": True, "width": 40}) # reduced by 8 here, len(updates) * 2, space, slash, left and right padding assert pbar.width == 40 - ((len(str(updates)) * 2) + 4)
def test_invalid_style(style): """If an invalid style integer is passed, assume style 0.""" pbar = ProgressBar(10, {"style": style}) assert pbar.style == 0
def test_default_options(): """Ensure the configuration when no options are given.""" with patch.object(progressbar, "get_term_width", return_value=10) as p_get: ProgressBar(10) assert p_get.called