summaryrefslogtreecommitdiff
path: root/yarns/900-implements.yarn
blob: ab3c44cdb8ac174dfc9a4a622b2048c5d2b6b83e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Step implementations
====================

This chapter contains implementations of each step. See the [yarn]
manual for details and examples.


Running distix
--------------

We need a way for the steps to run distix with various command line
arguments. Instead of having a different IMPLEMENTS step for each
command that needs to be run, we'll have one fairly generic one. It'll
allow the step to provide the command line arguments, but then also
adds in options and things to make distix be isolated from, say,
global configuration etc.

User needs to change directory. Since each step is run in a new shell,
we do this by keeping track ourselves of where the scenario wants to
be, and changing there when running various commands.

    IMPLEMENTS WHEN user changes working directory to (\S+)
    newdir = get_next_match()
    olddir = vars['cwd'] or '.'
    vars['cwd'] = os.path.abspath(os.path.join(datadir, olddir, newdir))
    print os.listdir('.')
    runcmd(['pwd'], cwd=vars['cwd'])

    IMPLEMENTS WHEN user attempts to run distix ([^$]*)
    args = get_next_match()
    configure_git()
    run_distix(args.split())

    IMPLEMENTS WHEN user attempts to run distix show \$PREFIX
    configure_git()
    run_distix(['show', vars['tid-prefix']])

    IMPLEMENTS WHEN user sets all tickets in (\S+) to (\S+)
    repo = get_next_match()
    keyvalue = get_next_match()
    run_distix(['list'], cwd=repo)
    list_output = vars['stdout']
    tickets = [
        line.split()[1]
        for line in list_output.splitlines()
        if line.startswith('    ')
    ]
    run_distix(['set', keyvalue] + tickets)

We also need steps for inspecting the results of attempting to run
distix.

    IMPLEMENTS THEN attempt succeeded
    print 'stdout:', repr(vars['stdout'])
    print 'stderr:', repr(vars['stderr'])
    print 'exit:', vars['exit']
    assert vars['exit'] == 0

    IMPLEMENTS THEN output is "(.*)"
    wanted = unescape(get_next_match())
    output = vars['stdout']
    print 'wanted:', repr(wanted)
    print 'output:', repr(output)
    assert wanted == unescape(vars['stdout'])

    IMPLEMENTS THEN output matches "(.*)"
    import re
    pattern = get_next_match().decode('utf8')
    print 'stdout:', repr(vars['stdout'])
    print 'pattern:', repr(pattern)
    assert re.search(pattern, vars['stdout'], re.M) is not None

    IMPLEMENTS THEN output doesn't match "(.*)"
    import re
    pattern = get_next_match()
    output = unicode(vars['stdout'], 'utf8')
    print 'pattern:', repr(pattern)
    print 'stdout:', repr(output)
    assert re.search(pattern, output) is None

    IMPLEMENTS THEN first ticket id is captured as \$TID
    # We assume previous command run was distix list.
    list_output = vars['stdout']
    tickets = [
        line.split()[1]
        for line in list_output.splitlines()
        if line.startswith('    ')
    ]
    vars['tickets'] = tickets
    vars['ticket-id-1'] = tickets[0]

    IMPLEMENTS THEN attempt failed
    assert vars['exit'] != 0

    IMPLEMENTS THEN error message matches (.*)
    import re
    pattern = get_next_match()
    assert re.search(pattern, vars['stderr']) is not None


File creation
-------------

We need to provide a way for scenarios to create files with known
content.

    IMPLEMENTS GIVEN file (\S+) containing "(.*)"
    filename = get_next_match()
    content = get_next_match()
    write_file(filename, unescape(content).encode('utf8'))


Maildir creation
----------------

We need to create a maildir with mails in it.

    IMPLEMENTS GIVEN maildir (\S+) containing a mail with subject "(.+)"
    dirname = get_next_match()
    subject = get_next_match()
    mkmaildir(dirname)
    mailfile = os.path.join(dirname, 'new', 'msg')
    write_file(mailfile, """\
    From: user@example.com
    Subject: {subject}

    Message body goes here.
    """.format(subject=subject).encode('utf8'))

File tests
-----------

Does a file or directory exist?

    IMPLEMENTS THEN (\S+) exists
    filename = get_next_match()
    assert os.path.exists(filename)

Repository/ticket tests
-----------------------

How many tickets does a repository have?

    IMPLEMENTS THEN repository (\S+) has (\d+) tickets?
    repo = get_next_match()
    count = int(get_next_match())
    ticketdir = os.path.join(repo, 'tickets')
    assert len(os.listdir(ticketdir)) == count

How many copies of a given message does a repository have? In any
tickets?

    IMPLEMENTS THEN repository (\S+) has one copy of message in (\S+)
    repo = get_next_match()
    msgfile = get_next_match()
    msg = cat_file(msgfile)
    tickets = os.path.join(repo, 'tickets')
    for dirname, subdirs, basenames in os.walk(repo):
        for basename in basenames:
            filename = os.path.join(dirname, basename)
            candidate = cat_file(filename)
            if candidate == msg:
                sys.exit(0)
    sys.exit(1)

Git operations
--------------

Clone a repository.

    IMPLEMENTS WHEN user clones (\S+) to (\S+)
    url = get_next_match()
    dirname = get_next_match()
    print repr(vars['cwd'])
    print repr(vars._dict)
    print runcmd(['pwd'])
    print os.listdir('.')
    print os.listdir(url)
    print repr(['git', 'clone', url, dirname])
    print runcmd(['git', 'clone', url, dirname])
    print runcmd(['pwd'], cwd=dirname)



Check that everything is in git.

    IMPLEMENTS THEN everything in (\S+) is committed to git
    ex, out, err = runcmd(['git', 'status', '--short'])
    print 'stdout:', repr(out)
    assert out == ''


Ticket id manipulation
----------------------

    IMPLEMENTS THEN ticket id \$TID 7-nybble prefix is \$PREFIX
    vars['tid-prefix'] = vars['ticket-id-1'][:7]

    IMPLEMENTS THEN output contains ticket id \$TID
    assert vars['ticket-id-1'] in vars['stdout']