From 8f1f3de1231f424dce269fef1e14d99a9c26cdda Mon Sep 17 00:00:00 2001 From: David Wilson Date: Tue, 6 Nov 2018 12:43:12 +0000 Subject: [PATCH] tests: Pythonize env_wrapper.sh, clean up local_test --- tests/data/env_wrapper.sh | 12 ------------ tests/data/stubs/stub-python.py | 15 +++++++++++++++ tests/local_test.py | 29 ++++++++++------------------- 3 files changed, 25 insertions(+), 31 deletions(-) delete mode 100755 tests/data/env_wrapper.sh create mode 100755 tests/data/stubs/stub-python.py diff --git a/tests/data/env_wrapper.sh b/tests/data/env_wrapper.sh deleted file mode 100755 index afb523f0..00000000 --- a/tests/data/env_wrapper.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# This script exists to test the behavior of Stream.python_path being set to a -# list. It sets an environmnt variable that we can detect, then executes any -# arguments passed to it. -export EXECUTED_VIA_ENV_WRAPPER=1 -if [ "${1:0:1}" == "-" ]; then - exec "$PYTHON" "$@" -else - export ENV_WRAPPER_FIRST_ARG="$1" - shift - exec "$@" -fi diff --git a/tests/data/stubs/stub-python.py b/tests/data/stubs/stub-python.py new file mode 100755 index 00000000..d9239c2b --- /dev/null +++ b/tests/data/stubs/stub-python.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +import json +import os +import subprocess +import sys + +os.environ['ORIGINAL_ARGV'] = json.dumps(sys.argv) +os.environ['THIS_IS_STUB_PYTHON'] = '1' + +if sys.argv[1].startswith('-'): + os.execvp(sys.executable, [sys.executable] + sys.argv[1:]) +else: + os.environ['STUB_PYTHON_FIRST_ARG'] = sys.argv.pop(1) + os.execvp(sys.executable, sys.argv[1:]) diff --git a/tests/local_test.py b/tests/local_test.py index 5a620e52..fe2bd149 100644 --- a/tests/local_test.py +++ b/tests/local_test.py @@ -5,11 +5,8 @@ import sys import unittest2 import mitogen -import mitogen.ssh -import mitogen.utils import testlib -import plain_old_module def get_sys_executable(): @@ -20,43 +17,37 @@ def get_os_environ(): return dict(os.environ) -class LocalTest(testlib.RouterMixin, testlib.TestCase): - stream_class = mitogen.ssh.Stream +class ConstructionTest(testlib.RouterMixin, testlib.TestCase): + stub_python_path = testlib.data_path('stubs/stub-python.py') def test_stream_name(self): context = self.router.local() pid = context.call(os.getpid) self.assertEquals('local.%d' % (pid,), context.name) - -class PythonPathTest(testlib.RouterMixin, testlib.TestCase): - stream_class = mitogen.ssh.Stream - - def test_inherited(self): + def test_python_path_inherited(self): context = self.router.local() self.assertEquals(sys.executable, context.call(get_sys_executable)) - def test_string(self): - os.environ['PYTHON'] = sys.executable + def test_python_path_string(self): context = self.router.local( - python_path=testlib.data_path('env_wrapper.sh'), + python_path=self.stub_python_path, ) - self.assertEquals(sys.executable, context.call(get_sys_executable)) env = context.call(get_os_environ) - self.assertEquals('1', env['EXECUTED_VIA_ENV_WRAPPER']) + self.assertEquals('1', env['THIS_IS_STUB_PYTHON']) - def test_list(self): + def test_python_path_list(self): context = self.router.local( python_path=[ - testlib.data_path('env_wrapper.sh'), + self.stub_python_path, "magic_first_arg", sys.executable ] ) self.assertEquals(sys.executable, context.call(get_sys_executable)) env = context.call(get_os_environ) - self.assertEquals('magic_first_arg', env['ENV_WRAPPER_FIRST_ARG']) - self.assertEquals('1', env['EXECUTED_VIA_ENV_WRAPPER']) + self.assertEquals('magic_first_arg', env['STUB_PYTHON_FIRST_ARG']) + self.assertEquals('1', env['THIS_IS_STUB_PYTHON']) if __name__ == '__main__':