Python-Vex可以在虚拟环境中执行命令

  • p5_190635
    了解作者
  • 36.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-13 14:30
    上传日期
Vex:可以在虚拟环境中执行命令
Python-Vex可以在虚拟环境中执行命令.zip
  • vex-master
  • README.rst
    19.6KB
  • scripts
  • vex
    55B
  • tox.ini
    98B
  • LICENSE
    1KB
  • setup.cfg
    28B
  • setup.py
    1.5KB
  • vex
  • exceptions.py
    1.7KB
  • options.py
    2.8KB
  • functional_tests
  • __init__.py
    0B
  • test_basic.py
    17.8KB
  • not_python
    43B
  • config.py
    5.6KB
  • _version.py
    19B
  • main.py
    7.4KB
  • run.py
    3.1KB
  • __init__.py
    88B
  • remove.py
    1.3KB
  • make.py
    2.4KB
  • tests
  • tempdir.py
    812B
  • tempvenv.py
    1.2KB
  • test_fakes.py
    603B
  • test_config.py
    5.5KB
  • test_run.py
    4.9KB
  • test_main.py
    4.2KB
  • __init__.py
    96B
  • tempvexrcfile.py
    1.1KB
  • fakes.py
    2KB
  • test_vex.py
    60B
  • test_shell_config.py
    6.4KB
  • shell_configs
  • fish
    3.1KB
  • bash
    1.5KB
  • zsh
    1.5KB
  • shell_config.py
    1.7KB
内容介绍
from __future__ import unicode_literals import os import sys import re import logging from threading import Timer from subprocess import Popen, PIPE from vex.tests import path_type from vex.tests.tempdir import TempDir, EmptyTempDir from vex.tests.tempvenv import TempVenv from vex.tests.tempvexrcfile import TempVexrcFile if sys.version_info < (3, 0): FileNotFoundError = OSError HERE = os.path.dirname(os.path.abspath(__file__)) logging.basicConfig(level=logging.DEBUG) class Run(object): """Boilerplate for running a vex process with given parameters. Context manager to ensure cleanup. Timeout to try to prevent hung tests. Can fake arguments and environment. Normally expect tests to wait until process finishes, but this is left to them for now. """ def __init__(self, args=None, env=None, timeout=None): self.args = args or [] self.env = env.copy() if env else {} self.timeout = timeout # self.timer = None self.process = None # self.command_found = None self.returned = None self.out = None self.err = None def start(self): env = self.env.copy() path = os.environ["PATH"] if isinstance(path, bytes): path = path.decode("utf-8") env["PATH"] = path process = None try: args = ["vex"] + self.args logging.debug("ARGS %r env %r", args, env) process = Popen( args, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=env) except FileNotFoundError as error: if error.errno != 2: raise self.command_found = False process = None else: if self.timeout is not None: self.timer = Timer(self.timeout, self.kill) self.timer.start() self.command_found = True self.process = process def __enter__(self): self.start() return self def kill(self): self.process.kill() def poll(self): self.process.poll() self.returned = self.process.returncode def finish(self, inp=None): assert inp is None or isinstance(inp, bytes) if self.process: out, err = self.process.communicate(inp) self.out = out self.err = err logging.debug("OUT %s", out.decode("utf-8")) logging.debug("ERR %s", err.decode("utf-8")) self.returned = self.process.returncode def __exit__(self, exc_type, exc_value, traceback): if self.timer: self.timer.cancel() self.timer = None if self.process and self.process.poll() is None: self.kill() def test_runs_without_args(): """vex Runs, but emits an error, proving that the test environment actually does have a vex executable on $PATH (as when run in a virtualenv after "pip install vex"). """ with Run([], timeout=0.5) as run: run.finish() assert run.command_found is True, run.command_found assert run.returned == 1 def test_help(): """vex --help Emits stuff containing at least "--help", not an error. """ with Run(["--help"], timeout=0.5) as run: run.finish() assert run.out is not None assert run.out.startswith(b"usage"), "unexpected output on stdout" assert b"--help" in run.out, "unexpected output on stdout" assert not run.err, "unexpected presence of output on stderr" def test_version(): """vex --version Emits a string of numbers and periods, one newline """ with Run(["--version"], timeout=0.5) as run: run.finish() assert run.out is not None match = re.match(br"^\d+\.\d+\.\d+\n$", run.out) assert match def test_list(): """vex --list Emits a list of directories in ve_base separated by newline """ ve_base = TempDir() os.mkdir(os.path.join(ve_base.path, b"foo")) os.mkdir(os.path.join(ve_base.path, b"bar")) os.mkdir(os.path.join(ve_base.path, b"-nope")) env = {"WORKON_HOME": ve_base.path} with ve_base, Run(["--list"], env=env, timeout=0.5) as run: run.finish() assert not run.err assert run.out == b"bar\nfoo\n" def test_list_no_ve_base(): nonexistent = os.path.join("/tmp/whatever/foo/bar") assert not os.path.exists(nonexistent) env = {"WORKON_HOME": nonexistent} with Run(["--list"], env=env, timeout=0.5) as run: run.finish() assert run.returned == 1 assert not run.out assert run.err assert nonexistent.encode("ascii") in run.err class TestShellConfig(object): def test_no_ve_base(self): """vex --shell-config bash with e.g. no ~/.virtualenvs """ env = {"WORKON_HOME": "/totally/nonexistent"} assert not os.path.exists(env["WORKON_HOME"]) with Run(["--shell-config", "bash"], env=env, timeout=0.5) as run: run.finish() assert run.out assert not run.err def test_no_arg(self): """vex --shell-config Non-traceback error. """ with Run(["--shell-config"], timeout=0.5) as run: run.finish() assert not run.out assert run.err assert not run.err.startswith(b"Traceback") def test_bash(self): """vex --shell-config bash Emits something and doesn't crash """ env = {"WORKON_HOME": os.getcwd()} with Run(["--shell-config", "bash"], env=env, timeout=0.5) as run: run.finish() assert run.out assert not run.err def test_zsh(self): """vex --shell-config zsh Emits something and doesn't crash """ env = {"WORKON_HOME": os.getcwd()} with Run(["--shell-config", "zsh"], env=env, timeout=0.5) as run: run.finish() assert run.out assert not run.err def test_fish(self): """vex --shell-config fish Emits something and doesn't crash """ env = {"WORKON_HOME": os.getcwd()} with Run(["--shell-config", "fish"], env=env, timeout=0.5) as run: run.finish() assert run.out assert not run.err def test_find_with_HOME(): """vex venvname echo foo with HOME set to a path containing a .virtualenvs dir containing a directory named venvname, resolve venvname as that directory and run without error. """ # 1. Make a temp directory # 2. Point the HOME variable at that # 3. Make a .virtualenvs directory in it # 4. Make a tempvenv inside that (time-consuming though...) # 5. run vex, passing basename of the tempvenv home = TempDir() workon_home = os.path.join(home.path, b".virtualenvs") assert isinstance(workon_home, path_type) os.mkdir(workon_home) name = "vex_test_find_with_HOME" venv = TempVenv(workon_home, name, []) env = {"HOME": home.path} with home, venv, Run([name, "echo", "foo"], env=env) as run: run.finish() assert run.command_found assert run.returned == 0 assert run.out == b"foo\n" # TODO: won't work on windows: no echo, pwd, etc. mark xfail or something class TestWithVirtualenv(object): def setup_class(self): # It's not ideal that we make a virtualenv only once, # but I'd rather do this than use a fake virtualenv # or wait an hour for tests to finish most of the time self.parent = TempDir() self.venv = TempVenv(self.parent.path, "vex_tests", []) self.venv.open() def teardown_class(self): try: self.venv.close() finally: self.parent.close() def test_virtualenv_created(self): """Spurious test showing the "test fixture" set up """ bin_path = os.path.join(self.venv.path, b
评论
    相关推荐
    • Python
      Python Boto3代码
    • Python
      python程序
    • Python
      Python
    • python
      Python,就在这里。
    • Python
      Python-
    • Python
      完整的python 其中包含用于学习python的代码,注释和微型项目。
    • Python
      项目Python
    • Python
      Pythonpython3中使用乌龟库的简单pong游戏 贡献 拉请求是欢迎的! :) 执照
    • Python
      Python Dit是Mijn储存库van Python
    • python
      Python 基本的python编程