| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# |
| 3 |
# This source code is licensed under the MIT license found in the |
| 4 |
# LICENSE file in the root directory of this source tree. |
| 5 |
|
| 6 |
|
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
|
| 10 |
from ..load import load_all_manifests, patch_loader |
| 11 |
from ..manifest import ManifestParser |
| 12 |
|
| 13 |
|
| 14 |
class ManifestTest(unittest.TestCase): |
| 15 |
def test_missing_section(self) -> None: |
| 16 |
with self.assertRaisesRegex( |
| 17 |
Exception, "manifest file test is missing required section manifest" |
| 18 |
): |
| 19 |
ManifestParser("test", "") |
| 20 |
|
| 21 |
def test_missing_name(self) -> None: |
| 22 |
with self.assertRaisesRegex( |
| 23 |
Exception, |
| 24 |
"manifest file test section 'manifest' is missing required field 'name'", |
| 25 |
): |
| 26 |
ManifestParser( |
| 27 |
"test", |
| 28 |
""" |
| 29 |
[manifest] |
| 30 |
""", |
| 31 |
) |
| 32 |
|
| 33 |
def test_minimal(self) -> None: |
| 34 |
p = ManifestParser( |
| 35 |
"test", |
| 36 |
""" |
| 37 |
[manifest] |
| 38 |
name = test |
| 39 |
""", |
| 40 |
) |
| 41 |
self.assertEqual(p.name, "test") |
| 42 |
self.assertEqual(p.fbsource_path, None) |
| 43 |
|
| 44 |
def test_minimal_with_fbsource_path(self) -> None: |
| 45 |
p = ManifestParser( |
| 46 |
"test", |
| 47 |
""" |
| 48 |
[manifest] |
| 49 |
name = test |
| 50 |
fbsource_path = fbcode/wat |
| 51 |
""", |
| 52 |
) |
| 53 |
self.assertEqual(p.name, "test") |
| 54 |
self.assertEqual(p.fbsource_path, "fbcode/wat") |
| 55 |
|
| 56 |
def test_unknown_field(self) -> None: |
| 57 |
with self.assertRaisesRegex( |
| 58 |
Exception, |
| 59 |
( |
| 60 |
"manifest file test section 'manifest' contains " |
| 61 |
"unknown field 'invalid.field'" |
| 62 |
), |
| 63 |
): |
| 64 |
ManifestParser( |
| 65 |
"test", |
| 66 |
""" |
| 67 |
[manifest] |
| 68 |
name = test |
| 69 |
invalid.field = woot |
| 70 |
""", |
| 71 |
) |
| 72 |
|
| 73 |
def test_invalid_section_name(self) -> None: |
| 74 |
with self.assertRaisesRegex( |
| 75 |
Exception, "manifest file test contains unknown section 'invalid.section'" |
| 76 |
): |
| 77 |
ManifestParser( |
| 78 |
"test", |
| 79 |
""" |
| 80 |
[manifest] |
| 81 |
name = test |
| 82 |
|
| 83 |
[invalid.section] |
| 84 |
foo = bar |
| 85 |
""", |
| 86 |
) |
| 87 |
|
| 88 |
def test_value_in_dependencies_section(self) -> None: |
| 89 |
with self.assertRaisesRegex( |
| 90 |
Exception, |
| 91 |
( |
| 92 |
"manifest file test section 'dependencies' has " |
| 93 |
"'foo = bar' but this section doesn't allow " |
| 94 |
"specifying values for its entries" |
| 95 |
), |
| 96 |
): |
| 97 |
ManifestParser( |
| 98 |
"test", |
| 99 |
""" |
| 100 |
[manifest] |
| 101 |
name = test |
| 102 |
|
| 103 |
[dependencies] |
| 104 |
foo = bar |
| 105 |
""", |
| 106 |
) |
| 107 |
|
| 108 |
def test_invalid_conditional_section_name(self) -> None: |
| 109 |
with self.assertRaisesRegex( |
| 110 |
Exception, |
| 111 |
( |
| 112 |
"manifest file test section 'dependencies.=' " |
| 113 |
"has invalid conditional: expected " |
| 114 |
"identifier found =" |
| 115 |
), |
| 116 |
): |
| 117 |
ManifestParser( |
| 118 |
"test", |
| 119 |
""" |
| 120 |
[manifest] |
| 121 |
name = test |
| 122 |
|
| 123 |
[dependencies.=] |
| 124 |
""", |
| 125 |
) |
| 126 |
|
| 127 |
def test_section_as_args(self) -> None: |
| 128 |
p = ManifestParser( |
| 129 |
"test", |
| 130 |
""" |
| 131 |
[manifest] |
| 132 |
name = test |
| 133 |
|
| 134 |
[dependencies] |
| 135 |
a |
| 136 |
b |
| 137 |
c |
| 138 |
|
| 139 |
[dependencies.test=on] |
| 140 |
foo |
| 141 |
""", |
| 142 |
) |
| 143 |
self.assertEqual(p.get_section_as_args("dependencies"), ["a", "b", "c"]) |
| 144 |
self.assertEqual( |
| 145 |
p.get_section_as_args("dependencies", {"test": "off"}), ["a", "b", "c"] |
| 146 |
) |
| 147 |
self.assertEqual( |
| 148 |
p.get_section_as_args("dependencies", {"test": "on"}), |
| 149 |
["a", "b", "c", "foo"], |
| 150 |
) |
| 151 |
|
| 152 |
p2 = ManifestParser( |
| 153 |
"test", |
| 154 |
""" |
| 155 |
[manifest] |
| 156 |
name = test |
| 157 |
|
| 158 |
[autoconf.args] |
| 159 |
--prefix=/foo |
| 160 |
--with-woot |
| 161 |
""", |
| 162 |
) |
| 163 |
self.assertEqual( |
| 164 |
p2.get_section_as_args("autoconf.args"), ["--prefix=/foo", "--with-woot"] |
| 165 |
) |
| 166 |
|
| 167 |
def test_section_as_dict(self) -> None: |
| 168 |
p = ManifestParser( |
| 169 |
"test", |
| 170 |
""" |
| 171 |
[manifest] |
| 172 |
name = test |
| 173 |
|
| 174 |
[cmake.defines] |
| 175 |
foo = bar |
| 176 |
|
| 177 |
[cmake.defines.test=on] |
| 178 |
foo = baz |
| 179 |
""", |
| 180 |
) |
| 181 |
self.assertEqual(p.get_section_as_dict("cmake.defines", {}), {"foo": "bar"}) |
| 182 |
self.assertEqual( |
| 183 |
p.get_section_as_dict("cmake.defines", {"test": "on"}), {"foo": "baz"} |
| 184 |
) |
| 185 |
|
| 186 |
p2 = ManifestParser( |
| 187 |
"test", |
| 188 |
""" |
| 189 |
[manifest] |
| 190 |
name = test |
| 191 |
|
| 192 |
[cmake.defines.test=on] |
| 193 |
foo = baz |
| 194 |
|
| 195 |
[cmake.defines] |
| 196 |
foo = bar |
| 197 |
""", |
| 198 |
) |
| 199 |
self.assertEqual( |
| 200 |
p2.get_section_as_dict("cmake.defines", {"test": "on"}), |
| 201 |
{"foo": "bar"}, |
| 202 |
msg="sections cascade in the order they appear in the manifest", |
| 203 |
) |
| 204 |
|
| 205 |
def test_parse_common_manifests(self) -> None: |
| 206 |
patch_loader(__name__) |
| 207 |
manifests = load_all_manifests(None) |
| 208 |
self.assertNotEqual(0, len(manifests), msg="parsed some number of manifests") |
| 209 |
|
| 210 |
def test_mismatch_name(self) -> None: |
| 211 |
with self.assertRaisesRegex( |
| 212 |
Exception, |
| 213 |
"filename of the manifest 'foo' does not match the manifest name 'bar'", |
| 214 |
): |
| 215 |
ManifestParser( |
| 216 |
"foo", |
| 217 |
""" |
| 218 |
[manifest] |
| 219 |
name = bar |
| 220 |
""", |
| 221 |
) |
| 222 |
|
| 223 |
def test_duplicate_manifest(self) -> None: |
| 224 |
patch_loader(__name__, "fixtures/duplicate") |
| 225 |
|
| 226 |
with self.assertRaisesRegex(Exception, "found duplicate manifest 'foo'"): |
| 227 |
load_all_manifests(None) |
| 228 |
|
| 229 |
if sys.version_info < (3, 2): |
| 230 |
|
| 231 |
def assertRaisesRegex(self, *args, **kwargs): |
| 232 |
return self.assertRaisesRegexp(*args, **kwargs) |