~bzr-pqm/bzr/bzr.dev

6421.3.3 by Vincent Ladeuil
Mwhahaha. You cannot get None for email not being set in bazaar.conf. On the other hand, a test raising NoWhoami was missing here ;)
1
# Copyright (C) 2006, 2007, 2009-2012 Canonical Ltd
1816.2.10 by Robey Pointer
code style changes
2
#
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
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.
1816.2.10 by Robey Pointer
code style changes
7
#
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
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.
1816.2.10 by Robey Pointer
code style changes
12
#
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
16
17
1816.2.10 by Robey Pointer
code style changes
18
"""Black-box tests for bzr whoami."""
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
19
20
import bzrlib
5171.3.18 by Andrew Bennetts
Specifically assert that setting an identity with --directory and --branch sets it in the branch, not globally.
21
from bzrlib import (
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
22
    branch,
5171.3.18 by Andrew Bennetts
Specifically assert that setting an identity with --directory and --branch sets it in the branch, not globally.
23
    config,
6421.3.3 by Vincent Ladeuil
Mwhahaha. You cannot get None for email not being set in bazaar.conf. On the other hand, a test raising NoWhoami was missing here ;)
24
    errors,
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
25
    tests,
5171.3.18 by Andrew Bennetts
Specifically assert that setting an identity with --directory and --branch sets it in the branch, not globally.
26
    )
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
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.assertEquals('', err)
34
        lines = out.splitlines()
35
        self.assertLength(1, lines)
36
        self.assertEquals(expected, lines[0].rstrip())
37
38
    def test_whoami_no_args_no_conf(self):
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
39
        # this should always identify something, if only "john@localhost"
1816.2.5 by Robey Pointer
clean up whoami tests a bit
40
        out = self.run_bzr("whoami")[0]
41
        self.assertTrue(len(out) > 0)
1816.2.10 by Robey Pointer
code style changes
42
        self.assertEquals(1, out.count('@'))
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
43
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
44
    def test_whoami_email_no_args(self):
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
45
        out = self.run_bzr("whoami --email")[0]
1816.2.5 by Robey Pointer
clean up whoami tests a bit
46
        self.assertTrue(len(out) > 0)
1816.2.10 by Robey Pointer
code style changes
47
        self.assertEquals(1, out.count('@'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
48
5616.3.1 by Jelmer Vernooij
Print error if both --email and a new identity were specified.
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.assertEquals("", out)
53
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
54
    def set_branch_email(self, b, email):
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
55
        b.get_config_stack().set('email', email)
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
56
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
57
    def test_whoami_branch(self):
58
        """branch specific user identity works."""
1816.2.10 by Robey Pointer
code style changes
59
        wt = self.make_branch_and_tree('.')
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
60
        b = bzrlib.branch.Branch.open('.')
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
61
        self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
62
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
63
        self.assertWhoAmI('branch@identi.ty', '--email')
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
64
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
65
        # Verify that the environment variable overrides the value
66
        # in the file
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
67
        self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
68
        self.assertWhoAmI('Different ID <other@environ.ment>')
69
        self.assertWhoAmI('other@environ.ment', '--email')
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
70
71
    def test_whoami_utf8(self):
72
        """verify that an identity can be in utf-8."""
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
73
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
1840.1.3 by Robey Pointer
fix some 80col wrapping and add a blackbox test to verify that a warning is displayed when a non-email identity is set
74
                     encoding='utf-8')
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
75
        self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
76
                          encoding='utf-8')
77
        self.assertWhoAmI('branch@identi.ty', '--email')
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
78
79
    def test_whoami_ascii(self):
1840.1.3 by Robey Pointer
fix some 80col wrapping and add a blackbox test to verify that a warning is displayed when a non-email identity is set
80
        """
81
        verify that whoami doesn't totally break when in utf-8, using an ascii
82
        encoding.
83
        """
1816.2.10 by Robey Pointer
code style changes
84
        wt = self.make_branch_and_tree('.')
1816.2.3 by Robey Pointer
move the whoami blackbox tests into their own file and add more tests
85
        b = bzrlib.branch.Branch.open('.')
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
86
        self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
87
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
88
                          encoding='ascii')
89
        self.assertWhoAmI('branch@identi.ty', '--email',
90
                          encoding='ascii')
1840.1.3 by Robey Pointer
fix some 80col wrapping and add a blackbox test to verify that a warning is displayed when a non-email identity is set
91
92
    def test_warning(self):
93
        """verify that a warning is displayed if no email is given."""
94
        self.make_branch_and_tree('.')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
95
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
1840.1.5 by Robey Pointer
change the warning message for a 'whoami' with no email address, on jam's recommendation
96
        self.assertEquals('"Branch Identity" does not seem to contain an '
97
                          'email address.  This is allowed, but not '
98
                          'recommended.\n', display)
5187.2.4 by Parth Malwankar
added tests.
99
100
    def test_whoami_not_set(self):
5609.31.1 by mbp at sourcefrog
Blackbox tests for no identity set must disable whoami inference
101
        """Ensure whoami error if username is not set and not inferred.
5187.2.4 by Parth Malwankar
added tests.
102
        """
5570.3.12 by Vincent Ladeuil
Replace osutils.set_or_unset_env calls with self.overrideEnv.
103
        self.overrideEnv('EMAIL', None)
104
        self.overrideEnv('BZR_EMAIL', None)
5609.31.1 by mbp at sourcefrog
Blackbox tests for no identity set must disable whoami inference
105
        # Also, make sure that it's not inferred from mailname.
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
106
        self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
5187.2.4 by Parth Malwankar
added tests.
107
        out, err = self.run_bzr(['whoami'], 3)
108
        self.assertContainsRe(err, 'Unable to determine your name')
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
109
110
    def test_whoami_directory(self):
5171.3.17 by Andrew Bennetts
Tweak docstrings.
111
        """Test --directory option."""
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
112
        wt = self.make_branch_and_tree('subdir')
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
113
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
114
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
115
                          '--directory', 'subdir')
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
116
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
117
                      'Changed Identity <changed@identi.ty>'])
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
118
        # Refresh wt as 'whoami' modified it
119
        wt = wt.bzrdir.open_workingtree()
120
        c = wt.branch.get_config_stack()
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
121
        self.assertEquals('Changed Identity <changed@identi.ty>',
6421.3.1 by Vincent Ladeuil
Migrate more branch options to config stacks.
122
                          c.get('email'))
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
123
124
    def test_whoami_remote_directory(self):
5171.3.17 by Andrew Bennetts
Tweak docstrings.
125
        """Test --directory option with a remote directory."""
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
126
        wt = self.make_branch_and_tree('subdir')
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
127
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
128
        url = self.get_readonly_url() + '/subdir'
5991.1.1 by Vincent Ladeuil
Slightly simplify whoami tests.
129
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
130
                          '--directory', url)
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
131
        url = self.get_url('subdir')
132
        self.run_bzr(['whoami', '--directory', url, '--branch',
133
                      'Changed Identity <changed@identi.ty>'])
5171.3.18 by Andrew Bennetts
Specifically assert that setting an identity with --directory and --branch sets it in the branch, not globally.
134
        # The identity has been set in the branch config (but not the global
135
        # config)
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
136
        c = branch.Branch.open(url).get_config_stack()
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
137
        self.assertEquals('Changed Identity <changed@identi.ty>',
6421.3.1 by Vincent Ladeuil
Migrate more branch options to config stacks.
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()
6421.3.3 by Vincent Ladeuil
Mwhahaha. You cannot get None for email not being set in bazaar.conf. On the other hand, a test raising NoWhoami was missing here ;)
145
        self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
146
147
    def test_whoami_nonbranch_directory(self):
5171.3.17 by Andrew Bennetts
Tweak docstrings.
148
        """Test --directory mentioning a non-branch directory."""
5171.3.15 by Martin von Gagern
Add --directory option to whoami.
149
        wt = self.build_tree(['subdir/'])
5171.3.16 by Andrew Bennetts
Remove redundant clearing and restoring of BZR_EMAIL; the TestCase base class already takes care of that.
150
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
151
        self.assertContainsRe(err, 'ERROR: Not a branch')