~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2010-10-13 00:26:41 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101013002641-9tlh9k89mlj1666m
Keep docs-plain working.

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, 2007, 2009, 2010 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 (
 
24
    osutils,
22
25
    config,
23
 
    tests,
24
26
    )
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):
 
27
from bzrlib.tests import TestCaseWithTransport
 
28
 
 
29
 
 
30
class TestWhoami(TestCaseWithTransport):
 
31
 
 
32
    def test_whoami(self):
37
33
        # this should always identify something, if only "john@localhost"
38
34
        out = self.run_bzr("whoami")[0]
39
35
        self.assertTrue(len(out) > 0)
40
36
        self.assertEquals(1, out.count('@'))
41
37
 
42
 
    def test_whoami_email_no_args(self):
43
38
        out = self.run_bzr("whoami --email")[0]
44
39
        self.assertTrue(len(out) > 0)
45
40
        self.assertEquals(1, out.count('@'))
46
41
 
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
 
 
52
42
    def test_whoami_branch(self):
53
43
        """branch specific user identity works."""
54
44
        wt = self.make_branch_and_tree('.')
55
45
        b = bzrlib.branch.Branch.open('.')
56
46
        b.get_config().set_user_option('email',
57
47
                                       'Branch Identity <branch@identi.ty>')
58
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
 
        self.assertWhoAmI('branch@identi.ty', '--email')
 
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)
60
52
 
61
53
        # Verify that the environment variable overrides the value
62
54
        # 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')
 
55
        os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
 
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)
66
60
 
67
61
    def test_whoami_utf8(self):
68
62
        """verify that an identity can be in utf-8."""
 
63
        wt = self.make_branch_and_tree('.')
69
64
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
70
65
                     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')
 
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)
74
71
 
75
72
    def test_whoami_ascii(self):
76
73
        """
81
78
        b = bzrlib.branch.Branch.open('.')
82
79
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
80
                                       '<branch@identi.ty>')
84
 
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
85
 
                          encoding='ascii')
86
 
        self.assertWhoAmI('branch@identi.ty', '--email',
87
 
                          encoding='ascii')
 
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)
88
85
 
89
86
    def test_warning(self):
90
87
        """verify that a warning is displayed if no email is given."""
95
92
                          'recommended.\n', display)
96
93
 
97
94
    def test_whoami_not_set(self):
98
 
        """Ensure whoami error if username is not set and not inferred.
 
95
        """Ensure whoami error if username is not set.
99
96
        """
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))
 
97
        osutils.set_or_unset_env('EMAIL', None)
 
98
        osutils.set_or_unset_env('BZR_EMAIL', None)
105
99
        out, err = self.run_bzr(['whoami'], 3)
106
100
        self.assertContainsRe(err, 'Unable to determine your name')
107
101
 
110
104
        wt = self.make_branch_and_tree('subdir')
111
105
        c = wt.branch.get_config()
112
106
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
113
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
 
                          '--directory', 'subdir')
 
107
        out, err = self.run_bzr("whoami --directory subdir")
 
108
        self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
115
109
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
110
                      'Changed Identity <changed@identi.ty>'])
117
 
        c = wt.branch.get_config()
118
111
        self.assertEquals('Changed Identity <changed@identi.ty>',
119
112
                          c.get_user_option('email'))
120
113
 
124
117
        c = wt.branch.get_config()
125
118
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
126
119
        url = self.get_readonly_url() + '/subdir'
127
 
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
128
 
                          '--directory', url)
 
120
        out, err = self.run_bzr(['whoami', '--directory', url])
 
121
        self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
129
122
        url = self.get_url('subdir')
130
123
        self.run_bzr(['whoami', '--directory', url, '--branch',
131
124
                      'Changed Identity <changed@identi.ty>'])
132
125
        # The identity has been set in the branch config (but not the global
133
126
        # config)
134
 
        c = wt.branch.get_config()
135
127
        self.assertEquals('Changed Identity <changed@identi.ty>',
136
128
                          c.get_user_option('email'))
137
129
        global_conf = config.GlobalConfig()