/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_whoami.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for brz whoami."""
19
 
 
20
 
from breezy import (
21
 
    branch,
22
 
    config,
23
 
    errors,
24
 
    tests,
25
 
    )
26
 
from breezy.sixish import PY3
27
 
 
28
 
from ..test_bedding import override_whoami
29
 
 
30
 
 
31
 
class TestWhoami(tests.TestCaseWithTransport):
32
 
 
33
 
    def assertWhoAmI(self, expected, *cmd_args, **kwargs):
34
 
        out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
35
 
        self.assertEqual('', err)
36
 
        lines = out.splitlines()
37
 
        self.assertLength(1, lines)
38
 
        if PY3 and isinstance(expected, bytes):
39
 
            expected = expected.decode(kwargs.get('encoding', 'ascii'))
40
 
        self.assertEqual(expected, lines[0].rstrip())
41
 
 
42
 
    def test_whoami_no_args_no_conf(self):
 
18
"""Black-box tests for bzr whoami."""
 
19
 
 
20
import os
 
21
 
 
22
import bzrlib
 
23
from bzrlib.branch import Branch
 
24
from bzrlib.tests.blackbox import ExternalBase
 
25
 
 
26
 
 
27
class TestWhoami(ExternalBase):
 
28
 
 
29
    def test_whoami(self):
43
30
        # this should always identify something, if only "john@localhost"
44
31
        out = self.run_bzr("whoami")[0]
45
32
        self.assertTrue(len(out) > 0)
46
 
        self.assertEqual(1, out.count('@'))
 
33
        self.assertEquals(1, out.count('@'))
47
34
 
48
 
    def test_whoami_email_no_args(self):
49
35
        out = self.run_bzr("whoami --email")[0]
50
36
        self.assertTrue(len(out) > 0)
51
 
        self.assertEqual(1, out.count('@'))
52
 
 
53
 
    def test_whoami_email_arg(self):
54
 
        # whoami --email is mutually exclusive with any arguments
55
 
        out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
56
 
        self.assertEqual("", out)
57
 
 
58
 
    def set_branch_email(self, b, email):
59
 
        b.get_config_stack().set('email', email)
 
37
        self.assertEquals(1, out.count('@'))
60
38
 
61
39
    def test_whoami_branch(self):
62
40
        """branch specific user identity works."""
63
41
        wt = self.make_branch_and_tree('.')
64
 
        b = branch.Branch.open('.')
65
 
        self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
66
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
67
 
        self.assertWhoAmI('branch@identi.ty', '--email')
 
42
        b = bzrlib.branch.Branch.open('.')
 
43
        b.get_config().set_user_option('email',
 
44
                                       'Branch Identity <branch@identi.ty>')
 
45
        bzr_email = os.environ.get('BZR_EMAIL')
 
46
        if bzr_email is not None:
 
47
            del os.environ['BZR_EMAIL']
 
48
        try:
 
49
            whoami = self.run_bzr("whoami")[0]
 
50
            self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
 
51
            whoami_email = self.run_bzr("whoami --email")[0]
 
52
            self.assertEquals('branch@identi.ty\n', whoami_email)
68
53
 
69
 
        # Verify that the environment variable overrides the value
70
 
        # in the file
71
 
        self.overrideEnv('BRZ_EMAIL', 'Different ID <other@environ.ment>')
72
 
        self.assertWhoAmI('Different ID <other@environ.ment>')
73
 
        self.assertWhoAmI('other@environ.ment', '--email')
 
54
            # Verify that the environment variable overrides the value
 
55
            # in the file
 
56
            os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
 
57
            whoami = self.run_bzr("whoami")[0]
 
58
            self.assertEquals('Different ID <other@environ.ment>\n', whoami)
 
59
            whoami_email = self.run_bzr("whoami --email")[0]
 
60
            self.assertEquals('other@environ.ment\n', whoami_email)
 
61
            del os.environ['BZR_EMAIL']
 
62
        finally:
 
63
            if bzr_email is not None:
 
64
                os.environ['BZR_EMAIL'] = bzr_email
74
65
 
75
66
    def test_whoami_utf8(self):
76
67
        """verify that an identity can be in utf-8."""
 
68
        wt = self.make_branch_and_tree('.')
77
69
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
78
70
                     encoding='utf-8')
79
 
        self.assertWhoAmI(b'Branch Identity \xe2\x82\xac <branch@identi.ty>',
80
 
                          encoding='utf-8')
81
 
        self.assertWhoAmI('branch@identi.ty', '--email')
 
71
        bzr_email = os.environ.get('BZR_EMAIL')
 
72
        if bzr_email is not None:
 
73
            del os.environ['BZR_EMAIL']
 
74
        try:
 
75
            whoami = self.run_bzr("whoami", encoding='utf-8')[0]
 
76
            self.assertEquals('Branch Identity \xe2\x82\xac ' +
 
77
                              '<branch@identi.ty>\n', whoami)
 
78
            whoami_email = self.run_bzr("whoami --email",
 
79
                                        encoding='utf-8')[0]
 
80
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
81
        finally:
 
82
            if bzr_email is not None:
 
83
                os.environ['BZR_EMAIL'] = bzr_email
82
84
 
83
85
    def test_whoami_ascii(self):
84
86
        """
86
88
        encoding.
87
89
        """
88
90
        wt = self.make_branch_and_tree('.')
89
 
        b = branch.Branch.open('.')
90
 
        self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
91
 
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
92
 
                          encoding='ascii')
93
 
        self.assertWhoAmI('branch@identi.ty', '--email',
94
 
                          encoding='ascii')
 
91
        b = bzrlib.branch.Branch.open('.')
 
92
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
 
93
                                       '<branch@identi.ty>')
 
94
        bzr_email = os.environ.get('BZR_EMAIL')
 
95
        if bzr_email is not None:
 
96
            del os.environ['BZR_EMAIL']
 
97
        try:
 
98
            whoami = self.run_bzr("whoami", encoding='ascii')[0]
 
99
            self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
 
100
            whoami_email = self.run_bzr("whoami --email",
 
101
                                        encoding='ascii')[0]
 
102
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
103
        finally:
 
104
            if bzr_email is not None:
 
105
                os.environ['BZR_EMAIL'] = bzr_email
95
106
 
96
107
    def test_warning(self):
97
108
        """verify that a warning is displayed if no email is given."""
98
109
        self.make_branch_and_tree('.')
99
110
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
100
 
        self.assertEqual('"Branch Identity" does not seem to contain an '
101
 
                         'email address.  This is allowed, but not '
102
 
                         'recommended.\n', display)
103
 
 
104
 
    def test_whoami_not_set(self):
105
 
        """Ensure whoami error if username is not set and not inferred.
106
 
        """
107
 
        override_whoami(self)
108
 
        out, err = self.run_bzr(['whoami'], 3)
109
 
        self.assertContainsRe(err, 'Unable to determine your name')
110
 
 
111
 
    def test_whoami_directory(self):
112
 
        """Test --directory option."""
113
 
        wt = self.make_branch_and_tree('subdir')
114
 
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
115
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
116
 
                          '--directory', 'subdir')
117
 
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
118
 
                      'Changed Identity <changed@identi.ty>'])
119
 
        # Refresh wt as 'whoami' modified it
120
 
        wt = wt.controldir.open_workingtree()
121
 
        c = wt.branch.get_config_stack()
122
 
        self.assertEqual('Changed Identity <changed@identi.ty>',
123
 
                         c.get('email'))
124
 
 
125
 
    def test_whoami_remote_directory(self):
126
 
        """Test --directory option with a remote directory."""
127
 
        wt = self.make_branch_and_tree('subdir')
128
 
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
129
 
        url = self.get_readonly_url() + '/subdir'
130
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
131
 
                          '--directory', url)
132
 
        url = self.get_url('subdir')
133
 
        self.run_bzr(['whoami', '--directory', url, '--branch',
134
 
                      'Changed Identity <changed@identi.ty>'])
135
 
        # The identity has been set in the branch config (but not the global
136
 
        # config)
137
 
        c = branch.Branch.open(url).get_config_stack()
138
 
        self.assertEqual('Changed Identity <changed@identi.ty>',
139
 
                         c.get('email'))
140
 
        # Ensuring that the value does not come from the breezy.conf file
141
 
        # itself requires some isolation setup
142
 
        override_whoami(self)
143
 
        global_conf = config.GlobalStack()
144
 
        self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
145
 
 
146
 
    def test_whoami_nonbranch_directory(self):
147
 
        """Test --directory mentioning a non-branch directory."""
148
 
        wt = self.build_tree(['subdir/'])
149
 
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
150
 
        self.assertContainsRe(err, 'ERROR: Not a branch')
 
111
        self.assertEquals('"Branch Identity" does not seem to contain an '
 
112
                          'email address.  This is allowed, but not '
 
113
                          'recommended.\n', display)