summaryrefslogtreecommitdiff
path: root/files.py
blob: 1a7451f8d225aa9d871b571265b5b21876d6536d (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
# Some generic file handling step implementations.

import os
import re
import time


# Create a file on disk from an embedded file.
def create_file(ctx, filename=None):
    with open(filename, "wb") as f:
        f.write(get_file(filename))


# Update mtime of a file in datadir to a specific time.
def touch_file(ctx, filename=None, y=None, mon=None, d=None, h=None, min=None, s=None):
    t = (int(y), int(mon), int(d), int(h), int(min), int(s), -1, -1, -1)
    ts = time.mktime(t)
    os.utime(filename, times=(ts, ts))


# Update mtime of a file in datadir to current time.
def update_mtime(ctx, filename=None):
    os.utime(filename)


# Check that a file exists.
def file_exists(ctx, filename=None):
    assert_eq(os.path.exists(filename), True)


# Check that a file does not exist.
def file_does_not_exist(ctx, filename=None):
    assert_eq(os.path.exists(filename), False)


# Check that only specific files exist in the current working directory.
def only_these_files_exist(ctx, filenames=None):
    filenames = filenames.replace(",", "").split()
    assert_eq(set(os.listdir(".")), set(filenames))


# Check that a file contents match a regex.
def file_matches(ctx, filename=None, regex=None):
    with open(filename) as f:
        content = f.read()
    m = re.search(regex, content)
    if m is None:
        print("content:", repr(content))
        print("regex:", repr(regex))
    assert_eq(bool(m), True)


# Check that two files have the same content
def files_match(ctx, filename1=None, filename2=None):
    with open(filename1, "rb") as f:
        data1 = f.read()
    with open(filename2, "rb") as f:
        data2 = f.read()
    assert_eq(data1, data2)


# Check that a file contains a fixed string.
def file_contains(ctx, filename=None, pattern=None):
    with open(filename) as f:
        content = f.read()
    assert_eq(pattern in content, True)


# Retrieve metadata for a file as a struct so it can be easily inspected.
def _get_metadata(filename):
    st = os.lstat(filename)
    keys = ["st_dev", "st_gid", "st_ino", "st_mode", "st_mtime", "st_size", "st_uid"]
    return {key: getattr(st, key) for key in keys}


# Store in the context current metdata for a file.
def remember_metadata(ctx, filename=None):
    meta = ctx.get("remembered-metadata", {})
    meta[filename] = _get_metadata(filename)
    ctx["remembered-metadata"] = meta


# Check that current metadata of a file is as stored in the context.
def has_same_metadata_as_remembered(ctx, filename=None):
    assert_eq(ctx["remembered-metadata"][filename], _get_metadata(filename))


# Check that current metadata of a file is different than stored in the context.
def has_diff_metadata_than_remembered(ctx, filename=None):
    assert_ne(ctx["remembered-metadata"][filename], _get_metadata(filename))