Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0a0c4dfb authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Preparing to switch away from the deprecated test runner" into main

parents 91eeab07 03e9c7c4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ Currently only offers to include classes which are fully passing; ignores
classes that have partial success.

Typical usage:
$ ENABLE_PROBE_IGNORED=1 atest MyTestsRavenwood
$ RAVENWOOD_RUN_DISABLED_TESTS=1 atest MyTestsRavenwood
$ cd /path/to/tests/root
$ python bulk_enable.py /path/to/atest/output/host_log.txt
"""
+23 −0
Original line number Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

android_ravenwood_test {
    name: "RavenwoodCoreTest",

    static_libs: [
        "androidx.annotation_annotation",
        "androidx.test.rules",
    ],

    srcs: [
        "test/**/*.java",
    ],
    sdk_version: "test_current",
    auto_gen_config: true,
}
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.platform.test.ravenwood.coretest;

import android.platform.test.ravenwood.RavenwoodRule;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;

/**
 * Test for the test runner validator in RavenwoodRule.
 */
@RunWith(AndroidJUnit4.class)
public class RavenwoodTestRunnerValidationTest {
    // Note the following rules don't have a @Rule, because they need to be applied in a specific
    // order. So we use a RuleChain instead.
    private ExpectedException mThrown = ExpectedException.none();
    private final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Rule
    public final RuleChain chain = RuleChain.outerRule(mThrown).around(mRavenwood);

    public RavenwoodTestRunnerValidationTest() {
        Assume.assumeTrue(mRavenwood._ravenwood_private$isOptionalValidationEnabled());
        // Because RavenwoodRule will throw this error before executing the test method,
        // we can't do it in the test method itself.
        // So instead, we initialize it here.
        mThrown.expectMessage("Switch to androidx.test.ext.junit.runners.AndroidJUnit4");
    }

    @Test
    public void testValidateTestRunner() {
    }
}
+68 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
#
# Copyright (C) 2024 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.

"""
Tool switch the deprecated jetpack test runner to the correct one.

Typical usage:
$ RAVENWOOD_OPTIONAL_VALIDATION=1 atest MyTestsRavenwood # Prepend RAVENWOOD_RUN_DISABLED_TESTS=1 as needed
$ cd /path/to/tests/root
$ python bulk_enable.py /path/to/atest/output/host_log.txt
"""

import collections
import os
import re
import subprocess
import sys

re_result = re.compile("I/ModuleListener.+?null-device-0 (.+?)#(.+?) ([A-Z_]+)(.*)$")

OLD_RUNNER = "androidx.test.runner.AndroidJUnit4"
NEW_RUNNER = "androidx.test.ext.junit.runners.AndroidJUnit4"
SED_ARG = r"s/%s/%s/g" % (OLD_RUNNER, NEW_RUNNER)

target = collections.defaultdict()

with open(sys.argv[1]) as f:
    for line in f.readlines():
        result = re_result.search(line)
        if result:
            clazz, method, state, msg = result.groups()
            if NEW_RUNNER in msg:
                target[clazz] = 1

if len(target) == 0:
    print("No tests need updating.")
    sys.exit(0)

num_fixed = 0
for clazz in target.keys():
    print("Fixing test runner", clazz)
    clazz_match = re.compile("%s\.(kt|java)" % (clazz.split(".")[-1]))
    found = False
    for root, dirs, files in os.walk("."):
        for f in files:
            if clazz_match.match(f):
                found = True
                num_fixed += 1
                path = os.path.join(root, f)
                subprocess.run(["sed", "-i", "-E", SED_ARG, path])
    if not found:
        print(f"  Warining: tests {clazz} not found")


print("Tests fixed", num_fixed)
+30 −0
Original line number Diff line number Diff line
@@ -28,7 +28,10 @@ import androidx.test.platform.app.InstrumentationRegistry;

import com.android.internal.os.RuntimeInit;

import org.junit.Assert;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.Statement;

import java.io.PrintStream;
import java.util.Map;
@@ -167,4 +170,31 @@ public class RavenwoodRuleImpl {
            }
        }
    }

    public static void validate(Statement base, Description description,
            boolean enableOptionalValidation) {
        validateTestRunner(base, description, enableOptionalValidation);
    }

    private static void validateTestRunner(Statement base, Description description,
            boolean shouldFail) {
        final var testClass = description.getTestClass();
        final var runWith = testClass.getAnnotation(RunWith.class);
        if (runWith == null) {
            return;
        }

        // Due to build dependencies, we can't directly refer to androidx classes here,
        // so just check the class name instead.
        if (runWith.value().getCanonicalName().equals("androidx.test.runner.AndroidJUnit4")) {
            var message = "Test " + testClass.getCanonicalName() + " uses deprecated"
                    + " test runner androidx.test.runner.AndroidJUnit4."
                    + " Switch to androidx.test.ext.junit.runners.AndroidJUnit4.";
            if (shouldFail) {
                Assert.fail(message);
            } else {
                System.err.println("Warning: " + message);
            }
        }
    }
}
Loading