Commit 7b6628d4 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

TEST: Test utility funcs, update example manifest

parent b7686d27
#!/usr/bin/env python
#
"""Utility functions used for testing. """
import os
import os.path as op
import contextlib
import threading
import functools as ft
import sys
import re
# py3
try:
import http.server as http
from io import StringIO
from unittest import mock
# py2
except ImportError:
from StringIO import StringIO
import SimpleHTTPServer as http
import SocketServer as socksrv
http.HTTPServer = socksrv.TCPServer
import mock
@contextlib.contextmanager
def onpath(dir):
"""Context manager which temporarily adds dir to the front of $PATH. """
path = os.environ['PATH']
os.environ['PATH'] = dir + op.pathsep + path
try:
yield
finally:
os.environ['PATH'] = path
@contextlib.contextmanager
def indir(dir):
"""Context manager which temporarily changes into dir."""
prevdir = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(prevdir)
class HTTPServer(threading.Thread):
"""Simple HTTP server which serves files from a specified directory.
Intended to be used via the :func:`server` context manager function.
"""
def __init__(self, rootdir):
threading.Thread.__init__(self)
self.daemon = True
self.rootdir = rootdir
handler = ft.partial(http.SimpleHTTPRequestHandler, directory=rootdir)
self.server = http.HTTPServer(('', 0), handler)
def stop(self):
self.server.shutdown()
@property
def port(self):
return self.server.server_port
def run(self):
self.server.serve_forever()
@contextlib.contextmanager
def server(rootdir=None):
"""Start a :class:`HTTPServer` on a separate thread to serve files from
``rootdir`` (defaults to the current working directory), then shut it down
afterwards.
"""
if rootdir is None:
rootdir = os.getcwd()
srv = HTTPServer(rootdir)
srv.start()
srv.url = 'http://localhost:{}'.format(srv.port)
try:
yield srv
finally:
srv.stop()
class CaptureStdout(object):
"""Context manager which captures stdout and stderr. """
def __init__(self):
self.reset()
def reset(self):
self.__mock_stdout = StringIO('')
self.__mock_stderr = StringIO('')
self.__mock_stdout.mode = 'w'
self.__mock_stderr.mode = 'w'
return self
def __enter__(self):
self.__real_stdout = sys.stdout
self.__real_stderr = sys.stderr
sys.stdout = self.__mock_stdout
sys.stderr = self.__mock_stderr
return self
def __exit__(self, *args, **kwargs):
sys.stdout = self.__real_stdout
sys.stderr = self.__real_stderr
return False
@property
def stdout(self):
self.__mock_stdout.seek(0)
return self.__mock_stdout.read()
@property
def stderr(self):
self.__mock_stderr.seek(0)
return self.__mock_stderr.read()
@contextlib.contextmanager
def mock_input(*responses):
"""Mock the built-in ``input`` or ``raw_input`` function so that it
returns the specified sequence of ``responses``.
Each response is returned from the ``input`` function in order, unless it
is a callable, in which case it is called, and then the next non-callable
response returned. This gives us a hacky way to manipulate things while
stuck in an input REPL loop.
"""
resp = iter(responses)
def _input(*a, **kwa):
n = next(resp)
while callable(n):
n()
n = next(resp)
return n
if sys.version[0] == '2': target = 'builtins.raw_input'
else: target = 'builtins.input'
with mock.patch(target, _input):
yield
def strip_ansi_escape_sequences(text):
"""Does what function name says it does. """
return re.sub('\x1b\[[0-9;]*m', '', text)
......@@ -63,29 +63,50 @@
// builds.
// - "environment" Conda environment.yml file
// - "sha256" SHA256 checksum of environment.yml file
// - "output" Optional. Number of lines of output to
// expect when installing the conda
// environment - used to report progress
// to the user.
// - "output" Dictionary containing number of lines of
// output to expect when installing the FSL
// environment ("install" : "100"), or when
// updating from an older FSL installation
// (e.g. "6.1.0" : "50") - used to report
// progress to the user.
"6.1.0" : [
{
"platform" : "linux-64",
"environment" : "http://localhost:8888/fsl-6.1.0-linux-64.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11"
}
},
{
"platform" : "macos-64",
"environment" : "http://localhost:8888/fsl-6.1.0-macos-64.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11"
}
},
{
"platform" : "linux-64",
"cuda" : "9.2",
"environment" : "http://localhost:8888/fsl-6.1.0-linux-64-cuda9.2.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11"
}
},
{
"platform" : "linux-64",
"cuda" : "10.2",
"environment" : "http://localhost:8888/fsl-6.1.0-linux-64-cuda10.2.yml",
"sha256" : "85ef650586c1d7f3475fe7d16108bb94b737decbfc7506ad4deff2917365d05f",
"output" : {
"install" : "92",
"6.0.6" : "11"
}
}
],
"6.2.0" : [
......@@ -93,20 +114,43 @@
"platform" : "linux-64",
"environment" : "http://localhost:8888/fsl-6.2.0-linux-64.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11",
"6.1.0" : "11"
}
},
{
"platform" : "macos-64",
"environment" : "http://localhost:8888/fsl-6.2.0-macos-64.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11",
"6.1.0" : "11"
}
},
{
"platform" : "linux-64",
"cuda" : "9.2",
"environment" : "http://localhost:8888/fsl-6.2.0-linux-64-cuda9.2.yml",
"sha256" : "952a0101fc19948e785572f8c06436c59f19c4ef92df9425f2e2033d4b986918",
"output" : "493"
"output" : {
"install" : "92",
"6.0.6" : "11",
"6.1.0" : "11"
}
},
{
"platform" : "linux-64",
"cuda" : "10.2",
"environment" : "http://localhost:8888/fsl-6.2.0-linux-64-cuda10.2.yml",
"sha256" : "6499385a2f5d2da48f4a44c95e090a5156319b0acbbd47029d4270abc691a290",
"output" : {
"install" : "92",
"6.0.6" : "11",
"6.1.0" : "11"
}
}
]
}
......
......@@ -161,7 +161,7 @@ def test_download_file():
f.write('hello\n')
with server(cwd) as srv:
url = 'http://localhost:{}/file'.format(srv.port)
url = '{}/file'.format(srv.url)
inst.download_file(url, 'copy')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment