~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-01-14 23:41:14 UTC
  • mfrom: (5611.1.3 bzr.dev)
  • Revision ID: pqm@pqm.ubuntu.com-20110114234114-r4hdusue691ekeg6
(jelmer) Cope with IPv6 addresses in ``bzr serve`` (Jelmer Vernooij)

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