summaryrefslogtreecommitdiff
path: root/tests/__init__.py
blob: ed6ac18ac3b69b56a631ae9f12f48808722104bb (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
import os
import subprocess


class TestRepo(object):
    '''Simple class to make it easy to create/manipulate test repos'''

    @staticmethod
    def create(path):
        os.mkdir(path)
        subprocess.check_call(['git', 'init'], cwd=path)
        return TestRepo(path)

    @staticmethod
    def clone(repo_url, path, mirror=False):
        args = ['git', 'clone']
        if mirror:
            args.append('--mirror')
        args.append(repo_url)
        args.append(path)
        subprocess.check_call(args)
        return TestRepo(path)

    def __init__(self, path):
        self.path = path

    def add_commit(self, fname, content, message):
        with open(os.path.join(self.path, fname), 'w') as f:
            f.write(content)
        subprocess.check_call(['git', 'add', '.'], cwd=self.path)
        subprocess.check_call(
            ['git', 'commit', '-a', '-m', message], cwd=self.path)
        return self.last_commit()

    def run_command(self, args):
        subprocess.check_call(args, cwd=self.path)

    def last_commit(self):
        out = subprocess.check_output(
            ['git', 'log', '--format=oneline', '-1'], cwd=self.path)
        return out.split(' ')[0]

    def create_patch(self, commit):
        patch = subprocess.check_output(
            ['git', 'format-patch', '%s^..%s' % (commit, commit)],
            cwd=self.path)
        return os.path.join(self.path, patch.strip())