~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Aaron Bentley
  • Date: 2007-06-11 14:59:52 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070611145952-cwt4480gphdhen6l
Get installation started

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2010, 2011 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
18
"""Black-box tests for bzr whoami."""
19
19
 
 
20
import os
 
21
 
20
22
import bzrlib
21
 
from bzrlib import (
22
 
    config,
23
 
    tests,
24
 
    )
25
 
 
26
 
 
27
 
class TestWhoami(tests.TestCaseWithTransport):
28
 
 
29
 
    def assertWhoAmI(self, expected, *cmd_args, **kwargs):
30
 
        out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
31
 
        self.assertEquals('', err)
32
 
        lines = out.splitlines()
33
 
        self.assertLength(1, lines)
34
 
        self.assertEquals(expected, lines[0].rstrip())
35
 
 
36
 
    def test_whoami_no_args_no_conf(self):
 
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):
37
30
        # this should always identify something, if only "john@localhost"
38
31
        out = self.run_bzr("whoami")[0]
39
32
        self.assertTrue(len(out) > 0)
40
33
        self.assertEquals(1, out.count('@'))
41
34
 
42
 
    def test_whoami_email_no_args(self):
43
 
        out = self.run_bzr("whoami --email")[0]
 
35
        out = self.run_bzr("whoami", "--email")[0]
44
36
        self.assertTrue(len(out) > 0)
45
37
        self.assertEquals(1, out.count('@'))
46
 
 
47
 
    def test_whoami_email_arg(self):
48
 
        # whoami --email is mutually exclusive with any arguments
49
 
        out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
50
 
        self.assertEquals("", out)
51
 
 
 
38
        
52
39
    def test_whoami_branch(self):
53
40
        """branch specific user identity works."""
54
41
        wt = self.make_branch_and_tree('.')
55
42
        b = bzrlib.branch.Branch.open('.')
56
43
        b.get_config().set_user_option('email',
57
44
                                       'Branch Identity <branch@identi.ty>')
58
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
 
        self.assertWhoAmI('branch@identi.ty', '--email')
 
45
        bzr_email = os.environ.get('BZR_EMAIL')
 
46
        if bzr_email is not None:
 
47
            del os.environ['BZR_EMAIL']
 
48
        bzremail = os.environ.get('BZREMAIL')
 
49
        if bzremail is not None:
 
50
            del os.environ['BZREMAIL']
 
51
        try:
 
52
            whoami = self.run_bzr("whoami")[0]
 
53
            self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
 
54
            whoami_email = self.run_bzr("whoami", "--email")[0]
 
55
            self.assertEquals('branch@identi.ty\n', whoami_email)
60
56
 
61
 
        # Verify that the environment variable overrides the value
62
 
        # in the file
63
 
        self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
64
 
        self.assertWhoAmI('Different ID <other@environ.ment>')
65
 
        self.assertWhoAmI('other@environ.ment', '--email')
 
57
            # Verify that the environment variable overrides the value 
 
58
            # in the file
 
59
            os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
 
60
            whoami = self.run_bzr("whoami")[0]
 
61
            self.assertEquals('Different ID <other@environ.ment>\n', whoami)
 
62
            whoami_email = self.run_bzr("whoami", "--email")[0]
 
63
            self.assertEquals('other@environ.ment\n', whoami_email)
 
64
            del os.environ['BZR_EMAIL']
 
65
            os.environ['BZREMAIL'] = 'Yet Another ID <yetother@environ.ment>'
 
66
            whoami, warn = self.run_bzr("whoami")
 
67
            self.assertEquals('Yet Another ID <yetother@environ.ment>\n', whoami)
 
68
            self.assertTrue(len(warn) > 0)
 
69
            del os.environ['BZREMAIL']
 
70
        finally:
 
71
            if bzr_email is not None:
 
72
                os.environ['BZR_EMAIL'] = bzr_email
 
73
            if bzremail is not None:
 
74
                os.environ['BZREMAIL'] = bzremail
66
75
 
67
76
    def test_whoami_utf8(self):
68
77
        """verify that an identity can be in utf-8."""
69
 
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
 
78
        wt = self.make_branch_and_tree('.')
 
79
        self.run_bzr('whoami', u'Branch Identity \u20ac <branch@identi.ty>',
70
80
                     encoding='utf-8')
71
 
        self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
72
 
                          encoding='utf-8')
73
 
        self.assertWhoAmI('branch@identi.ty', '--email')
 
81
        bzr_email = os.environ.get('BZR_EMAIL')
 
82
        if bzr_email is not None:
 
83
            del os.environ['BZR_EMAIL']
 
84
        try:
 
85
            whoami = self.run_bzr("whoami", encoding='utf-8')[0]
 
86
            self.assertEquals('Branch Identity \xe2\x82\xac ' +
 
87
                              '<branch@identi.ty>\n', whoami)
 
88
            whoami_email = self.run_bzr("whoami", "--email",
 
89
                                        encoding='utf-8')[0]
 
90
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
91
        finally:
 
92
            if bzr_email is not None:
 
93
                os.environ['BZR_EMAIL'] = bzr_email
74
94
 
75
95
    def test_whoami_ascii(self):
76
96
        """
81
101
        b = bzrlib.branch.Branch.open('.')
82
102
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
103
                                       '<branch@identi.ty>')
84
 
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
85
 
                          encoding='ascii')
86
 
        self.assertWhoAmI('branch@identi.ty', '--email',
87
 
                          encoding='ascii')
 
104
        bzr_email = os.environ.get('BZR_EMAIL')
 
105
        if bzr_email is not None:
 
106
            del os.environ['BZR_EMAIL']
 
107
        try:
 
108
            whoami = self.run_bzr("whoami", encoding='ascii')[0]
 
109
            self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
 
110
            whoami_email = self.run_bzr("whoami", "--email",
 
111
                                        encoding='ascii')[0]
 
112
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
113
        finally:
 
114
            if bzr_email is not None:
 
115
                os.environ['BZR_EMAIL'] = bzr_email
88
116
 
89
117
    def test_warning(self):
90
118
        """verify that a warning is displayed if no email is given."""
91
119
        self.make_branch_and_tree('.')
92
 
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
 
120
        display = self.run_bzr('whoami', 'Branch Identity')[1]
93
121
        self.assertEquals('"Branch Identity" does not seem to contain an '
94
122
                          'email address.  This is allowed, but not '
95
123
                          'recommended.\n', display)
96
 
 
97
 
    def test_whoami_not_set(self):
98
 
        """Ensure whoami error if username is not set and not inferred.
99
 
        """
100
 
        self.overrideEnv('EMAIL', None)
101
 
        self.overrideEnv('BZR_EMAIL', None)
102
 
        # Also, make sure that it's not inferred from mailname.
103
 
        self.overrideAttr(config, '_auto_user_id',
104
 
            lambda: (None, None))
105
 
        out, err = self.run_bzr(['whoami'], 3)
106
 
        self.assertContainsRe(err, 'Unable to determine your name')
107
 
 
108
 
    def test_whoami_directory(self):
109
 
        """Test --directory option."""
110
 
        wt = self.make_branch_and_tree('subdir')
111
 
        c = wt.branch.get_config()
112
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
113
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
 
                          '--directory', 'subdir')
115
 
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
 
                      'Changed Identity <changed@identi.ty>'])
117
 
        c = wt.branch.get_config()
118
 
        self.assertEquals('Changed Identity <changed@identi.ty>',
119
 
                          c.get_user_option('email'))
120
 
 
121
 
    def test_whoami_remote_directory(self):
122
 
        """Test --directory option with a remote directory."""
123
 
        wt = self.make_branch_and_tree('subdir')
124
 
        c = wt.branch.get_config()
125
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
126
 
        url = self.get_readonly_url() + '/subdir'
127
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
128
 
                          '--directory', url)
129
 
        url = self.get_url('subdir')
130
 
        self.run_bzr(['whoami', '--directory', url, '--branch',
131
 
                      'Changed Identity <changed@identi.ty>'])
132
 
        # The identity has been set in the branch config (but not the global
133
 
        # config)
134
 
        c = wt.branch.get_config()
135
 
        self.assertEquals('Changed Identity <changed@identi.ty>',
136
 
                          c.get_user_option('email'))
137
 
        global_conf = config.GlobalConfig()
138
 
        self.assertEquals(None, global_conf.get_user_option('email'))
139
 
 
140
 
    def test_whoami_nonbranch_directory(self):
141
 
        """Test --directory mentioning a non-branch directory."""
142
 
        wt = self.build_tree(['subdir/'])
143
 
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
144
 
        self.assertContainsRe(err, 'ERROR: Not a branch')