~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_gpg.py

(jelmer) Switch the commit code over to use config stacks. (Bazaar
 Developers)

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import sys
22
22
 
23
23
from bzrlib import (
 
24
    config,
24
25
    errors,
25
26
    gpg,
 
27
    tests,
26
28
    trace,
27
29
    ui,
28
30
    )
31
33
    features,
32
34
    )
33
35
 
34
 
class FakeConfig(object):
35
 
 
36
 
    def gpg_signing_key(self):
37
 
        return "amy@example.com"
38
 
 
39
 
    def gpg_signing_command(self):
40
 
        return "false"
41
 
 
42
 
    def acceptable_keys(self):
43
 
        return None
44
 
 
45
 
 
46
 
class TestCommandLine(TestCase):
 
36
 
 
37
class FakeConfig(config.Stack):
 
38
 
 
39
    def __init__(self):
 
40
        store = config.IniFileStore()
 
41
        store._load_from_string('''
 
42
gpg_signing_key=amy@example.com
 
43
gpg_signing_command=false''')
 
44
        super(FakeConfig, self).__init__([store.get_sections])
 
45
 
 
46
 
 
47
class TestCommandLine(tests.TestCase):
 
48
 
 
49
    def setUp(self):
 
50
        super(TestCommandLine, self).setUp()
 
51
        self.my_gpg = gpg.GPGStrategy(FakeConfig())
47
52
 
48
53
    def test_signing_command_line(self):
49
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
50
54
        self.assertEqual(['false',  '--clearsign', '-u', 'amy@example.com'],
51
 
                         my_gpg._command_line())
 
55
                         self.my_gpg._command_line())
52
56
 
53
57
    def test_checks_return_code(self):
54
58
        # This test needs a unix like platform - one with 'false' to run.
55
59
        # if you have one, please make this work :)
56
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
57
 
        self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
 
60
        self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
58
61
 
59
62
    def assertProduces(self, content):
60
63
        # This needs a 'cat' command or similar to work.
61
 
        my_gpg = gpg.GPGStrategy(FakeConfig())
62
64
        if sys.platform == 'win32':
63
65
            # Windows doesn't come with cat, and we don't require it
64
66
            # so lets try using python instead.
65
67
            # But stupid windows and line-ending conversions.
66
68
            # It is too much work to make sys.stdout be in binary mode.
67
69
            # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
68
 
            my_gpg._command_line = lambda:[sys.executable, '-c',
 
70
            self.my_gpg._command_line = lambda:[sys.executable, '-c',
69
71
                    'import sys; sys.stdout.write(sys.stdin.read())']
70
72
            new_content = content.replace('\n', '\r\n')
71
73
 
72
 
            self.assertEqual(new_content, my_gpg.sign(content))
 
74
            self.assertEqual(new_content, self.my_gpg.sign(content))
73
75
        else:
74
 
            my_gpg._command_line = lambda:['cat', '-']
75
 
            self.assertEqual(content, my_gpg.sign(content))
 
76
            self.my_gpg._command_line = lambda:['cat', '-']
 
77
            self.assertEqual(content, self.my_gpg.sign(content))
76
78
 
77
79
    def test_returns_output(self):
78
80
        content = "some content\nwith newlines\n"