diff options
author | Ramy Medhat <abdelaal@google.com> | 2020-06-12 01:58:47 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-06-12 01:58:47 +0000 |
commit | bc24177eff064c9d044b1fbd17e9f176cd33b094 (patch) | |
tree | 7deca6d2816092f5d12e347cb0f8d637de38d26d | |
parent | 9684e5657e5489b0859b5ea2d004678929e767a5 (diff) | |
parent | e112c17285c5a5372ae8fa53840cc14c0c594b66 (diff) |
Add warning-only mode for metalava sandbox. am: 841ad21b7e am: e112c17285
Original change: https://googleplex-android-review.googlesource.com/c/platform/tools/metalava/+/11832287
Change-Id: I5a7935b9f2515294709002d75aa85bad6c8869bf
-rw-r--r-- | src/main/java/com/android/tools/metalava/Driver.kt | 16 | ||||
-rw-r--r-- | src/main/java/com/android/tools/metalava/Options.kt | 15 | ||||
-rw-r--r-- | src/test/java/com/android/tools/metalava/OptionsTest.kt | 8 |
3 files changed, 29 insertions, 10 deletions
diff --git a/src/main/java/com/android/tools/metalava/Driver.kt b/src/main/java/com/android/tools/metalava/Driver.kt index 9626d7f..1241a16 100644 --- a/src/main/java/com/android/tools/metalava/Driver.kt +++ b/src/main/java/com/android/tools/metalava/Driver.kt @@ -122,7 +122,9 @@ fun run( } if (hasFileReadViolations) { stderr.println("$PROGRAM_NAME detected access to files that are not explicitly specified. See ${options.strictInputViolationsFile} for details.") - exitCode = -1 + if (options.strictInputFiles == Options.StrictInputFileMode.STRICT) { + exitCode = -1 + } } } catch (e: DriverException) { stdout.flush() @@ -186,14 +188,20 @@ private fun maybeActivateSandbox() { val writer = options.strictInputViolationsPrintWriter!! // Writes all violations to [Options.strictInputFiles]. - // Note violation reads on directories are logged, but is considered to be a "warning" and - // doesn't affect the exit code. See [FileReadSandbox] for the details. + // If Options.StrictInputFile.Mode is STRICT, then all violations on reads are logged, and the + // tool exits with a negative error code if there are any file read violations. Directory read + // violations are logged, but are considered to be a "warning" and doesn't affect the exit code. + // If STRICT_WARN, all violations on reads are logged similar to STRICT, but the exit code is + // unaffected. + // If STRICT_WITH_STACK, similar to STRICT, but also logs the stack trace to + // Options.strictInputFiles. + // See [FileReadSandbox] for the details. FileReadSandbox.activate(object : FileReadSandbox.Listener { var seen = mutableSetOf<String>() override fun onViolation(absolutePath: String, isDirectory: Boolean) { if (!seen.contains(absolutePath)) { val suffix = if (isDirectory) "/" else "" - writer.println("Sandbox violation: $absolutePath$suffix") + writer.println("$absolutePath$suffix") if (options.strictInputFiles == Options.StrictInputFileMode.STRICT_WITH_STACK) { Throwable().printStackTrace(writer) } diff --git a/src/main/java/com/android/tools/metalava/Options.kt b/src/main/java/com/android/tools/metalava/Options.kt index 7e0a888..1636985 100644 --- a/src/main/java/com/android/tools/metalava/Options.kt +++ b/src/main/java/com/android/tools/metalava/Options.kt @@ -169,6 +169,7 @@ const val ARG_ERROR_MESSAGE_CHECK_COMPATIBILITY_CURRENT = "--error-message:compa const val ARG_NO_IMPLICIT_ROOT = "--no-implicit-root" const val ARG_STRICT_INPUT_FILES = "--strict-input-files" const val ARG_STRICT_INPUT_FILES_STACK = "--strict-input-files:stack" +const val ARG_STRICT_INPUT_FILES_WARN = "--strict-input-files:warn" const val ARG_STRICT_INPUT_FILES_EXEMPT = "--strict-input-files-exempt" class Options( @@ -667,12 +668,14 @@ class Options( enum class StrictInputFileMode { PERMISSIVE, STRICT, + STRICT_WARN, STRICT_WITH_STACK; companion object { fun fromArgument(arg: String): StrictInputFileMode { return when (arg) { ARG_STRICT_INPUT_FILES -> STRICT + ARG_STRICT_INPUT_FILES_WARN -> STRICT_WARN ARG_STRICT_INPUT_FILES_STACK -> STRICT_WITH_STACK else -> PERMISSIVE } @@ -682,7 +685,8 @@ class Options( /** * Whether we should allow metalava to read files that are not explicitly specified in the - * command line. See [ARG_STRICT_INPUT_FILES] and [ARG_STRICT_INPUT_FILES_STACK] + * command line. See [ARG_STRICT_INPUT_FILES], [ARG_STRICT_INPUT_FILES_WARN] and + * [ARG_STRICT_INPUT_FILES_STACK]. */ var strictInputFiles = StrictInputFileMode.PERMISSIVE @@ -1333,9 +1337,9 @@ class Options( allowImplicitRoot = false } - ARG_STRICT_INPUT_FILES, ARG_STRICT_INPUT_FILES_STACK -> { + ARG_STRICT_INPUT_FILES, ARG_STRICT_INPUT_FILES_WARN, ARG_STRICT_INPUT_FILES_STACK -> { if (strictInputViolationsFile != null) { - throw DriverException("$ARG_STRICT_INPUT_FILES and $ARG_STRICT_INPUT_FILES_STACK may be specified only once") + throw DriverException("$ARG_STRICT_INPUT_FILES, $ARG_STRICT_INPUT_FILES_WARN and $ARG_STRICT_INPUT_FILES_STACK may be specified only once") } strictInputFiles = StrictInputFileMode.fromArgument(arg) @@ -2398,8 +2402,11 @@ class Options( "Otherwise, $PROGRAM_NAME adds in source roots implied by the source files", "$ARG_STRICT_INPUT_FILES <file>", "Do not read files that are not explicitly specified in the command line. " + "All violations are written to the given file. Reads on directories are always allowed, but " + - "$PROGRAM_NAME still trackes reads on directories that are not specified in the command line, " + + "$PROGRAM_NAME still tracks reads on directories that are not specified in the command line, " + "and write them to the file.", + "$ARG_STRICT_INPUT_FILES_WARN <file>", "Warn when files not explicitly specified on the command line are " + + "read. All violations are written to the given file. Reads on directories not specified in the command " + + "line are allowed but also logged.", "$ARG_STRICT_INPUT_FILES_STACK <file>", "Same as $ARG_STRICT_INPUT_FILES but also print stacktraces.", "$ARG_STRICT_INPUT_FILES_EXEMPT <files or dirs>", "Used with $ARG_STRICT_INPUT_FILES. Explicitly allow " + "access to files and/or directories (separated by `${File.pathSeparator}). Can also be " + diff --git a/src/test/java/com/android/tools/metalava/OptionsTest.kt b/src/test/java/com/android/tools/metalava/OptionsTest.kt index cf31e96..33cfc7f 100644 --- a/src/test/java/com/android/tools/metalava/OptionsTest.kt +++ b/src/test/java/com/android/tools/metalava/OptionsTest.kt @@ -407,8 +407,12 @@ Sandboxing: --strict-input-files <file> Do not read files that are not explicitly specified in the command line. All violations are written to the given file. Reads on directories are - always allowed, but metalava still trackes reads on directories that are - not specified in the command line, and write them to the file. + always allowed, but metalava still tracks reads on directories that are not + specified in the command line, and write them to the file. +--strict-input-files:warn <file> + Warn when files not explicitly specified on the command line are read. All + violations are written to the given file. Reads on directories not + specified in the command line are allowed but also logged. --strict-input-files:stack <file> Same as --strict-input-files but also print stacktraces. --strict-input-files-exempt <files or dirs> |