20
20
# import system imports here
23
from bzrlib import errors, ui
24
import bzrlib.gpg as gpg
25
from bzrlib.tests import TestCase
26
from bzrlib.tests import features
28
class FakeConfig(object):
30
def gpg_signing_key(self):
31
return "amy@example.com"
33
def gpg_signing_command(self):
36
def acceptable_keys(self):
40
class TestCommandLine(TestCase):
31
from bzrlib.tests import (
37
class FakeConfig(config.MemoryStack):
39
def __init__(self, content=None):
42
gpg_signing_key=amy@example.com
43
gpg_signing_command=false'''
44
super(FakeConfig, self).__init__(content)
47
class TestCommandLine(tests.TestCase):
50
super(TestCommandLine, self).setUp()
51
self.my_gpg = gpg.GPGStrategy(FakeConfig())
42
53
def test_signing_command_line(self):
43
my_gpg = gpg.GPGStrategy(FakeConfig())
54
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
55
self.my_gpg._command_line())
57
def test_signing_command_line_from_default(self):
58
# Using 'default' for gpg_signing_key will use the mail part of 'email'
59
my_gpg = gpg.GPGStrategy(FakeConfig('''
60
email=Amy <amy@example.com>
61
gpg_signing_key=default
62
gpg_signing_command=false'''))
63
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
64
my_gpg._command_line())
66
def test_signing_command_line_from_email(self):
67
# Not setting gpg_signing_key will use the mail part of 'email'
68
my_gpg = gpg.GPGStrategy(FakeConfig('''
69
email=Amy <amy@example.com>
70
gpg_signing_command=false'''))
44
71
self.assertEqual(['false', '--clearsign', '-u', 'amy@example.com'],
45
72
my_gpg._command_line())
47
74
def test_checks_return_code(self):
48
75
# This test needs a unix like platform - one with 'false' to run.
49
76
# if you have one, please make this work :)
50
my_gpg = gpg.GPGStrategy(FakeConfig())
51
self.assertRaises(errors.SigningFailed, my_gpg.sign, 'content')
77
self.assertRaises(errors.SigningFailed, self.my_gpg.sign, 'content')
53
79
def assertProduces(self, content):
54
80
# This needs a 'cat' command or similar to work.
55
my_gpg = gpg.GPGStrategy(FakeConfig())
56
81
if sys.platform == 'win32':
57
82
# Windows doesn't come with cat, and we don't require it
58
83
# so lets try using python instead.
59
84
# But stupid windows and line-ending conversions.
60
85
# It is too much work to make sys.stdout be in binary mode.
61
86
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443
62
my_gpg._command_line = lambda:[sys.executable, '-c',
87
self.my_gpg._command_line = lambda:[sys.executable, '-c',
63
88
'import sys; sys.stdout.write(sys.stdin.read())']
64
89
new_content = content.replace('\n', '\r\n')
66
self.assertEqual(new_content, my_gpg.sign(content))
91
self.assertEqual(new_content, self.my_gpg.sign(content))
68
my_gpg._command_line = lambda:['cat', '-']
69
self.assertEqual(content, my_gpg.sign(content))
93
self.my_gpg._command_line = lambda:['cat', '-']
94
self.assertEqual(content, self.my_gpg.sign(content))
71
96
def test_returns_output(self):
72
97
content = "some content\nwith newlines\n"