File: _util.py

package info (click to toggle)
fabric 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 988 kB
  • sloc: python: 4,816; makefile: 8
file content (75 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from contextlib import contextmanager
import os
import re
import sys

from invoke.vendor.lexicon import Lexicon
from pytest_relaxed import trap

from fabric.main import make_program


support = os.path.join(os.path.abspath(os.path.dirname(__file__)), "_support")
config_file = os.path.abspath(os.path.join(support, "config.yml"))

# TODO: move invoke's support_path + load + etc somewhere importable? or into
# pytest-relaxed, despite it not being strictly related to that feature set?
# ugh
@contextmanager
def support_path():
    sys.path.insert(0, support)
    try:
        yield
    finally:
        sys.path.pop(0)


def load(name):
    with support_path():
        imported = __import__(name)
        return imported


# TODO: this could become a fixture in conftest.py, presumably, and just yield
# stdout, allowing the tests themselves to assert more naturally
@trap
def expect(invocation, out, program=None, test="equals"):
    if program is None:
        program = make_program()
    program.run("fab {}".format(invocation), exit=False)
    output = sys.stdout.getvalue()
    if test == "equals":
        assert output == out
    elif test == "contains":
        assert out in output
    elif test == "regex":
        assert re.match(out, output)
    else:
        err = "Don't know how to expect that <stdout> {} <expected>!"
        assert False, err.format(test)
    # Safety check: no stderr
    assert not sys.stderr.getvalue()


def faux_v1_env():
    # Close enough to v1 _AttributeDict...
    # Contains a copy of enough of v1's defaults to prevent us having to do a
    # lot of extra .get()s...meh
    return Lexicon(
        always_use_pty=True,
        forward_agent=False,
        gateway=None,
        host_string="localghost",
        key_filename=None,
        no_agent=False,
        password=None,
        port=22,
        ssh_config_path=None,
        # Used in a handful of safety tests, so it gets a 'real' value. eh.
        sudo_password="nope",
        sudo_prompt=None,
        timeout=None,
        use_ssh_config=False,
        user="localuser",
        warn_only=False,
    )