1#!/usr/bin/env python3 2# Copyright 2023 The ChromiumOS Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Provides utilities to read and edit the ChromiumOS Manifest entries. 7 8While this code reads and edits the internal manifest, it should only operate 9on toolchain projects (llvm-project, etc.) which are public. 10""" 11 12import unittest 13from xml.etree import ElementTree 14 15import manifest_utils 16 17 18MANIFEST_FIXTURE = """<?xml version="1.0" encoding="UTF-8"?> 19<manifest> 20 <!-- Comment that should not be removed. 21 Multiple lines. --> 22 <include name="_remotes.xml" /> 23 <default revision="refs/heads/main" 24 remote="cros" 25 sync-j="8" /> 26 27 <include name="_kernel_upstream.xml" /> 28 29 <!-- Common projects for developing CrOS. --> 30 <project path="src/repohooks" 31 name="chromiumos/repohooks" 32 groups="minilayout,paygen,firmware,buildtools,labtools,crosvm" /> 33 <repo-hooks in-project="chromiumos/repohooks" 34 enabled-list="pre-upload" /> 35 <project path="chromite" 36 name="chromiumos/chromite" 37 groups="minilayout,paygen,firmware,buildtools,chromeos-admin"> 38 <copyfile src="AUTHORS" dest="AUTHORS" /> 39 <copyfile src="LICENSE" dest="LICENSE" /> 40 </project> 41 <project path="src/third_party/llvm-project" 42 name="external/github.com/llvm/llvm-project" 43 groups="notdefault,bazel" 44 revision="abcd" /> 45 <project path="chromite/third_party/pyelftools" 46 name="chromiumos/third_party/pyelftools" 47 revision="refs/heads/chromeos-0.22" 48 groups="minilayout,paygen,firmware,buildtools" /> 49</manifest> 50""" 51 52 53class TestManifestUtils(unittest.TestCase): 54 """Test manifest_utils.""" 55 56 def test_update_chromeos_manifest(self): 57 root = ElementTree.fromstring( 58 MANIFEST_FIXTURE, 59 parser=manifest_utils.make_xmlparser(), 60 ) 61 manifest_utils.update_chromeos_manifest_tree("wxyz", root) 62 string_root1 = ElementTree.tostring(root) 63 self.assertRegex( 64 str(string_root1, encoding="utf-8"), 65 r'revision="wxyz"', 66 ) 67 self.assertRegex( 68 str(string_root1, encoding="utf-8"), 69 r"<!-- Comment that should not be removed.", 70 ) 71 self.assertNotRegex( 72 str(string_root1, encoding="utf-8"), 73 r'revision="abcd"', 74 ) 75 # Check idempotence. 76 manifest_utils.update_chromeos_manifest_tree("wxyz", root) 77 string_root2 = ElementTree.tostring(root) 78 self.assertEqual(string_root1, string_root2) 79 80 def test_extract_current_llvm_hash(self): 81 root = ElementTree.fromstring( 82 MANIFEST_FIXTURE, 83 parser=manifest_utils.make_xmlparser(), 84 ) 85 self.assertEqual( 86 manifest_utils.extract_current_llvm_hash_from_xml(root), 87 "abcd", 88 ) 89 90 91if __name__ == "__main__": 92 unittest.main() 93