#!/usr/bin/env python3 # Copyright (C) 2025 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
def test_get_projects_from_manifest_with_glob_src(self): """Test the manifest is not parsed if the src of linkfile is a glob pattern."""
manifest_content_linkfile = """<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<project groups="pdk" name="platform/bionic" path="bionic">
<linkfile src="**/*.txt" dest="BIONIC_INSTRUCTIONS"/>
</project>
</manifest> """
projects = source_trimmer.get_projects_from_manifest(manifest_content_linkfile)
self.assertIsNone(projects)
def test_find_projects_to_remove(self): """Test that the projects to remove are identified correctly."""
all_projects = source_trimmer.get_projects_from_manifest(self.manifest_content)
groups_to_keep = ["pdk"]
projects_to_remove = source_trimmer.find_projects_to_remove(
all_projects, groups_to_keep
) # platform/bionic and platform/bootable/recovery should be kept.
expected_to_remove = [
source_trimmer.Project(
name="platform/build",
path="build/make",
groups=["pdk-desktop"],
operations=[],
),
source_trimmer.Project(
name="platform/system/core",
path="system/core",
groups=[],
operations=[],
),
source_trimmer.Project(
name="platform/lib/foo",
path="external/foo",
groups=["group1"],
operations=[],
),
source_trimmer.Project(
name="platform/lib/bar",
path="platform/lib/bar",
groups=["group1", "group2"],
operations=[],
),
]
self.assertEqual(projects_to_remove, expected_to_remove)
def test_find_projects_to_remove_multiple_groups(self): """Test that the projects to remove are identified correctly when multiple groups are specified."""
all_projects = source_trimmer.get_projects_from_manifest(self.manifest_content)
groups_to_keep = ["pdk", "pdk-desktop"]
projects_to_remove = source_trimmer.find_projects_to_remove(
all_projects, groups_to_keep
) # platform/bionic, platform/bootable/recovery and platform/build should be kept.
expected_to_remove = [
source_trimmer.Project(
name="platform/system/core",
path="system/core",
groups=[],
operations=[],
),
source_trimmer.Project(
name="platform/lib/foo",
path="external/foo",
groups=["group1"],
operations=[],
),
source_trimmer.Project(
name="platform/lib/bar",
path="platform/lib/bar",
groups=["group1", "group2"],
operations=[],
),
]
self.assertEqual(projects_to_remove, expected_to_remove)
def test_remove_project_directories(self): """Test that the project directories are removed correctly."""
projects_to_remove = [
source_trimmer.Project(
name="platform/build",
path="build/make",
groups=["pdk-desktop"],
operations=[],
),
source_trimmer.Project(
name="platform/system/core",
path="system/core",
groups=[],
operations=[],
),
] with tempfile.TemporaryDirectory() as tmpdir:
checkout_root = pathlib.Path(tmpdir) # Create dummy project directories.
(checkout_root / "build/make").mkdir(parents=True)
(checkout_root / "system/core").mkdir(parents=True)
(checkout_root / "bionic").mkdir(parents=True) # Should be kept.
self.assertFalse((checkout_root / "build/make").exists())
self.assertFalse((checkout_root / "system/core").exists())
self.assertTrue((checkout_root / "bionic").exists()) # Check that empty parent 'build' is also removed.
self.assertFalse((checkout_root / "build").exists())
def test_remove_project_directories_dry_run(self): """Test that the project directories are not removed in dry run mode."""
projects_to_remove = [
source_trimmer.Project(
name="platform/build",
path="build/make",
groups=["pdk-desktop"],
operations=[],
),
] with tempfile.TemporaryDirectory() as tmpdir:
checkout_root = pathlib.Path(tmpdir)
(checkout_root / "build/make").mkdir(parents=True)
def test_keep_projects_overrides_removal(self): """Test that projects to keep are not removed, even if they would be marked for removal.""" # These projects would be marked for removal by find_projects_to_remove.
projects_to_remove = [
source_trimmer.Project(
name="platform/build",
path="build/make",
groups=["pdk-desktop"],
operations=[],
),
source_trimmer.Project(
name="platform/system/core",
path="system/core",
groups=[],
operations=[],
),
source_trimmer.Project(
name="platform/lib/foo",
path="external/foo",
groups=["another-group"],
operations=[],
),
]
keep_project_names = set(["platform/system/core", "platform/lib/foo"])
# This logic is what's in the main() function of the script.
final_projects_to_remove = [
p for p in projects_to_remove if p["name"] notin keep_project_names
]
for name, raw_groups, expected in test_cases: with self.subTest(name=name):
result = source_trimmer.process_groups_to_keep(raw_groups)
self.assertEqual(result, expected, msg=f"Failed test case: {name}")
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.