summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/python/test_create_directories.py
blob: ffbed625a52f877e85106cc3520dc83f8585fe5f (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import unittest.mock as mock
from argparse import Namespace
from collections import defaultdict
from textwrap import dedent

import mozunit
import pytest
import six
from conftest import setup_args
from manifestparser import TestManifest


# Directly running runTests() is likely not working nor a good idea
# So at least we try to minimize with just:
#  - getActiveTests()
#  - create manifests list
#  - parseAndCreateTestsDirs()
#
# Hopefully, breaking the runTests() calls to parseAndCreateTestsDirs() will
# anyway trigger other tests failures so it would be spotted, and we at least
# ensure some coverage of handling the manifest content, creation of the
# directories and cleanup
@pytest.fixture
def prepareRunTests(setup_test_harness, parser):
    setup_test_harness(*setup_args)
    runtests = pytest.importorskip("runtests")
    md = runtests.MochitestDesktop("plain", {"log_tbpl": "-"})

    options = vars(parser.parse_args([]))

    def inner(**kwargs):
        opts = options.copy()
        opts.update(kwargs)

        manifest = opts.get("manifestFile")
        if isinstance(manifest, six.string_types):
            md.testRootAbs = os.path.dirname(manifest)
        elif isinstance(manifest, TestManifest):
            md.testRootAbs = manifest.rootdir

        md._active_tests = None
        md.prefs_by_manifest = defaultdict(set)
        tests = md.getActiveTests(Namespace(**opts))
        manifests = set(t["manifest"] for t in tests)
        for m in sorted(manifests):
            md.parseAndCreateTestsDirs(m)
        return md

    return inner


@pytest.fixture
def create_manifest(tmpdir, build_obj):
    def inner(string, name="manifest.ini"):
        manifest = tmpdir.join(name)
        manifest.write(string, ensure=True)
        # pylint --py3k: W1612
        path = six.text_type(manifest)
        return TestManifest(manifests=(path,), strict=False, rootdir=tmpdir.strpath)

    return inner


def create_manifest_empty(create_manifest):
    manifest = create_manifest(
        dedent(
            """
    [DEFAULT]
    [files/test_pass.html]
    [files/test_fail.html]
    """
        )
    )

    return {
        "runByManifest": True,
        "manifestFile": manifest,
    }


def create_manifest_one(create_manifest):
    manifest = create_manifest(
        dedent(
            """
    [DEFAULT]
    test-directories =
        .snap_firefox_current_real
    [files/test_pass.html]
    [files/test_fail.html]
    """
        )
    )

    return {
        "runByManifest": True,
        "manifestFile": manifest,
    }


def create_manifest_mult(create_manifest):
    manifest = create_manifest(
        dedent(
            """
    [DEFAULT]
    test-directories =
        .snap_firefox_current_real
        .snap_firefox_current_real2
    [files/test_pass.html]
    [files/test_fail.html]
    """
        )
    )

    return {
        "runByManifest": True,
        "manifestFile": manifest,
    }


def test_no_entry(prepareRunTests, create_manifest):
    options = create_manifest_empty(create_manifest)
    with mock.patch("os.makedirs") as mock_os_makedirs:
        _ = prepareRunTests(**options)
        mock_os_makedirs.assert_not_called()


def test_one_entry(prepareRunTests, create_manifest):
    options = create_manifest_one(create_manifest)
    with mock.patch("os.makedirs") as mock_os_makedirs:
        md = prepareRunTests(**options)
        mock_os_makedirs.assert_called_once_with(".snap_firefox_current_real")

        opts = mock.Mock(pidFile="")  # so cleanup() does not check it
        with mock.patch("os.path.exists") as mock_os_path_exists, mock.patch(
            "shutil.rmtree"
        ) as mock_shutil_rmtree:
            md.cleanup(opts, False)
            mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
            mock_shutil_rmtree.assert_called_once_with(".snap_firefox_current_real")


def test_one_entry_already_exists(prepareRunTests, create_manifest):
    options = create_manifest_one(create_manifest)
    with mock.patch(
        "os.path.exists", return_value=True
    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
        with pytest.raises(FileExistsError):
            _ = prepareRunTests(**options)
        mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
        mock_os_makedirs.assert_not_called()


def test_mult_entry(prepareRunTests, create_manifest):
    options = create_manifest_mult(create_manifest)
    with mock.patch("os.makedirs") as mock_os_makedirs:
        md = prepareRunTests(**options)
        assert mock_os_makedirs.call_count == 2
        mock_os_makedirs.assert_has_calls(
            [
                mock.call(".snap_firefox_current_real"),
                mock.call(".snap_firefox_current_real2"),
            ]
        )

        opts = mock.Mock(pidFile="")  # so cleanup() does not check it
        with mock.patch("os.path.exists") as mock_os_path_exists, mock.patch(
            "shutil.rmtree"
        ) as mock_shutil_rmtree:
            md.cleanup(opts, False)

            assert mock_os_path_exists.call_count == 2
            mock_os_path_exists.assert_has_calls(
                [
                    mock.call(".snap_firefox_current_real"),
                    mock.call().__bool__(),
                    mock.call(".snap_firefox_current_real2"),
                    mock.call().__bool__(),
                ]
            )

            assert mock_os_makedirs.call_count == 2
            mock_shutil_rmtree.assert_has_calls(
                [
                    mock.call(".snap_firefox_current_real"),
                    mock.call(".snap_firefox_current_real2"),
                ]
            )


def test_mult_entry_one_already_exists(prepareRunTests, create_manifest):
    options = create_manifest_mult(create_manifest)
    with mock.patch(
        "os.path.exists", side_effect=[True, False]
    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
        with pytest.raises(FileExistsError):
            _ = prepareRunTests(**options)
        mock_os_path_exists.assert_called_once_with(".snap_firefox_current_real")
        mock_os_makedirs.assert_not_called()

    with mock.patch(
        "os.path.exists", side_effect=[False, True]
    ) as mock_os_path_exists, mock.patch("os.makedirs") as mock_os_makedirs:
        with pytest.raises(FileExistsError):
            _ = prepareRunTests(**options)
        assert mock_os_path_exists.call_count == 2
        mock_os_path_exists.assert_has_calls(
            [
                mock.call(".snap_firefox_current_real"),
                mock.call(".snap_firefox_current_real2"),
            ]
        )
        mock_os_makedirs.assert_not_called()


if __name__ == "__main__":
    mozunit.main()