1from __future__ import absolute_import
2
3import uuid
4
5import pytest
6from py._path.common import fspath
7
8import mozcrash
9
10
11@pytest.fixture(scope='session')
12def stackwalk(tmpdir_factory):
13    stackwalk = tmpdir_factory.mktemp('stackwalk_binary').join('stackwalk')
14    stackwalk.write('fake binary')
15    stackwalk.chmod(0o744)
16    return stackwalk
17
18
19@pytest.fixture
20def check_for_crashes(tmpdir, stackwalk, monkeypatch):
21    monkeypatch.delenv('MINIDUMP_SAVE_PATH', raising=False)
22
23    def wrapper(dump_directory=fspath(tmpdir),
24                symbols_path='symbols_path',
25                stackwalk_binary=fspath(stackwalk),
26                dump_save_path=None,
27                test_name=None,
28                quiet=True):
29        return mozcrash.check_for_crashes(dump_directory,
30                                          symbols_path,
31                                          stackwalk_binary,
32                                          dump_save_path,
33                                          test_name,
34                                          quiet)
35
36    return wrapper
37
38
39@pytest.fixture
40def check_for_java_exception():
41
42    def wrapper(logcat=None,
43                test_name=None,
44                quiet=True):
45        return mozcrash.check_for_java_exception(logcat,
46                                                 test_name,
47                                                 quiet)
48
49    return wrapper
50
51
52@pytest.fixture
53def minidump_files(request, tmpdir):
54    files = []
55
56    for i in range(getattr(request, 'param', 1)):
57        name = uuid.uuid4()
58
59        dmp = tmpdir.join('{}.dmp'.format(name))
60        dmp.write('foo')
61
62        extra = tmpdir.join('{}.extra'.format(name))
63        extra.write('bar')
64
65        files.append({'dmp': dmp, 'extra': extra})
66
67    return files
68
69
70@pytest.fixture(autouse=True)
71def mock_popen(monkeypatch):
72    """Generate a class that can mock subprocess.Popen.
73
74    :param stdouts: Iterable that should return an iterable for the
75                    stdout of each process in turn.
76    """
77    class MockPopen(object):
78        def __init__(self, args, *args_rest, **kwargs):
79            # all_popens.append(self)
80            self.args = args
81            self.returncode = 0
82
83        def communicate(self):
84            return (u'Stackwalk command: {}'.format(" ".join(self.args)), "")
85
86        def wait(self):
87            return self.returncode
88
89    monkeypatch.setattr(mozcrash.mozcrash.subprocess, 'Popen', MockPopen)
90