summaryrefslogtreecommitdiff
path: root/subplot/summain.py
blob: 4bde65c5252e075582cd2dbf3914b6d79ccbea7e (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
import difflib
import logging
import os
import socket


def install_summain(ctx):
    runcmd_prepend_to_path = globals()["runcmd_prepend_to_path"]
    srcdir = globals()["srcdir"]
    bindir = os.path.join(srcdir, "target", "debug")
    runcmd_prepend_to_path(ctx, dirname=bindir)


def create_directory(ctx, dirname=None):
    os.mkdir(dirname)


def create_file(ctx, filename=None):
    open(filename, "w").close()


def create_symlink(ctx, linkname=None, target=None):
    os.symlink(target, linkname)


def create_socket(ctx, filename=None):
    fd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    fd.bind(filename)


def create_fifo(ctx, filename=None):
    os.mkfifo(filename)


def set_mode(ctx, filename=None, mode=None):
    os.chmod(filename, int(mode, 8))


def set_atime(ctx, filename=None, timestamp=None):
    st = os.lstat(filename)
    os.utime(filename, (int(timestamp), int(st.st_mtime)), follow_symlinks=False)


def set_mtime(ctx, filename=None, timestamp=None):
    st = os.lstat(filename)
    os.utime(filename, (int(st.st_atime), int(timestamp)), follow_symlinks=False)


def output_matches_file(ctx, filename=None):
    runcmd_get_stdout = globals()["runcmd_get_stdout"]
    get_file = globals()["get_file"]
    assert_eq = globals()["assert_eq"]

    actual = runcmd_get_stdout(ctx)
    expected = get_file(filename).decode("UTF-8")

    diff = "".join(
        line.rstrip("\n") + "\n"
        for line in difflib.unified_diff(
            expected.splitlines(),
            actual.splitlines(),
            fromfile="expected",
            tofile="actual",
        )
    )
    logging.debug("output_matches:")
    logging.debug(f"  actual:\n{actual}")
    logging.debug(f"  expect:\n{expected}")
    logging.debug(f"  diff:\n{diff}")
    if actual != expected:
        print("ERROR: diff:")
        print(diff)
    assert_eq(actual, expected)