/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-11 08:44:59 UTC
  • mfrom: (5221 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511084459-pb0uinna9zs3wu59
Merge trunk - resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
"""Black-box tests for bzr whoami."""
 
19
 
 
20
import os
 
21
 
 
22
import bzrlib
 
23
from bzrlib import osutils
 
24
from bzrlib.branch import Branch
 
25
from bzrlib.tests.blackbox import ExternalBase
 
26
 
 
27
 
 
28
class TestWhoami(ExternalBase):
 
29
 
 
30
    def test_whoami(self):
 
31
        # this should always identify something, if only "john@localhost"
 
32
        out = self.run_bzr("whoami")[0]
 
33
        self.assertTrue(len(out) > 0)
 
34
        self.assertEquals(1, out.count('@'))
 
35
 
 
36
        out = self.run_bzr("whoami --email")[0]
 
37
        self.assertTrue(len(out) > 0)
 
38
        self.assertEquals(1, out.count('@'))
 
39
 
 
40
    def test_whoami_branch(self):
 
41
        """branch specific user identity works."""
 
42
        wt = self.make_branch_and_tree('.')
 
43
        b = bzrlib.branch.Branch.open('.')
 
44
        b.get_config().set_user_option('email',
 
45
                                       'Branch Identity <branch@identi.ty>')
 
46
        bzr_email = os.environ.get('BZR_EMAIL')
 
47
        if bzr_email is not None:
 
48
            del os.environ['BZR_EMAIL']
 
49
        try:
 
50
            whoami = self.run_bzr("whoami")[0]
 
51
            self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
 
52
            whoami_email = self.run_bzr("whoami --email")[0]
 
53
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
54
 
 
55
            # Verify that the environment variable overrides the value
 
56
            # in the file
 
57
            os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
 
58
            whoami = self.run_bzr("whoami")[0]
 
59
            self.assertEquals('Different ID <other@environ.ment>\n', whoami)
 
60
            whoami_email = self.run_bzr("whoami --email")[0]
 
61
            self.assertEquals('other@environ.ment\n', whoami_email)
 
62
            del os.environ['BZR_EMAIL']
 
63
        finally:
 
64
            if bzr_email is not None:
 
65
                os.environ['BZR_EMAIL'] = bzr_email
 
66
 
 
67
    def test_whoami_utf8(self):
 
68
        """verify that an identity can be in utf-8."""
 
69
        wt = self.make_branch_and_tree('.')
 
70
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
 
71
                     encoding='utf-8')
 
72
        bzr_email = os.environ.get('BZR_EMAIL')
 
73
        if bzr_email is not None:
 
74
            del os.environ['BZR_EMAIL']
 
75
        try:
 
76
            whoami = self.run_bzr("whoami", encoding='utf-8')[0]
 
77
            self.assertEquals('Branch Identity \xe2\x82\xac ' +
 
78
                              '<branch@identi.ty>\n', whoami)
 
79
            whoami_email = self.run_bzr("whoami --email",
 
80
                                        encoding='utf-8')[0]
 
81
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
82
        finally:
 
83
            if bzr_email is not None:
 
84
                os.environ['BZR_EMAIL'] = bzr_email
 
85
 
 
86
    def test_whoami_ascii(self):
 
87
        """
 
88
        verify that whoami doesn't totally break when in utf-8, using an ascii
 
89
        encoding.
 
90
        """
 
91
        wt = self.make_branch_and_tree('.')
 
92
        b = bzrlib.branch.Branch.open('.')
 
93
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
 
94
                                       '<branch@identi.ty>')
 
95
        bzr_email = os.environ.get('BZR_EMAIL')
 
96
        if bzr_email is not None:
 
97
            del os.environ['BZR_EMAIL']
 
98
        try:
 
99
            whoami = self.run_bzr("whoami", encoding='ascii')[0]
 
100
            self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
 
101
            whoami_email = self.run_bzr("whoami --email",
 
102
                                        encoding='ascii')[0]
 
103
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
104
        finally:
 
105
            if bzr_email is not None:
 
106
                os.environ['BZR_EMAIL'] = bzr_email
 
107
 
 
108
    def test_warning(self):
 
109
        """verify that a warning is displayed if no email is given."""
 
110
        self.make_branch_and_tree('.')
 
111
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
 
112
        self.assertEquals('"Branch Identity" does not seem to contain an '
 
113
                          'email address.  This is allowed, but not '
 
114
                          'recommended.\n', display)
 
115
 
 
116
    def test_whoami_not_set(self):
 
117
        """Ensure whoami error if username is not set.
 
118
        """
 
119
        osutils.set_or_unset_env('EMAIL', None)
 
120
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
121
        out, err = self.run_bzr(['whoami'], 3)
 
122
        self.assertContainsRe(err, 'Unable to determine your name')