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']