diff options
| author | Yuexi Ma <yuexima@google.com> | 2021-01-28 19:54:32 -0800 |
|---|---|---|
| committer | Yuexi Ma <yuexima@google.com> | 2021-02-02 15:59:08 -0800 |
| commit | a2309f72b8f500355d8515f4150c18c3a3aa452b (patch) | |
| tree | 02b38985dfac8fb1e3524ede40800d3dd04014d8 | |
| parent | 68c5a9249fa244f58b04a4dbf7f75ebce6419ffe (diff) | |
| download | platform_test_app_compat_csuite-a2309f72b8f500355d8515f4150c18c3a3aa452b.tar.gz platform_test_app_compat_csuite-a2309f72b8f500355d8515f4150c18c3a3aa452b.tar.bz2 platform_test_app_compat_csuite-a2309f72b8f500355d8515f4150c18c3a3aa452b.zip | |
Add a file based package name provider
When the list of package names is too large, the test command might be too long for some system to handle. This CL adds a file based package name provider so that the test can accept files that contains long package name lists.
Test: atest :presubmit
Bug: 178392533
Change-Id: I0946e8de1c9226edb2560fd3190941b9a8edc8ed
Merged-In: I0946e8de1c9226edb2560fd3190941b9a8edc8ed
(cherry picked from commit 0aa77a2f149a74adb9961864a4cf20dfe1814dba)
4 files changed, 178 insertions, 0 deletions
diff --git a/harness/src/main/java/com/android/csuite/core/FileBasedPackageNameProvider.java b/harness/src/main/java/com/android/csuite/core/FileBasedPackageNameProvider.java new file mode 100644 index 0000000..6f6af06 --- /dev/null +++ b/harness/src/main/java/com/android/csuite/core/FileBasedPackageNameProvider.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2021 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. + */ + +package com.android.csuite.core; + +import com.android.tradefed.config.Option; + +import com.google.common.annotations.VisibleForTesting; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +/** A package name provider that accepts files that contains package names. */ +public final class FileBasedPackageNameProvider implements PackageNameProvider { + @VisibleForTesting static final String PACKAGES_FILE = "packages-file"; + @VisibleForTesting static final String COMMENT_LINE_PREFIX = "#"; + + @Option( + name = PACKAGES_FILE, + description = + "File paths that contain package names separated by newline characters." + + " Comment lines are supported only if the lines start with double slash." + + " Trailing comments are not supported. Empty lines are ignored.") + private final Set<File> mPackagesFiles = new HashSet<>(); + + @Override + public Set<String> get() throws IOException { + Set<String> packages = new HashSet<>(); + for (File packagesFile : mPackagesFiles) { + packages.addAll( + Files.readAllLines(packagesFile.toPath()).parallelStream() + .map(String::trim) + .filter(this::isPackageName) + .collect(Collectors.toSet())); + } + return packages; + } + + private boolean isPackageName(String text) { + // Check the text is not an empty string and not a comment line. + return !text.isEmpty() && !text.startsWith(COMMENT_LINE_PREFIX); + } +} diff --git a/harness/src/main/resources/config/csuite-base.xml b/harness/src/main/resources/config/csuite-base.xml index 33788ab..03e418b 100644 --- a/harness/src/main/resources/config/csuite-base.xml +++ b/harness/src/main/resources/config/csuite-base.xml @@ -35,4 +35,5 @@ <target_preparer class="com.android.csuite.config.ModuleGenerator" /> <object type="PACKAGE_NAME_PROVIDER" class="com.android.csuite.core.CommandLinePackageNameProvider" /> + <object type="PACKAGE_NAME_PROVIDER" class="com.android.csuite.core.FileBasedPackageNameProvider" /> </configuration> diff --git a/harness/src/test/java/com/android/csuite/CSuiteUnitTests.java b/harness/src/test/java/com/android/csuite/CSuiteUnitTests.java index c97736d..8ba878d 100644 --- a/harness/src/test/java/com/android/csuite/CSuiteUnitTests.java +++ b/harness/src/test/java/com/android/csuite/CSuiteUnitTests.java @@ -28,6 +28,7 @@ import org.junit.runners.Suite.SuiteClasses; com.android.csuite.config.AppRemoteFileResolverTest.class, com.android.csuite.config.ModuleGeneratorTest.class, com.android.csuite.core.CommandLinePackageNameProviderTest.class, + com.android.csuite.core.FileBasedPackageNameProviderTest.class, com.android.csuite.testing.CorrespondencesTest.class, com.android.csuite.testing.MoreAssertsTest.class, }) diff --git a/harness/src/test/java/com/android/csuite/core/FileBasedPackageNameProviderTest.java b/harness/src/test/java/com/android/csuite/core/FileBasedPackageNameProviderTest.java new file mode 100644 index 0000000..ee429b2 --- /dev/null +++ b/harness/src/test/java/com/android/csuite/core/FileBasedPackageNameProviderTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2020 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. + */ + +package com.android.csuite.core; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.tradefed.config.ConfigurationException; +import com.android.tradefed.config.OptionSetter; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Set; + +@RunWith(JUnit4.class) +public final class FileBasedPackageNameProviderTest { + private static final String TEST_PACKAGE_NAME1 = "test.package.name1"; + private static final String TEST_PACKAGE_NAME2 = "test.package.name2"; + private static final String PACKAGE_PLACEHOLDER = "{package}"; + private static final Exception NO_EXCEPTION = null; + + @Rule public final TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void get_fileNotSpecified_returnsEmptySet() throws Exception { + FileBasedPackageNameProvider provider = createProvider(); + + Set<String> packageNames = provider.get(); + + assertThat(packageNames).isEmpty(); + } + + @Test + public void get_multipleFileSpecified_returnsAllEntries() throws Exception { + String packageName1 = "a"; + String packageName2 = "b"; + String packageName3 = "c"; + String packageName4 = "d"; + FileBasedPackageNameProvider provider = + createProvider( + createPackagesFile(packageName1 + "\n" + packageName2), + createPackagesFile(packageName3 + "\n" + packageName4)); + + Set<String> packageNames = provider.get(); + + assertThat(packageNames) + .containsExactly(packageName1, packageName2, packageName3, packageName4); + } + + @Test + public void get_fileContainsEmptyLines_ignoresEmptyLines() throws Exception { + String packageName1 = "a"; + String packageName2 = "b"; + FileBasedPackageNameProvider provider = + createProvider(createPackagesFile(packageName1 + "\n \n\n" + packageName2)); + + Set<String> packageNames = provider.get(); + + assertThat(packageNames).containsExactly(packageName1, packageName2); + } + + @Test + public void get_fileContainsCommentLines_ignoresCommentLines() throws Exception { + String packageName1 = "a"; + String packageName2 = "b"; + FileBasedPackageNameProvider provider = + createProvider( + createPackagesFile( + packageName1 + + "\n" + + FileBasedPackageNameProvider.COMMENT_LINE_PREFIX + + " Some comments\n" + + packageName2)); + + Set<String> packageNames = provider.get(); + + assertThat(packageNames).containsExactly(packageName1, packageName2); + } + + private FileBasedPackageNameProvider createProvider(Path... packagesFiles) + throws IOException, ConfigurationException { + FileBasedPackageNameProvider provider = new FileBasedPackageNameProvider(); + OptionSetter optionSetter = new OptionSetter(provider); + for (Path packagesFile : packagesFiles) { + optionSetter.setOptionValue( + FileBasedPackageNameProvider.PACKAGES_FILE, packagesFile.toString()); + } + return provider; + } + + private Path createPackagesFile(String content) throws IOException { + Path tempFile = Files.createTempFile(tempFolder.getRoot().toPath(), "packages", ".txt"); + Files.write(tempFile, content.getBytes()); + return tempFile; + } +} |
