diff options
| author | Mike Frysinger <vapier@google.com> | 2020-09-06 14:53:18 -0400 |
|---|---|---|
| committer | Mike Frysinger <vapier@google.com> | 2020-11-18 19:10:57 +0000 |
| commit | 8c1e9cbef161f2ff12dadbacf26affd23876fde9 (patch) | |
| tree | fcfdc404568a2d8dbc9edd9ec99ecdd758f8c424 /tests/test_manifest_xml.py | |
| parent | a488af5ea5c53dd7cf2c90a751e77cc4ba87b7c3 (diff) | |
| download | git-repo-8c1e9cbef161f2ff12dadbacf26affd23876fde9.tar.gz | |
manifest_xml: refactor manifest parsing from client management
We conflate the manifest & parsing logic with the management of the
repo client checkout in a single class. This makes testing just one
part (the manifest parsing) hard as it requires a full checkout too.
Start splitting the two apart into separate classes to make it easy
to reason about & test.
Change-Id: Iaf897c93db9c724baba6044bfe7a589c024523b2
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/288682
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'tests/test_manifest_xml.py')
| -rw-r--r-- | tests/test_manifest_xml.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py index aa6cb7df..40385cce 100644 --- a/tests/test_manifest_xml.py +++ b/tests/test_manifest_xml.py | |||
| @@ -19,6 +19,8 @@ | |||
| 19 | from __future__ import print_function | 19 | from __future__ import print_function |
| 20 | 20 | ||
| 21 | import os | 21 | import os |
| 22 | import shutil | ||
| 23 | import tempfile | ||
| 22 | import unittest | 24 | import unittest |
| 23 | import xml.dom.minidom | 25 | import xml.dom.minidom |
| 24 | 26 | ||
| @@ -146,3 +148,90 @@ class ValueTests(unittest.TestCase): | |||
| 146 | with self.assertRaises(error.ManifestParseError): | 148 | with self.assertRaises(error.ManifestParseError): |
| 147 | node = self._get_node('<node a="xx"/>') | 149 | node = self._get_node('<node a="xx"/>') |
| 148 | manifest_xml.XmlInt(node, 'a') | 150 | manifest_xml.XmlInt(node, 'a') |
| 151 | |||
| 152 | |||
| 153 | class XmlManifestTests(unittest.TestCase): | ||
| 154 | """Check manifest processing.""" | ||
| 155 | |||
| 156 | def setUp(self): | ||
| 157 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | ||
| 158 | self.repodir = os.path.join(self.tempdir, '.repo') | ||
| 159 | self.manifest_dir = os.path.join(self.repodir, 'manifests') | ||
| 160 | self.manifest_file = os.path.join( | ||
| 161 | self.repodir, manifest_xml.MANIFEST_FILE_NAME) | ||
| 162 | self.local_manifest_dir = os.path.join( | ||
| 163 | self.repodir, manifest_xml.LOCAL_MANIFESTS_DIR_NAME) | ||
| 164 | os.mkdir(self.repodir) | ||
| 165 | os.mkdir(self.manifest_dir) | ||
| 166 | |||
| 167 | # The manifest parsing really wants a git repo currently. | ||
| 168 | gitdir = os.path.join(self.repodir, 'manifests.git') | ||
| 169 | os.mkdir(gitdir) | ||
| 170 | with open(os.path.join(gitdir, 'config'), 'w') as fp: | ||
| 171 | fp.write("""[remote "origin"] | ||
| 172 | url = https://localhost:0/manifest | ||
| 173 | """) | ||
| 174 | |||
| 175 | def tearDown(self): | ||
| 176 | shutil.rmtree(self.tempdir, ignore_errors=True) | ||
| 177 | |||
| 178 | def getXmlManifest(self, data): | ||
| 179 | """Helper to initialize a manifest for testing.""" | ||
| 180 | with open(self.manifest_file, 'w') as fp: | ||
| 181 | fp.write(data) | ||
| 182 | return manifest_xml.XmlManifest(self.repodir, self.manifest_file) | ||
| 183 | |||
| 184 | def test_empty(self): | ||
| 185 | """Parse an 'empty' manifest file.""" | ||
| 186 | manifest = self.getXmlManifest( | ||
| 187 | '<?xml version="1.0" encoding="UTF-8"?>' | ||
| 188 | '<manifest></manifest>') | ||
| 189 | self.assertEqual(manifest.remotes, {}) | ||
| 190 | self.assertEqual(manifest.projects, []) | ||
| 191 | |||
| 192 | def test_link(self): | ||
| 193 | """Verify Link handling with new names.""" | ||
| 194 | manifest = manifest_xml.XmlManifest(self.repodir, self.manifest_file) | ||
| 195 | with open(os.path.join(self.manifest_dir, 'foo.xml'), 'w') as fp: | ||
| 196 | fp.write('<manifest></manifest>') | ||
| 197 | manifest.Link('foo.xml') | ||
| 198 | with open(self.manifest_file) as fp: | ||
| 199 | self.assertIn('<include name="foo.xml" />', fp.read()) | ||
| 200 | |||
| 201 | def test_toxml_empty(self): | ||
| 202 | """Verify the ToXml() helper.""" | ||
| 203 | manifest = self.getXmlManifest( | ||
| 204 | '<?xml version="1.0" encoding="UTF-8"?>' | ||
| 205 | '<manifest></manifest>') | ||
| 206 | self.assertEqual(manifest.ToXml().toxml(), '<?xml version="1.0" ?><manifest/>') | ||
| 207 | |||
| 208 | def test_todict_empty(self): | ||
| 209 | """Verify the ToDict() helper.""" | ||
| 210 | manifest = self.getXmlManifest( | ||
| 211 | '<?xml version="1.0" encoding="UTF-8"?>' | ||
| 212 | '<manifest></manifest>') | ||
| 213 | self.assertEqual(manifest.ToDict(), {}) | ||
| 214 | |||
| 215 | def test_project_group(self): | ||
| 216 | """Check project group settings.""" | ||
| 217 | manifest = self.getXmlManifest(""" | ||
| 218 | <manifest> | ||
| 219 | <remote name="test-remote" fetch="http://localhost" /> | ||
| 220 | <default remote="test-remote" revision="refs/heads/main" /> | ||
| 221 | <project name="test-name" path="test-path"/> | ||
| 222 | <project name="extras" path="path" groups="g1,g2,g1"/> | ||
| 223 | </manifest> | ||
| 224 | """) | ||
| 225 | self.assertEqual(len(manifest.projects), 2) | ||
| 226 | # Ordering isn't guaranteed. | ||
| 227 | result = { | ||
| 228 | manifest.projects[0].name: manifest.projects[0].groups, | ||
| 229 | manifest.projects[1].name: manifest.projects[1].groups, | ||
| 230 | } | ||
| 231 | project = manifest.projects[0] | ||
| 232 | self.assertCountEqual( | ||
| 233 | result['test-name'], | ||
| 234 | ['name:test-name', 'all', 'path:test-path']) | ||
| 235 | self.assertCountEqual( | ||
| 236 | result['extras'], | ||
| 237 | ['g1', 'g2', 'g1', 'name:extras', 'all', 'path:path']) | ||
