Skip to content

Commit

Permalink
translate expectations into the native newline style
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin J. Mitchell committed Jun 7, 2010
1 parent b384d56 commit 4177240
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
54 changes: 28 additions & 26 deletions slave/buildslave/test/unit/test_slave_commands_base.py
Expand Up @@ -4,6 +4,7 @@
from twisted.trial import unittest
from twisted.internet import task, defer

from buildslave.test.util import nl
from buildslave.commands.base import ShellCommand, Obfuscated, \
DummyCommand, WaitCommand, waitCommandRegistry, AbandonChain
from buildslave.commands.utils import getCommand
Expand All @@ -30,6 +31,12 @@ def stdoutCommand(output):
def stderrCommand(output):
return [sys.executable, '-c', 'import sys; sys.stdout.write("%s\\n")' % output]

# windows returns rc 1, because exit status cannot indicate "signalled";
# posix returns rc -1 for "signalled"
FATAL_RC = -1
if sys.platform.startswith('win'):
FATAL_RC = 1

class TestShellCommand(unittest.TestCase):
def testStart(self):
basedir = "test_slave_commands_base.shellcommand.start"
Expand All @@ -38,7 +45,7 @@ def testStart(self):

d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
d.addCallback(check)
return d
Expand All @@ -50,7 +57,7 @@ def testNoStdout(self):

d = s.start()
def check(ign):
self.failIf({'stdout': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failIf({'stdout': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
d.addCallback(check)
return d
Expand All @@ -62,9 +69,9 @@ def testKeepStdout(self):

d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
self.failUnlessEquals(s.stdout, 'hello\n')
self.failUnlessEquals(s.stdout, nl('hello\n'))
d.addCallback(check)
return d

Expand All @@ -75,7 +82,7 @@ def testStderr(self):

d = s.start()
def check(ign):
self.failIf({'stderr': 'hello\n'} not in b.updates, ppupdates(b.updates))
self.failIf({'stderr': nl('hello\n')} not in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
d.addCallback(check)
return d
Expand All @@ -87,7 +94,7 @@ def testNoStderr(self):

d = s.start()
def check(ign):
self.failIf({'stderr': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failIf({'stderr': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
d.addCallback(check)
return d
Expand All @@ -99,9 +106,9 @@ def testKeepStderr(self):

d = s.start()
def check(ign):
self.failUnless({'stderr': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failUnless({'stderr': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
self.failUnlessEquals(s.stderr, 'hello\n')
self.failUnlessEquals(s.stderr, nl('hello\n'))
d.addCallback(check)
return d

Expand All @@ -112,9 +119,9 @@ def testKeepStdout(self):

d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
self.failUnlessEquals(s.stdout, 'hello\n')
self.failUnlessEquals(s.stdout, nl('hello\n'))
d.addCallback(check)
return d

Expand All @@ -125,7 +132,7 @@ def testStringCommand(self):

d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} in b.updates, ppupdates(b.updates))
self.failUnless({'rc': 0} in b.updates, ppupdates(b.updates))
d.addCallback(check)
return d
Expand All @@ -138,8 +145,8 @@ def testCommandTimeout(self):
s._reactor = clock
d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} not in b.updates, ppupdates(b.updates))
self.failUnless({'rc': -1} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} not in b.updates, ppupdates(b.updates))
self.failUnless({'rc': FATAL_RC} in b.updates, ppupdates(b.updates))
d.addCallback(check)
clock.advance(6)
return d
Expand All @@ -152,13 +159,8 @@ def testCommandMaxTime(self):
s._reactor = clock
d = s.start()
def check(ign):
self.failUnless({'stdout': 'hello\n'} not in b.updates, ppupdates(b.updates))
# windows returns rc 1, because exit status cannot indicate "signalled";
# posix returns rc -1 for "signalled"
exprc = -1
if sys.platform.startswith('win'):
exprc = 1
self.failUnless({'rc': exprc} in b.updates, ppupdates(b.updates))
self.failUnless({'stdout': nl('hello\n')} not in b.updates, ppupdates(b.updates))
self.failUnless({'rc': FATAL_RC} in b.updates, ppupdates(b.updates))
d.addCallback(check)
clock.advance(6) # should knock out maxTime
return d
Expand Down Expand Up @@ -191,7 +193,7 @@ def testLogEnviron(self):
d = s.start()
def check(ign):
headers = "".join([update.values()[0] for update in b.updates if update.keys() == ["header"] ])
self.failUnless("FOO=BAR\n" in headers, "got:\n" + headers)
self.failUnless(nl("FOO=BAR\n") in headers, "got:\n" + headers)
d.addCallback(check)
return d

Expand All @@ -203,7 +205,7 @@ def testNoLogEnviron(self):
d = s.start()
def check(ign):
headers = "".join([update.values()[0] for update in b.updates if update.keys() == ["header"] ])
self.failUnless("FOO=BAR\n" not in headers, "got:\n" + headers)
self.failUnless(nl("FOO=BAR\n") not in headers, "got:\n" + headers)
d.addCallback(check)
return d

Expand All @@ -219,8 +221,8 @@ def testEnvironExpandVar(self):
def check(ign):
headers = "".join([update.values()[0] for update in b.updates if update.keys() == ["header"] ])
self.failUnless("EXPND=-$" not in headers, "got:\n" + headers)
self.failUnless("DOESNT_FIND=--\n" in headers, "got:\n" + headers)
self.failUnless("DOESNT_EXPAND=-${---}-\n" in headers, "got:\n" + headers)
self.failUnless("DOESNT_FIND=--" in headers, "got:\n" + headers)
self.failUnless("DOESNT_EXPAND=-${---}-" in headers, "got:\n" + headers)
d.addCallback(check)
return d

Expand All @@ -241,8 +243,8 @@ def testSendStatus(self):
basedir = "test_slave_commands_base.logging.sendStatus"
b = FakeSlaveBuilder(False, basedir)
s = ShellCommand(b, stdoutCommand('hello'), basedir)
s.sendStatus({'stdout': 'hello\n'})
self.failUnlessEqual(b.updates, [{'stdout': 'hello\n'}], ppupdates(b.updates))
s.sendStatus({'stdout': nl('hello\n')})
self.failUnlessEqual(b.updates, [{'stdout': nl('hello\n')}], ppupdates(b.updates))

def testSendBuffered(self):
basedir = "test_slave_commands_base.logging.sendBuffered"
Expand Down
12 changes: 12 additions & 0 deletions slave/buildslave/test/util.py
@@ -0,0 +1,12 @@
import sys

def nl(s):
"""Convert the given string to the native newline format, assuming it is
already in normal UNIX newline format (\n). Use this to create the
appropriate expectation in a failUnlessEqual"""
if not isinstance(s, basestring):
return s
if sys.platform.startswith('win'):
return s.replace('\n', '\r\n')
else:
return s

0 comments on commit 4177240

Please sign in to comment.