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

Commit 75816497 authored by John Wu's avatar John Wu
Browse files

[Ravenwood] Add "--strip-mockito" to Ravenizer

In some situations, removing the Android device-side Mockito from test
dependencies is nearly impossible. To make these tests continue to
function on Ravenwood, make the Ravenizer tool remove all conflicting
classes and files from the final test jar.

Bug: 292141694
Flag: EXEMPT host test change only
Test: atest RavenwoodCoreTest
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: Icfb81dc3b30f06e94050ebdf7ec5bb53959fcc12
parent f811af15
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -17,9 +17,15 @@ android_ravenwood_test {
        "junit-params",
        "platform-parametric-runner-lib",
        "truth",

        // This library should be removed by Ravenizer
        "mockito-target-minus-junit4",
    ],
    srcs: [
        "test/**/*.java",
    ],
    ravenizer: {
        strip_mockito: true,
    },
    auto_gen_config: true,
}
+35 −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.ravenwoodtest.coretest;

import static org.junit.Assert.assertThrows;

import org.junit.Test;

public class RavenwoodMockitoTest {

    @Test
    public void checkMockitoClasses() {
        // DexMaker should not exist
        assertThrows(
                ClassNotFoundException.class,
                () -> Class.forName("com.android.dx.DexMaker"));
        // Mockito 2 should not exist
        assertThrows(
                ClassNotFoundException.class,
                () -> Class.forName("org.mockito.Matchers"));
    }
}
+13 −5
Original line number Diff line number Diff line
@@ -85,18 +85,17 @@ data class RavenizerStats(
/**
 * Main class.
 */
class Ravenizer(val options: RavenizerOptions) {
    fun run() {
class Ravenizer {
    fun run(options: RavenizerOptions) {
        val stats = RavenizerStats()

        val fatalValidation = options.fatalValidation.get

        stats.totalTime = log.nTime {
            process(
                options.inJar.get,
                options.outJar.get,
                options.enableValidation.get,
                fatalValidation,
                options.fatalValidation.get,
                options.stripMockito.get,
                stats,
            )
        }
@@ -108,6 +107,7 @@ class Ravenizer(val options: RavenizerOptions) {
        outJar: String,
        enableValidation: Boolean,
        fatalValidation: Boolean,
        stripMockito: Boolean,
        stats: RavenizerStats,
    ) {
        var allClasses = ClassNodes.loadClassStructures(inJar) {
@@ -126,6 +126,9 @@ class Ravenizer(val options: RavenizerOptions) {
                }
            }
        }
        if (includeUnsupportedMockito(allClasses)) {
            log.w("Unsupported Mockito detected in $inJar}!")
        }

        stats.totalProcessTime = log.vTime("$executableName processing $inJar") {
            ZipFile(inJar).use { inZip ->
@@ -145,6 +148,11 @@ class Ravenizer(val options: RavenizerOptions) {
                            )
                        }

                        if (stripMockito && entry.name.isMockitoFile()) {
                            // Skip this entry
                            continue
                        }

                        val className = zipEntryNameToClassName(entry.name)

                        if (className != null) {
+1 −1
Original line number Diff line number Diff line
@@ -36,6 +36,6 @@ fun main(args: Array<String>) {
        log.v("Options: $options")

        // Run.
        Ravenizer(options).run()
        Ravenizer().run(options)
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -47,6 +47,9 @@ class RavenizerOptions(

    /** Whether the validation failure is fatal or not. */
    var fatalValidation: SetOnce<Boolean> = SetOnce(false),

    /** Whether to remove mockito and dexmaker classes. */
    var stripMockito: SetOnce<Boolean> = SetOnce(false),
) {
    companion object {

@@ -85,6 +88,9 @@ class RavenizerOptions(
                        "--fatal-validation" -> ret.fatalValidation.set(true)
                        "--no-fatal-validation" -> ret.fatalValidation.set(false)

                        "--strip-mockito" -> ret.stripMockito.set(true)
                        "--no-strip-mockito" -> ret.stripMockito.set(false)

                        else -> throw ArgumentsException("Unknown option: $arg")
                    }
                } catch (e: SetOnce.SetMoreThanOnceException) {
Loading