summaryrefslogtreecommitdiff
path: root/subplot.py
blob: 7d9544b940f2c36c029f89dca42628e55e02ed22 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import json
import os
import shutil
import tempfile


# A shell script to run the sp-codegen binary from the source directory's Rust
# build target directory, adding the -t option to use the source directory's
# template directory.
#
# This is a string template where the source directory is filled in when used.
wrapper = """\
#!/bin/sh
set -eu
exec '{srcdir}/target/debug/{prog}' "$@" --resources '{srcdir}/share'
"""


def install_subplot(ctx):
    runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"]
    srcdir = globals()["srcdir"]

    # Create a "bin" directory for the wrapper. Can't put this into datadir, as
    # some of Subplot's scenarios make assumptions on what files exist there.
    bindir = ctx["bin-dir"] = tempfile.mkdtemp()
    for prog in ["sp-codegen", "sp-docgen"]:
        filename = os.path.join(bindir, prog)
        with open(filename, "w") as f:
            f.write(wrapper.format(prog=prog, srcdir=srcdir))
        os.chmod(filename, 0o755)

    # Add the temporary bin directory to the path, and then the source
    # directory's Rust binary target directory.
    runcmd_prepend_to_path(ctx, dirname=bindir)
    runcmd_prepend_to_path(ctx, dirname=os.path.join(srcdir, "target", "debug"))


def uninstall_subplot(ctx):
    shutil.rmtree(ctx["bin-dir"])


def scenario_was_run(ctx, name=None):
    runcmd_stdout_contains = globals()["runcmd_stdout_contains"]
    runcmd_stdout_contains(ctx, text="\nscenario: {}\n".format(name))


def scenario_was_not_run(ctx, name=None):
    runcmd_stdout_doesnt_contain = globals()["runcmd_stdout_doesnt_contain"]
    runcmd_stdout_doesnt_contain(ctx, text="\nscenario: {}\n".format(name))


def step_was_run(ctx, keyword=None, name=None):
    runcmd_stdout_contains = globals()["runcmd_stdout_contains"]
    runcmd_stdout_contains(ctx, text="\n  step: {} {}\n".format(keyword, name))


def step_was_run_and_then(ctx, keyword1=None, name1=None, keyword2=None, name2=None):
    runcmd_stdout_contains = globals()["runcmd_stdout_contains"]
    runcmd_stdout_contains(
        ctx,
        text="\n  step: {} {}\n  step: {} {}".format(keyword1, name1, keyword2, name2),
    )


def cleanup_was_run(ctx, keyword1=None, name1=None, keyword2=None, name2=None):
    runcmd_stdout_contains = globals()["runcmd_stdout_contains"]
    runcmd_stdout_contains(
        ctx,
        text="\n  cleanup: {} {}\n  cleanup: {} {}\n".format(
            keyword1, name1, keyword2, name2
        ),
    )


def cleanup_was_not_run(ctx, keyword=None, name=None):
    runcmd_stdout_doesnt_contain = globals()["runcmd_stdout_doesnt_contain"]
    runcmd_stdout_doesnt_contain(ctx, text="\n  cleanup: {} {}\n".format(keyword, name))


def json_output_matches_file(ctx, filename=None):
    assert_dict_eq = globals()["assert_dict_eq"]

    ns = ctx.declare("_runcmd")
    actual = json.loads(ns["stdout"])
    expected = json.load(open(filename))
    assert_dict_eq(actual, expected)


def file_ends_in_zero_newlines(ctx, filename=None):
    assert_ne = globals()["assert_ne"]

    content = open(filename, "r").read()
    print("content:", repr(content))
    assert_ne(content[-1], "\n")


def file_ends_in_one_newline(ctx, filename=None):
    assert_eq = globals()["assert_eq"]
    assert_ne = globals()["assert_ne"]

    content = open(filename, "r").read()
    print("content:", repr(content))
    assert_eq(content[-1], "\n")
    assert_ne(content[-2], "\n")


def file_ends_in_two_newlines(ctx, filename=None):
    assert_eq = globals()["assert_eq"]

    content = open(filename, "r").read()
    print("content:", repr(content))
    assert_eq(content[-2:], "\n\n")


def binary(basename):
    srcdir = globals()["srcdir"]
    return os.path.join(srcdir, "target", "debug", basename)