magic-wormhole/src/wormhole/test/test_scripts.py

181 lines
7.0 KiB
Python
Raw Normal View History

import os, sys
2015-09-27 01:00:09 +00:00
from twisted.trial import unittest
from twisted.python import procutils, log
from twisted.internet.utils import getProcessOutputAndValue
from .. import __version__
from .common import ServerBase
class ScriptsBase:
2015-09-27 01:00:09 +00:00
def find_executable(self):
# to make sure we're running the right executable (in a virtualenv),
# we require that our "wormhole" lives in the same directory as our
# "python"
2015-09-27 01:00:09 +00:00
locations = procutils.which("wormhole")
if not locations:
raise unittest.SkipTest("unable to find 'wormhole' in $PATH")
wormhole = locations[0]
if (os.path.dirname(os.path.abspath(wormhole)) !=
os.path.dirname(sys.executable)):
2015-09-27 01:00:09 +00:00
log.msg("locations: %s" % (locations,))
log.msg("sys.executable: %s" % (sys.executable,))
raise unittest.SkipTest("found the wrong 'wormhole' in $PATH: %s %s"
% (wormhole, sys.executable))
2015-09-27 01:00:09 +00:00
return wormhole
def is_runnable(self):
# One property of Versioneer is that many changes to the source tree
# (making a commit, dirtying a previously-clean tree) will change the
# version string. Entrypoint scripts frequently insist upon importing
# a library version that matches the script version (whatever was
# reported when 'pip install' was run), and throw a
# DistributionNotFound error when they don't match. This is really
# annoying in a workspace created with "pip install -e .", as you
# must re-run pip after each commit.
# So let's report just one error in this case (from test_version),
# and skip the other tests that we know will fail.
wormhole = self.find_executable()
d = getProcessOutputAndValue(wormhole, ["--version"])
def _check(res):
out, err, rc = res
if rc != 0:
raise unittest.SkipTest("wormhole is not runnable in this tree")
d.addCallback(_check)
return d
class ScriptVersion(ServerBase, ScriptsBase, unittest.TestCase):
# we need Twisted to run the server, but we run the sender and receiver
# with deferToThread()
2015-09-27 01:00:09 +00:00
def test_version(self):
# "wormhole" must be on the path, so e.g. "pip install -e ." in a
# virtualenv
wormhole = self.find_executable()
d = getProcessOutputAndValue(wormhole, ["--version"])
def _check(res):
out, err, rc = res
# argparse on py2 sends --version to stderr
# argparse on py3 sends --version to stdout
# aargh
out = out.decode("utf-8")
err = err.decode("utf-8")
if "DistributionNotFound" in err:
log.msg("stderr was %s" % err)
last = err.strip().split("\n")[-1]
self.fail("wormhole not runnable: %s" % last)
if sys.version_info[0] == 2:
self.failUnlessEqual(out, "")
self.failUnlessEqual(err, "magic-wormhole %s\n" % __version__)
else:
self.failUnlessEqual(err, "")
self.failUnlessEqual(out, "magic-wormhole %s\n" % __version__)
2015-09-27 01:00:09 +00:00
self.failUnlessEqual(rc, 0)
d.addCallback(_check)
return d
class Scripts(ServerBase, ScriptsBase, unittest.TestCase):
# we need Twisted to run the server, but we run the sender and receiver
# with deferToThread()
def setUp(self):
d = self.is_runnable()
d.addCallback(lambda _: ServerBase.setUp(self))
return d
2015-09-27 01:00:09 +00:00
def test_send_text_pre_generated_code(self):
wormhole = self.find_executable()
server_args = ["--relay-url", self.relayurl]
code = "1-abc"
message = "test message"
send_args = server_args + [
"send-text",
"--code", code,
message,
]
d1 = getProcessOutputAndValue(wormhole, send_args)
receive_args = server_args + [
"receive-text",
code,
]
d2 = getProcessOutputAndValue(wormhole, receive_args)
def _check_sender(res):
out, err, rc = res
out = out.decode("utf-8")
err = err.decode("utf-8")
2015-09-27 01:00:09 +00:00
self.failUnlessEqual(out,
"On the other computer, please run: "
"wormhole receive-text\n"
"Wormhole code is: %s\n\n"
"text sent\n" % code
)
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
return d2
d1.addCallback(_check_sender)
def _check_receiver(res):
out, err, rc = res
out = out.decode("utf-8")
err = err.decode("utf-8")
2015-09-27 01:00:09 +00:00
self.failUnlessEqual(out, message+"\n")
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
d1.addCallback(_check_receiver)
return d1
def test_send_file_pre_generated_code(self):
code = "1-abc"
message = "test message"
send_dir = self.mktemp()
os.mkdir(send_dir)
with open(os.path.join(send_dir, "testfile"), "w") as f:
f.write(message)
wormhole = self.find_executable()
server_args = ["--relay-url", self.relayurl]
send_args = server_args + [
"send-file",
"--code", code,
"testfile",
]
d1 = getProcessOutputAndValue(wormhole, send_args, path=send_dir)
receive_dir = self.mktemp()
os.mkdir(receive_dir)
receive_args = server_args + [
"receive-file",
code,
]
d2 = getProcessOutputAndValue(wormhole, receive_args, path=receive_dir)
def _check_sender(res):
out, err, rc = res
out = out.decode("utf-8")
err = err.decode("utf-8")
2015-09-27 01:00:09 +00:00
self.failUnlessIn("On the other computer, please run: "
"wormhole receive-file\n"
"Wormhole code is '%s'\n\n" % code,
out)
self.failUnlessIn("File sent.. waiting for confirmation\n"
"Confirmation received. Transfer complete.\n",
out)
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
return d2
d1.addCallback(_check_sender)
def _check_receiver(res):
out, err, rc = res
out = out.decode("utf-8")
err = err.decode("utf-8")
2015-09-27 01:00:09 +00:00
self.failUnlessIn("Receiving %d bytes for 'testfile'" % len(message),
out)
self.failUnlessIn("Received file written to ", out)
self.failUnlessEqual(err, "")
self.failUnlessEqual(rc, 0)
fn = os.path.join(receive_dir, "testfile")
self.failUnless(os.path.exists(fn))
with open(fn, "r") as f:
self.failUnlessEqual(f.read(), message)
d1.addCallback(_check_receiver)
return d1