2 """Tests various operations using the cola.git module
15 class GitCommandTest(unittest
.TestCase
):
16 """Runs tests using a git.Git instance"""
19 """Creates a git.Git instance for later use"""
22 def test_version(self
):
23 """Test running 'git version'"""
24 version
= self
.git
.version()
25 self
.failUnless(version
.startswith('git version'))
28 """Test running 'git tag'"""
29 tags
= self
.git
.tag().splitlines()
30 self
.failUnless( 'v1.0.0' in tags
)
33 """Test running 'git show'"""
34 sha
= '1b9742bda5d26a4f250fa64657f66ed20624a084'
35 contents
= self
.git
.show(sha
).splitlines()
36 self
.failUnless(contents
[0] == '/build')
38 def test_stdout(self
):
39 """Test overflowing the stdout buffer"""
40 # Write to stdout only
42 's = "\\0" * (1024 * 16 + 1);'
43 'sys.stdout.write(s);')
44 status
, out
= git
.Git
.execute(['python', '-c', code
],
47 self
.assertEqual(status
, 0)
48 self
.assertEqual(len(out
), 1024 * 16 + 1)
50 def test_stderr_empty(self
):
51 """Test that stderr is ignored by execute() without with_stderr"""
52 # Write to stderr but ignore it
54 's = "\\0" * (1024 * 16 + 1);'
55 'sys.stderr.write(s);')
56 status
, out
= git
.Git
.execute(['python', '-c', code
],
59 self
.assertEqual(status
, 0)
60 self
.assertEqual(len(out
), 0)
62 def test_stderr_nonempty_with_stderr(self
):
63 """Test that with_stderr makes execute() see stderr"""
64 # Write to stderr and capture it
66 's = "\\0" * (1024 * 16 + 1);'
67 'sys.stderr.write(s);')
68 status
, out
= git
.Git
.execute(['python', '-c', code
],
72 self
.assertEqual(status
, 0)
73 self
.assertEqual(len(out
), 1024 * 16 + 1)
75 def test_stdout_and_stderr_ignores_stderr(self
):
76 """Test ignoring stderr when stdout+stderr are provided"""
77 # Write to stdout only
79 's = "\\0" * (1024 * 16 + 1);'
80 'sys.stdout.write(s);')
81 status
, out
= git
.Git
.execute(['python', '-c', code
],
84 self
.assertEqual(status
, 0)
85 self
.assertEqual(len(out
), 1024 * 16 + 1)
87 def test_stdout_and_stderr_ignores_stderr_v2(self
):
88 """Test ignoring stderr when stdout+stderr are provided (v2)"""
89 # Write to stdout and stderr but only capture stdout
91 's = "\\0" * (1024 * 16 + 1);'
92 'sys.stdout.write(s);'
93 'sys.stderr.write(s);')
94 status
, out
= git
.Git
.execute(['python', '-c', code
],
97 self
.assertEqual(status
, 0)
98 self
.assertEqual(len(out
), 1024 * 16 + 1)
100 def test_stdout_and_stderr_sees_stderr(self
):
101 """Test seeing both stderr and stdout when both are available"""
102 # Write to stdout and stderr and capture both
103 code
= ('import sys;'
104 's = "\\0" * (1024 * 16 + 1);'
105 'sys.stdout.write(s);'
106 'sys.stderr.write(s);')
107 status
, out
= git
.Git
.execute(['python', '-c', code
],
110 with_raw_output
=True)
111 self
.assertEqual(status
, 0)
112 self
.assertEqual(len(out
), 1024 * 16 * 2 + 2)
114 def test_stdout_and_stderr_sees_stderr_v2(self
):
115 """Test seeing both stderr and stdout when both are available (v2)."""
116 # Write to stderr and stdout (swapped) and capture both
117 code
= ('import sys;'
118 's = "\\0" * (1024 * 16 + 1);'
119 'sys.stderr.write(s);'
120 'sys.stdout.write(s);')
121 status
, out
= git
.Git
.execute(['python', '-c', code
],
122 # otherwise, same as above
125 with_raw_output
=True)
126 self
.assertEqual(status
, 0)
127 self
.assertEqual(len(out
), 1024 * 16 * 2 + 2)
129 def test_it_doesnt_deadlock(self
):
130 """Test that we don't deadlock with both stderr and stdout"""
131 # 16k+1 bytes to exhaust any output buffers
132 code
= ('import sys;'
133 's = "\\0" * (1024 * 16 + 1);'
134 'sys.stderr.write(s);'
135 'sys.stdout.write(s);')
136 out
= git
.Git
.execute(['python', '-c', code
])
137 self
.assertEqual(out
, '\0' * (1024 * 16 + 1))
139 def test_it_handles_interrupted_syscalls(self
):
140 """Test that we handle interrupted system calls"""
141 # send ourselves a signal that causes EINTR
142 prev_handler
= signal
.signal(signal
.SIGALRM
, lambda x
,y
: 1)
146 status
, output
= git
.Git
.execute(['sleep', '1'],
148 self
.assertEqual(status
, 0)
150 signal
.signal(signal
.SIGALRM
, prev_handler
)
152 if __name__
== '__main__':