diff options
author | Paul Duffin <paulduffin@google.com> | 2022-01-17 16:36:52 +0000 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2022-01-21 19:36:46 +0000 |
commit | f5245ce53daa2255cd39b7664379368044da80f6 (patch) | |
tree | 64951cfcb62721abf10ceb0dead6f86678fa7227 /build/mainline_modules_sdks_test.py | |
parent | b349358ae2753423bbec05d4c41161718bb774e9 (diff) | |
download | common-f5245ce53daa2255cd39b7664379368044da80f6.tar.gz |
Extract logic for building the snapshots to improve testability
Previously, the produce_dist() method of SdkDistProducer was not
testable because it attempted to invoke Soong to build the sdk
snapshots. So, the TestPopulateDist tested the populate_dist() method
instead. That was a problem because it meant that future changes to
the produce_dist() method could not be tested, at least not without
duplicating its functionality.
This change extracts the logic for building the snapshots (i.e. the
code that invokes soong to build the snapshot files) from the
SdkDistProducer into a separate SnapshotBuilder class. That allows
the test to substitute a FakeSnapshotBuilder that generates some fake
sdk snapshot zip files and call produce_dist() instead of calling
populate_dist() and populate_stubs().
It also renames the test to TestProduceDist to reflect that change.
This is part of a larger refactoring to improve the testability of the
mainline_modules_sdks.py file in preparation for adding support for
building build release specific snapshots.
Test: atest mainline_modules_sdks_test
packages/modules/common/build/mainline_modules_sdks.sh
tree out/dist/mainline-sdks out/dist/stubs
- check that it is identical to before this change
Bug: 204763318
Change-Id: Icf2e4b0200cc53863e45cf68208fbc8ec13c6f2c
Diffstat (limited to 'build/mainline_modules_sdks_test.py')
-rw-r--r-- | build/mainline_modules_sdks_test.py | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/build/mainline_modules_sdks_test.py b/build/mainline_modules_sdks_test.py index 57601361..34e6b5ae 100644 --- a/build/mainline_modules_sdks_test.py +++ b/build/mainline_modules_sdks_test.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. """Unit tests for mainline_modules_sdks.py.""" - from pathlib import Path import os import tempfile @@ -24,12 +23,15 @@ import zipfile import mainline_modules_sdks as mm -class TestPopulateDist(unittest.TestCase): +class FakeSnapshotBuilder(mm.SnapshotBuilder): + """A fake snapshot builder that does not run the build. - def create_snapshot_file(self, out_dir, name, version): - sdks_out_dir = Path(out_dir, "soong/mainline-sdks") - sdks_out_dir.mkdir(parents=True, exist_ok=True) - zip_file = Path(sdks_out_dir, f"{name}-{version}.zip") + This skips the whole build process and just creates some fake sdk + modules. + """ + + def create_snapshot_file(self, name, version): + zip_file = Path(self.get_sdk_path(name, version)) with zipfile.ZipFile(zip_file, "w") as z: z.writestr("Android.bp", "") if name.endswith("-sdk"): @@ -38,6 +40,19 @@ class TestPopulateDist(unittest.TestCase): z.writestr("sdk_library/public/lib.jar", "") z.writestr("sdk_library/public/api.txt", "") + def build_snapshots(self, sdk_versions, modules): + # Create input file structure. + sdks_out_dir = Path(self.get_mainline_sdks_path()) + sdks_out_dir.mkdir(parents=True, exist_ok=True) + # Create a fake sdk zip file for each module. + for module in modules: + for sdk in module.sdks: + for sdk_version in sdk_versions: + self.create_snapshot_file(sdk, sdk_version) + + +class TestProduceDist(unittest.TestCase): + def test(self): """Verify the dist/mainline-sdks directory is populated correctly""" with tempfile.TemporaryDirectory() as tmp_dir: @@ -51,18 +66,20 @@ class TestPopulateDist(unittest.TestCase): mm.MAINLINE_MODULES_BY_APEX["com.android.ipsec"], ] - # Create input file structure. - for module in modules: - for sdk in module.sdks: - self.create_snapshot_file(tmp_out_dir, sdk, "current") + subprocess_runner = mm.SubprocessRunner() - producer = mm.SdkDistProducer( + snapshot_builder = FakeSnapshotBuilder( + subprocess_runner=subprocess_runner, out_dir=tmp_out_dir, + ) + + producer = mm.SdkDistProducer( + subprocess_runner=subprocess_runner, + snapshot_builder=snapshot_builder, dist_dir=tmp_dist_dir, ) - sdk_versions = ["current"] - producer.populate_dist(sdk_versions, modules) + producer.produce_dist(modules) files = [] for abs_dir, _, filenames in os.walk(tmp_dist_dir): @@ -114,7 +131,11 @@ def readTestData(relative_path): class TestSoongConfigBoilerplateInserter(unittest.TestCase): def apply_transformations(self, src, transformations, expected): - producer = mm.SdkDistProducer(script=self._testMethodName) + producer = mm.SdkDistProducer( + subprocess_runner=None, + snapshot_builder=None, + script=self._testMethodName, + ) with tempfile.TemporaryDirectory() as tmp_dir: path = os.path.join(tmp_dir, "Android.bp") |