/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 breezy/tests/blackbox/test_whoami.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

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