| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import tempfile |
| 10 |
import unittest |
| 11 |
|
| 12 |
import mozunit |
| 13 |
from manifestparser import ManifestParser, convert |
| 14 |
|
| 15 |
here = os.path.dirname(os.path.abspath(__file__)) |
| 16 |
|
| 17 |
# In some cases tempfile.mkdtemp() may returns a path which contains |
| 18 |
# symlinks. Some tests here will then break, as the manifestparser.convert |
| 19 |
# function returns paths that does not contains symlinks. |
| 20 |
# |
| 21 |
# Workaround is to use the following function, if absolute path of temp dir |
| 22 |
# must be compared. |
| 23 |
|
| 24 |
|
| 25 |
def create_realpath_tempdir(): |
| 26 |
""" |
| 27 |
Create a tempdir without symlinks. |
| 28 |
""" |
| 29 |
return os.path.realpath(tempfile.mkdtemp()) |
| 30 |
|
| 31 |
|
| 32 |
class TestDirectoryConversion(unittest.TestCase): |
| 33 |
"""test conversion of a directory tree to a manifest structure""" |
| 34 |
|
| 35 |
def create_stub(self, directory=None): |
| 36 |
"""stub out a directory with files in it""" |
| 37 |
|
| 38 |
files = ("foo", "bar", "fleem") |
| 39 |
if directory is None: |
| 40 |
directory = create_realpath_tempdir() |
| 41 |
for i in files: |
| 42 |
open(os.path.join(directory, i), "w").write(i) |
| 43 |
subdir = os.path.join(directory, "subdir") |
| 44 |
os.mkdir(subdir) |
| 45 |
open(os.path.join(subdir, "subfile"), "w").write("baz") |
| 46 |
return directory |
| 47 |
|
| 48 |
def test_directory_to_manifest(self): |
| 49 |
""" |
| 50 |
Test our ability to convert a static directory structure to a |
| 51 |
manifest. |
| 52 |
""" |
| 53 |
|
| 54 |
# create a stub directory |
| 55 |
stub = self.create_stub() |
| 56 |
try: |
| 57 |
stub = stub.replace(os.path.sep, "/") |
| 58 |
self.assertTrue(os.path.exists(stub) and os.path.isdir(stub)) |
| 59 |
|
| 60 |
# Make a manifest for it |
| 61 |
manifest = convert([stub]) |
| 62 |
out_tmpl = """[%(stub)s/bar] |
| 63 |
|
| 64 |
[%(stub)s/fleem] |
| 65 |
|
| 66 |
[%(stub)s/foo] |
| 67 |
|
| 68 |
[%(stub)s/subdir/subfile] |
| 69 |
|
| 70 |
""" # noqa |
| 71 |
self.assertEqual(str(manifest), out_tmpl % dict(stub=stub)) |
| 72 |
except BaseException: |
| 73 |
raise |
| 74 |
finally: |
| 75 |
shutil.rmtree(stub) # cleanup |
| 76 |
|
| 77 |
def test_convert_directory_manifests_in_place(self): |
| 78 |
""" |
| 79 |
keep the manifests in place |
| 80 |
""" |
| 81 |
|
| 82 |
stub = self.create_stub() |
| 83 |
try: |
| 84 |
ManifestParser.populate_directory_manifests([stub], filename="manifest.ini") |
| 85 |
self.assertEqual( |
| 86 |
sorted(os.listdir(stub)), |
| 87 |
["bar", "fleem", "foo", "manifest.ini", "subdir"], |
| 88 |
) |
| 89 |
parser = ManifestParser() |
| 90 |
parser.read(os.path.join(stub, "manifest.ini")) |
| 91 |
self.assertEqual( |
| 92 |
[i["name"] for i in parser.tests], ["subfile", "bar", "fleem", "foo"] |
| 93 |
) |
| 94 |
parser = ManifestParser() |
| 95 |
parser.read(os.path.join(stub, "subdir", "manifest.ini")) |
| 96 |
self.assertEqual(len(parser.tests), 1) |
| 97 |
self.assertEqual(parser.tests[0]["name"], "subfile") |
| 98 |
except BaseException: |
| 99 |
raise |
| 100 |
finally: |
| 101 |
shutil.rmtree(stub) |
| 102 |
|
| 103 |
def test_convert_directory_manifests_in_place_toml(self): |
| 104 |
""" |
| 105 |
keep the manifests in place (TOML) |
| 106 |
""" |
| 107 |
|
| 108 |
stub = self.create_stub() |
| 109 |
try: |
| 110 |
ManifestParser.populate_directory_manifests([stub], filename="manifest.ini") |
| 111 |
self.assertEqual( |
| 112 |
sorted(os.listdir(stub)), |
| 113 |
["bar", "fleem", "foo", "manifest.ini", "subdir"], |
| 114 |
) |
| 115 |
parser = ManifestParser(use_toml=True) |
| 116 |
parser.read(os.path.join(stub, "manifest.ini")) |
| 117 |
self.assertEqual( |
| 118 |
[i["name"] for i in parser.tests], ["subfile", "bar", "fleem", "foo"] |
| 119 |
) |
| 120 |
parser = ManifestParser(use_toml=True) |
| 121 |
parser.read(os.path.join(stub, "subdir", "manifest.ini")) |
| 122 |
self.assertEqual(len(parser.tests), 1) |
| 123 |
self.assertEqual(parser.tests[0]["name"], "subfile") |
| 124 |
except BaseException: |
| 125 |
raise |
| 126 |
finally: |
| 127 |
shutil.rmtree(stub) |
| 128 |
|
| 129 |
def test_manifest_ignore(self): |
| 130 |
"""test manifest `ignore` parameter for ignoring directories""" |
| 131 |
|
| 132 |
stub = self.create_stub() |
| 133 |
try: |
| 134 |
ManifestParser.populate_directory_manifests( |
| 135 |
[stub], filename="manifest.ini", ignore=("subdir",) |
| 136 |
) |
| 137 |
parser = ManifestParser(use_toml=False) |
| 138 |
parser.read(os.path.join(stub, "manifest.ini")) |
| 139 |
self.assertEqual([i["name"] for i in parser.tests], ["bar", "fleem", "foo"]) |
| 140 |
self.assertFalse( |
| 141 |
os.path.exists(os.path.join(stub, "subdir", "manifest.ini")) |
| 142 |
) |
| 143 |
except BaseException: |
| 144 |
raise |
| 145 |
finally: |
| 146 |
shutil.rmtree(stub) |
| 147 |
|
| 148 |
def test_manifest_ignore_toml(self): |
| 149 |
"""test manifest `ignore` parameter for ignoring directories (TOML)""" |
| 150 |
|
| 151 |
stub = self.create_stub() |
| 152 |
try: |
| 153 |
ManifestParser.populate_directory_manifests( |
| 154 |
[stub], filename="manifest.ini", ignore=("subdir",) |
| 155 |
) |
| 156 |
parser = ManifestParser(use_toml=True) |
| 157 |
parser.read(os.path.join(stub, "manifest.ini")) |
| 158 |
self.assertEqual([i["name"] for i in parser.tests], ["bar", "fleem", "foo"]) |
| 159 |
self.assertFalse( |
| 160 |
os.path.exists(os.path.join(stub, "subdir", "manifest.ini")) |
| 161 |
) |
| 162 |
except BaseException: |
| 163 |
raise |
| 164 |
finally: |
| 165 |
shutil.rmtree(stub) |
| 166 |
|
| 167 |
def test_pattern(self): |
| 168 |
"""test directory -> manifest with a file pattern""" |
| 169 |
|
| 170 |
stub = self.create_stub() |
| 171 |
try: |
| 172 |
parser = convert([stub], pattern="f*", relative_to=stub) |
| 173 |
self.assertEqual([i["name"] for i in parser.tests], ["fleem", "foo"]) |
| 174 |
|
| 175 |
# test multiple patterns |
| 176 |
parser = convert([stub], pattern=("f*", "s*"), relative_to=stub) |
| 177 |
self.assertEqual( |
| 178 |
[i["name"] for i in parser.tests], ["fleem", "foo", "subdir/subfile"] |
| 179 |
) |
| 180 |
except BaseException: |
| 181 |
raise |
| 182 |
finally: |
| 183 |
shutil.rmtree(stub) |
| 184 |
|
| 185 |
def test_update(self): |
| 186 |
""" |
| 187 |
Test our ability to update tests from a manifest and a directory of |
| 188 |
files |
| 189 |
""" |
| 190 |
|
| 191 |
# boilerplate |
| 192 |
tempdir = create_realpath_tempdir() |
| 193 |
for i in range(10): |
| 194 |
open(os.path.join(tempdir, str(i)), "w").write(str(i)) |
| 195 |
|
| 196 |
# otherwise empty directory with a manifest file |
| 197 |
newtempdir = create_realpath_tempdir() |
| 198 |
manifest_file = os.path.join(newtempdir, "manifest.ini") |
| 199 |
manifest_contents = str(convert([tempdir], relative_to=tempdir)) |
| 200 |
with open(manifest_file, "w") as f: |
| 201 |
f.write(manifest_contents) |
| 202 |
|
| 203 |
# get the manifest |
| 204 |
manifest = ManifestParser(manifests=(manifest_file,), use_toml=False) |
| 205 |
|
| 206 |
# All of the tests are initially missing: |
| 207 |
paths = [str(i) for i in range(10)] |
| 208 |
self.assertEqual([i["name"] for i in manifest.missing()], paths) |
| 209 |
|
| 210 |
# But then we copy one over: |
| 211 |
self.assertEqual(manifest.get("name", name="1"), ["1"]) |
| 212 |
manifest.update(tempdir, name="1") |
| 213 |
self.assertEqual(sorted(os.listdir(newtempdir)), ["1", "manifest.ini"]) |
| 214 |
|
| 215 |
# Update that one file and copy all the "tests": |
| 216 |
open(os.path.join(tempdir, "1"), "w").write("secret door") |
| 217 |
manifest.update(tempdir) |
| 218 |
self.assertEqual( |
| 219 |
sorted(os.listdir(newtempdir)), |
| 220 |
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "manifest.ini"], |
| 221 |
) |
| 222 |
self.assertEqual( |
| 223 |
open(os.path.join(newtempdir, "1")).read().strip(), "secret door" |
| 224 |
) |
| 225 |
|
| 226 |
# clean up: |
| 227 |
shutil.rmtree(tempdir) |
| 228 |
shutil.rmtree(newtempdir) |
| 229 |
|
| 230 |
def test_update_toml(self): |
| 231 |
""" |
| 232 |
Test our ability to update tests from a manifest and a directory of |
| 233 |
files (TOML) |
| 234 |
""" |
| 235 |
|
| 236 |
# boilerplate |
| 237 |
tempdir = create_realpath_tempdir() |
| 238 |
for i in range(10): |
| 239 |
open(os.path.join(tempdir, str(i)), "w").write(str(i)) |
| 240 |
|
| 241 |
# otherwise empty directory with a manifest file |
| 242 |
newtempdir = create_realpath_tempdir() |
| 243 |
manifest_file = os.path.join(newtempdir, "manifest.toml") |
| 244 |
manifest_contents = str(convert([tempdir], relative_to=tempdir)) |
| 245 |
with open(manifest_file, "w") as f: |
| 246 |
f.write(manifest_contents) |
| 247 |
|
| 248 |
# get the manifest |
| 249 |
manifest = ManifestParser(manifests=(manifest_file,), use_toml=True) |
| 250 |
|
| 251 |
# All of the tests are initially missing: |
| 252 |
paths = [str(i) for i in range(10)] |
| 253 |
self.assertEqual([i["name"] for i in manifest.missing()], paths) |
| 254 |
|
| 255 |
# But then we copy one over: |
| 256 |
self.assertEqual(manifest.get("name", name="1"), ["1"]) |
| 257 |
manifest.update(tempdir, name="1") |
| 258 |
self.assertEqual(sorted(os.listdir(newtempdir)), ["1", "manifest.toml"]) |
| 259 |
|
| 260 |
# Update that one file and copy all the "tests": |
| 261 |
open(os.path.join(tempdir, "1"), "w").write("secret door") |
| 262 |
manifest.update(tempdir) |
| 263 |
self.assertEqual( |
| 264 |
sorted(os.listdir(newtempdir)), |
| 265 |
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "manifest.toml"], |
| 266 |
) |
| 267 |
self.assertEqual( |
| 268 |
open(os.path.join(newtempdir, "1")).read().strip(), "secret door" |
| 269 |
) |
| 270 |
|
| 271 |
# clean up: |
| 272 |
shutil.rmtree(tempdir) |
| 273 |
shutil.rmtree(newtempdir) |
| 274 |
|
| 275 |
|
| 276 |
if __name__ == "__main__": |
| 277 |
mozunit.main() |