def test_text_ui_get_boolean(self): stdin = tests.StringIOWrapper("y\n" # True "n\n" # False " \n y \n" # True " no \n" # False "yes with garbage\nY\n" # True "not an answer\nno\n" # False "I'm sure!\nyes\n" # True "NO\n" # False "foo\n") stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) self.assertEqual(True, factory.get_boolean(u"")) self.assertEqual(False, factory.get_boolean(u"")) self.assertEqual(True, factory.get_boolean(u"")) self.assertEqual(False, factory.get_boolean(u"")) self.assertEqual(True, factory.get_boolean(u"")) self.assertEqual(False, factory.get_boolean(u"")) self.assertEqual(True, factory.get_boolean(u"")) self.assertEqual(False, factory.get_boolean(u"")) self.assertEqual("foo\n", factory.stdin.read()) # stdin should be empty self.assertEqual('', factory.stdin.readline()) # return false on EOF self.assertEqual(False, factory.get_boolean(u""))
def test_text_ui_choose_return_values(self): choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3) stdin = tests.StringIOWrapper("y\n" # 0 "n\n" # 1 " \n" # default: 3 " no \n" # 1 "b\na\nd \n" # bad shortcuts, all ignored "yes with garbage\nY\n" # 0 "not an answer\nno\n" # 1 "info\nmore info\n" # 3 "Maybe\n" # 2 "foo\n") stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) self.assertEqual(0, choose()) self.assertEqual(1, choose()) self.assertEqual(3, choose()) self.assertEqual(1, choose()) self.assertEqual(0, choose()) self.assertEqual(1, choose()) self.assertEqual(3, choose()) self.assertEqual(2, choose()) self.assertEqual("foo\n", factory.stdin.read()) # stdin should be empty self.assertEqual('', factory.stdin.readline()) # return None on EOF self.assertEqual(None, choose())
def test_output_encoding_configuration(self): enc = fixtures.generate_unicode_encodings().next() config.GlobalStack().set('output_encoding', enc) ui = tests.TestUIFactory(stdin=None, stdout=tests.StringIOWrapper(), stderr=tests.StringIOWrapper()) output = ui.make_output_stream() self.assertEqual(output.encoding, enc)
def test_text_ui_choose_no_default(self): stdin = tests.StringIOWrapper(" \n" # no default, invalid! " yes \n" # 0 "foo\n") stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) self.assertEqual(0, factory.choose(u"", u"&Yes\n&No")) self.assertEqual("foo\n", factory.stdin.read())
def test_text_tick_after_update(self): ui_factory = _mod_ui_text.TextUIFactory(stdout=tests.StringIOWrapper(), stderr=tests.StringIOWrapper()) pb = ui_factory.nested_progress_bar() try: pb.update('task', 0, 3) # Reset the clock, so that it actually tries to repaint itself ui_factory._progress_view._last_repaint = time.time() - 1.0 pb.tick() finally: pb.finished()
def test_text_ui_get_integer(self): stdin = tests.StringIOWrapper( "1\n" " -2 \n" "hmmm\nwhat else ?\nCome on\nok 42\n4.24\n42\n") stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) self.assertEqual(1, factory.get_integer(u"")) self.assertEqual(-2, factory.get_integer(u"")) self.assertEqual(42, factory.get_integer(u""))
def test_text_ui_getusername_utf8(self): ui = _mod_ui_text.TextUIFactory(None, None, None) ui.stdin = tests.StringIOWrapper(u'someuser\u1234'.encode('utf8')) ui.stdout = tests.StringIOWrapper() ui.stderr = tests.StringIOWrapper() ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = "utf8" username = ui.get_username(u'Hello %(host)s', host=u'some\u1234') self.assertEqual(u"someuser\u1234", username) self.assertEqual(u"Hello some\u1234: ", ui.stderr.getvalue().decode("utf8")) self.assertEqual('', ui.stdout.getvalue())
def test_text_ui_choose_bad_parameters(self): stdin = tests.StringIOWrapper() stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) # invalid default index self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3) # duplicated choice self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe") # duplicated shortcut self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
def test_text_ui_choose_prompt(self): stdin = tests.StringIOWrapper() stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr) # choices with explicit shortcuts factory.choose(u"prompt", u"&yes\n&No\nmore &info") self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue()) # automatic shortcuts factory.stderr.truncate(0) factory.choose(u"prompt", u"yes\nNo\nmore info") self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
def test_text_ui_getusername(self): ui = _mod_ui_text.TextUIFactory(None, None, None) ui.stdin = tests.StringIOWrapper('someuser\n\n') ui.stdout = tests.StringIOWrapper() ui.stderr = tests.StringIOWrapper() ui.stdout.encoding = 'utf8' self.assertEqual('someuser', ui.get_username(u'Hello %(host)s', host='some')) self.assertEqual('Hello some: ', ui.stderr.getvalue()) self.assertEqual('', ui.stdout.getvalue()) self.assertEqual('', ui.get_username(u"Gebruiker")) # stdin should be empty self.assertEqual('', ui.stdin.readline())
def test_text_factory_utf8_password(self): """Test an utf8 password.""" ui = _mod_ui_text.TextUIFactory(None, None, None) ui.stdin = tests.StringIOWrapper(u'baz\u1234'.encode('utf8')) ui.stdout = tests.StringIOWrapper() ui.stderr = tests.StringIOWrapper() ui.stderr.encoding = ui.stdout.encoding = ui.stdin.encoding = 'utf8' password = ui.get_password(u'Hello \u1234 %(user)s', user=u'some\u1234') self.assertEqual(u'baz\u1234', password) self.assertEqual(u'Hello \u1234 some\u1234: ', ui.stderr.getvalue().decode('utf8')) # stdin and stdout should be empty self.assertEqual('', ui.stdin.readline()) self.assertEqual('', ui.stdout.getvalue())
def test_render_progress_unicode_enc_none(self): out = tests.StringIOWrapper() out.encoding = None view = self.make_view_only(out, 20) task = self.make_task(None, view, u"\xa7", 0, 1) view.show_progress(task) self.assertEqual('\r/ ? 0/1 \r', out.getvalue())
def test_recommend_upgrade_current_format(self): stderr = tests.StringIOWrapper() ui.ui_factory = tests.TestUIFactory(stderr=stderr) format = controldir.ControlComponentFormat() format.check_support_status(allow_unsupported=False, recommend_upgrade=True) self.assertEqual("", stderr.getvalue())
def test_silent_ui_getbool(self): factory = _mod_ui.SilentUIFactory() stdout = tests.StringIOWrapper() self.assertRaises( NotImplementedError, self.apply_redirected, None, stdout, stdout, factory.get_boolean, u"foo")
def test_text_factory_prompts_and_clears(self): # a get_boolean call should clear the pb before prompting out = TTYStringIO() self.overrideEnv('TERM', 'xterm') factory = _mod_ui_text.TextUIFactory( stdin=tests.StringIOWrapper("yada\ny\n"), stdout=out, stderr=out) factory._avail_width = lambda: 79 pb = factory.nested_progress_bar() pb.show_bar = False pb.show_spinner = False pb.show_count = False pb.update("foo", 0, 1) self.assertEqual(True, self.apply_redirected(None, factory.stdout, factory.stdout, factory.get_boolean, u"what do you want")) output = out.getvalue() self.assertContainsRe(output, "| foo *\r\r *\r*") self.assertContainsString(output, r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ") # stdin should have been totally consumed self.assertEqual('', factory.stdin.readline())
def test_gather_user_credentials_prompts(self): service = LaunchpadService() self.assertIs(None, service.registrant_password) g_conf = config.GlobalStack() g_conf.set('email', 'Test User <*****@*****.**>') g_conf.store.save() stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() ui.ui_factory = tests.TestUIFactory(stdin='userpass\n', stdout=stdout, stderr=stderr) self.assertIs(None, service.registrant_password) service.gather_user_credentials() self.assertEqual('*****@*****.**', service.registrant_email) self.assertEqual('userpass', service.registrant_password) self.assertEquals('', stdout.getvalue()) self.assertContainsRe(stderr.getvalue(), 'launchpad.net password for test@user\\.com')
def test_sftp_doesnt_prompt_username(self): stdout = tests.StringIOWrapper() ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n', stdout=stdout) t = self.get_transport_for_connection(set_config=False) self.assertIs(None, t._get_credentials()[0]) # No prompts should've been printed, stdin shouldn't have been read self.assertEqual("", stdout.getvalue()) self.assertEqual(0, ui.ui_factory.stdin.tell())
def test_annotate_unicode_author(self): tree1 = self.make_branch_and_tree('tree1') self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')]) tree1.add(['a'], ['a-id']) tree1.commit('a', rev_id='rev-1', committer=u'Pepe P\xe9rez <*****@*****.**>', timestamp=1166046000.00, timezone=0) self.build_tree_contents([('tree1/b', 'bye')]) tree1.add(['b'], ['b-id']) tree1.commit('b', rev_id='rev-2', committer=u'p\xe9rez', timestamp=1166046000.00, timezone=0) tree1.lock_read() self.addCleanup(tree1.unlock) revtree_1 = tree1.branch.repository.revision_tree('rev-1') revtree_2 = tree1.branch.repository.revision_tree('rev-2') # this passes if no exception is raised to_file = StringIO() annotate.annotate_file_tree(revtree_1, 'a-id', to_file=to_file, branch=tree1.branch) sio = StringIO() to_file = codecs.getwriter('ascii')(sio) to_file.encoding = 'ascii' # codecs does not set it annotate.annotate_file_tree(revtree_2, 'b-id', to_file=to_file, branch=tree1.branch) self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue()) # test now with to_file.encoding = None to_file = tests.StringIOWrapper() to_file.encoding = None annotate.annotate_file_tree(revtree_2, 'b-id', to_file=to_file, branch=tree1.branch) self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue()) # and when it does not exist to_file = StringIO() annotate.annotate_file_tree(revtree_2, 'b-id', to_file=to_file, branch=tree1.branch) self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
def test_push(self): self.make_branch_and_tree('branch') self.start_logging_connections() cmd = cmd_push() # We don't care about the ouput but 'outf' should be defined cmd.outf = tests.StringIOWrapper() cmd.run(self.get_url('remote'), directory='branch') self.assertEqual(1, len(self.connections))
def test_push_onto_stacked(self): self.make_branch_and_tree('base', format='1.9') self.make_branch_and_tree('source', format='1.9') self.start_logging_connections() cmd = cmd_push() cmd.outf = tests.StringIOWrapper() cmd.run(self.get_url('remote'), directory='source', stacked_on=self.get_url('base')) self.assertEqual(1, len(self.connections))
def test_silent_factory_get_password(self): # A silent factory that can't do user interaction can't get a # password. Possibly it should raise a more specific error but it # can't succeed. ui = _mod_ui.SilentUIFactory() stdout = tests.StringIOWrapper() self.assertRaises( NotImplementedError, self.apply_redirected, None, stdout, stdout, ui.get_password) # and it didn't write anything out either self.assertEqual('', stdout.getvalue())
def test_output_clears_terminal(self): stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() clear_calls = [] uif = _mod_ui_text.TextUIFactory(None, stdout, stderr) uif.clear_term = lambda: clear_calls.append('clear') stream = _mod_ui_text.TextUIOutputStream(uif, uif.stdout) stream.write("Hello world!\n") stream.write("there's more...\n") stream.writelines(["1\n", "2\n", "3\n"]) self.assertEqual(stdout.getvalue(), "Hello world!\n" "there's more...\n" "1\n2\n3\n") self.assertEqual(['clear', 'clear', 'clear'], clear_calls) stream.flush()
def test_empty_branch_command(self): """The 'bzr push' command should make a limited number of HPSS calls. """ cmd = builtins.cmd_push() cmd.outf = tests.StringIOWrapper() cmd.run(directory=self.get_url('empty'), location=self.smart_server.get_url() + 'target') # HPSS calls as of 2008/09/22: # [BzrDir.open, BzrDir.open_branch, BzrDir.find_repositoryV2, # Branch.get_stacked_on_url, get, get, Branch.lock_write, # Branch.last_revision_info, Branch.unlock] self.assertTrue(len(self.hpss_calls) <= 9, self.hpss_calls)
def test_pull(self): wt1 = self.make_branch_and_tree('branch1') tip = wt1.commit('empty commit') wt2 = self.make_branch_and_tree('branch2') self.start_logging_connections() cmd = builtins.cmd_pull() # We don't care about the ouput but 'outf' should be defined cmd.outf = tests.StringIOWrapper() cmd.run(self.get_url('branch1'), directory='branch2') self.assertEqual(1, len(self.connections))
def test_smtp_password_from_user(self): user = '******' password = '******' factory = WideOpenSMTPFactory() conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user, smtp_factory=factory) self.assertIs(None, conn._smtp_password) ui.ui_factory = tests.TestUIFactory(stdin=password + '\n', stdout=tests.StringIOWrapper()) conn._connect() self.assertEqual(password, conn._smtp_password) # stdin should be empty (the provided password have been consumed) self.assertEqual('', ui.ui_factory.stdin.readline())
def test_recommend_upgrade_old_format(self): stderr = tests.StringIOWrapper() ui.ui_factory = tests.TestUIFactory(stderr=stderr) format = OldControlComponentFormat() format.check_support_status(allow_unsupported=False, recommend_upgrade=False) self.assertEqual("", stderr.getvalue()) format.check_support_status(allow_unsupported=False, recommend_upgrade=True, basedir='apath') self.assertEqual( 'An old format that is slow is deprecated and a better format ' 'is available.\nIt is recommended that you upgrade by running ' 'the command\n bzr upgrade apath\n', stderr.getvalue())
def test_prompt_for_password(self): t = self.get_transport() # Ensure that the test framework set the password self.assertIsNot(t._password, None) # Reset the password (get_url set the password to 'bar' so we # reset it to None in the transport before the connection). password = t._password t._password = None ui.ui_factory = tests.TestUIFactory(stdin=password+'\n', stdout=tests.StringIOWrapper()) # Ask the server to check the password self._add_authorized_user(t._user, password) # Issue a request to the server to connect t.has('whatever/not/existing') # stdin should be empty (the provided password have been consumed) self.assertEqual('', ui.ui_factory.stdin.readline())
def test_update(self): remote_wt = self.make_branch_and_tree('remote') local_wt = self.make_branch_and_tree('local') remote_branch = branch.Branch.open(self.get_url('remote')) local_wt.branch.bind(remote_branch) remote_wt.commit('empty commit') self.start_logging_connections() update = builtins.cmd_update() # update needs the encoding from outf to print URLs update.outf = tests.StringIOWrapper() # update calls it 'dir' where other commands calls it 'directory' update.run(dir='local') self.assertEqual(1, len(self.connections))
def test_pull_with_bound_branch(self): master_wt = self.make_branch_and_tree('master') local_wt = self.make_branch_and_tree('local') master_branch = branch.Branch.open(self.get_url('master')) local_wt.branch.bind(master_branch) remote_wt = self.make_branch_and_tree('remote') remote_wt.commit('empty commit') self.start_logging_connections() pull = builtins.cmd_pull() # We don't care about the ouput but 'outf' should be defined pull.outf = tests.StringIOWrapper() pull.run(self.get_url('remote'), directory='local') self.assertEqual(2, len(self.connections))
def test_no_prompt_for_password_when_using_auth_config(self): t = self.get_transport() # Reset the password (get_url set the password to 'bar' so we # reset it to None in the transport before the connection). password = t._password t._password = None ui.ui_factory = tests.TestUIFactory(stdin='precious\n', stdout=tests.StringIOWrapper()) # Ask the server to check the password self._add_authorized_user(t._user, password) # Create a config file with the right password conf = config.AuthenticationConfig() conf._get_config().update({'ftptest': {'scheme': 'ftp', 'user': t._user, 'password': password}}) conf._save() # Issue a request to the server to connect t.put_bytes('foo', 'test bytes\n') self.assertEqual('test bytes\n', t.get_bytes('foo')) # stdin should have been left untouched self.assertEqual('precious\n', ui.ui_factory.stdin.readline())