summaryrefslogtreecommitdiff
path: root/jt2.py
blob: 7d5304e22536b43469c4046d7eabe1183dd2db6a (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
# This defines the functions that implement each step. Functions
# return either a saga.Success or a saga.Failure object. These are
# subclasses of dict, and the runner generated by Saga will abort if a
# step returns failure. Either dict may contain arbitrary fields.
#
# For simplicity, the runner will capture exceptions, and treats them
# as failures with the exception stored within it. Also, it treats a
# None return value as success with no keys.
#
# Values may be saved in named variables with the saga.set_variable
# function and retrieved with the saga.get_variable function. It's an
# error to retrieve an unset variable.


import os

import saga


# This either returns None or raises an exception.
def create_empty_git_repository():
    subprocess.check-call(['git', 'init', '.'], stderr=subprocess.STDOUT)


# This either returns None or raises an exception.
def only_filename(filename):
    assert os.listdir('.') == [filename]


# This run jt, with the --date= option that tells jt to pretend the
# current date is as given. The runner sets PATH to have the source
# directory of the project being tested.
def run_jt(cmd):
    prefix = 'jt '
    assert cmd.startswith(prefix)
    args = cmd[len(prefix:)]

    date = saga.get_variable('date')
    shellcmd = "jt --date={} {}".format(date, args)

    exitcode, output = subprocess.getstatusoutput(
        ['sh', '-c', shellcmd], stderr=subprocess.STDOUT)

    # We always return success, and expect the result to be checked by
    # a later step.
    return saga.Success(exitcode=exitcode, output=output)


def exists(filename):
    assert os.path.exists(filename)


def file_contains(filename, text):
    content = open(filename).read()
    assert text in content


def shell_command_failed(captured_result=None):
    exitcode = captured['exitcode']
    assert exitcode == 0


def stderr_contains(error, captured_result=None):
    stderr = captured_result['stderr']
    assert error in stderr


# These need to be passed to jt somehow, not sure how. That's not
# important for now.
def set_date(date):
    saga.save_variable('date', date)