Merge changes Icd25d2d0,I39ef10b0,I06bb80fe am: d3f5268dfb

Original change: https://android-review.googlesource.com/c/platform/build/+/1554917

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Iad2825ed6072f276b51594fb382b23220a6f6cc4
This commit is contained in:
Treehugger Robot
2021-02-12 05:25:01 +00:00
committed by Automerger Merge Worker
8 changed files with 184 additions and 36 deletions

View File

@@ -18,6 +18,7 @@ java_test_host {
static_libs: [ static_libs: [
"junit" "junit"
], ],
manifest: "TEST_MANIFEST.MF",
test_suites: ["general-tests"] test_suites: ["general-tests"]
} }

View File

@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Main-Class: com.android.build.config.TestRunner

View File

@@ -0,0 +1,39 @@
/*
* 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.build.config;
/**
* Exception to indicate that a fatal error has occurred. Throwing this
* will cause errors to be printed, cleanup to occur, and the command to
* exit with a failure code.
*
* These are user errors. Throwing other exceptions will result in
* the stack trace being shown.
*/
public class CommandException extends RuntimeException {
public CommandException() {
super();
}
public CommandException(String message) {
super(message);
}
public CommandException(String message, Throwable chain) {
super(message, chain);
}
}

View File

@@ -49,6 +49,16 @@ public class ErrorReporter {
*/ */
private boolean mHadError; private boolean mHadError;
public static class FatalException extends RuntimeException {
FatalException(String message) {
super(message);
}
FatalException(String message, Throwable chain) {
super(message, chain);
}
}
/** /**
* Whether errors are errors, warnings or hidden. * Whether errors are errors, warnings or hidden.
*/ */
@@ -127,6 +137,35 @@ public class ErrorReporter {
public String getHelp() { public String getHelp() {
return mHelp; return mHelp;
} }
/**
* Add an error with no source position.
*/
public void add(String message) {
ErrorReporter.this.add(this, false, new Position(), message);
}
/**
* Add an error.
*/
public void add(Position pos, String message) {
ErrorReporter.this.add(this, false, pos, message);
}
/**
* Add an error with no source position, and throw a FatalException, stopping processing
* immediately.
*/
public void fatal(String message) {
ErrorReporter.this.add(this, true, new Position(), message);
}
/**
* Add an error, and throw a FatalException, stopping processing immediately.
*/
public void fatal(Position pos, String message) {
ErrorReporter.this.add(this, true, pos, message);
}
} }
/** /**
@@ -154,6 +193,13 @@ public class ErrorReporter {
public String getMessage() { public String getMessage() {
return mMessage; return mMessage;
} }
@Override
public String toString() {
return mPosition
+ "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] "
+ mMessage;
}
} }
private void initLocked() { private void initLocked() {
@@ -190,23 +236,17 @@ public class ErrorReporter {
} }
} }
/**
* Add an error with no source position.
*/
public void add(Category category, String message) {
add(category, new Position(), message);
}
/** /**
* Add an error. * Add an error.
*/ */
public void add(Category category, Position pos, String message) { private void add(Category category, boolean fatal, Position pos, String message) {
synchronized (mEntries) { synchronized (mEntries) {
initLocked(); initLocked();
if (mCategories.get(category.getCode()) != category) { if (mCategories.get(category.getCode()) != category) {
throw new RuntimeException("Errors.Category used from the wrong Errors object."); throw new RuntimeException("Errors.Category used from the wrong Errors object.");
} }
mEntries.add(new Entry(category, pos, message)); final Entry entry = new Entry(category, pos, message);
mEntries.add(entry);
final Level level = category.getLevel(); final Level level = category.getLevel();
if (level == Level.WARNING || level == Level.ERROR) { if (level == Level.WARNING || level == Level.ERROR) {
mHadWarningOrError = true; mHadWarningOrError = true;
@@ -214,6 +254,9 @@ public class ErrorReporter {
if (level == Level.ERROR) { if (level == Level.ERROR) {
mHadError = true; mHadError = true;
} }
if (fatal) {
throw new FatalException(entry.toString());
}
} }
} }
@@ -250,13 +293,10 @@ public class ErrorReporter {
public void printErrors(PrintStream out) { public void printErrors(PrintStream out) {
synchronized (mEntries) { synchronized (mEntries) {
for (Entry entry: mEntries) { for (Entry entry: mEntries) {
final Category category = entry.getCategory(); if (entry.getCategory().getLevel() == Level.HIDDEN) {
final Level level = category.getLevel();
if (level == Level.HIDDEN) {
continue; continue;
} }
out.println(entry.getPosition() + "[" + level.getLabel() + " " out.println(entry.toString());
+ category.getCode() + "] " + entry.getMessage());
} }
} }
} }

View File

@@ -38,27 +38,45 @@ public class Main {
// TODO: Get the variables that were defined in starlark and use that to write // TODO: Get the variables that were defined in starlark and use that to write
// out the make, soong and bazel input files. // out the make, soong and bazel input files.
mErrors.ERROR_COMMAND_LINE.add("asdf");
throw new RuntimeException("poop");
} }
public static void main(String[] args) { public static void main(String[] args) {
Errors errors = new Errors(); Errors errors = new Errors();
int exitCode = 0;
try {
Options options = Options.parse(errors, args); Options options = Options.parse(errors, args);
if (errors.hadError()) { if (errors.hadError()) {
Options.printHelp(System.err); Options.printHelp(System.err);
System.err.println(); System.err.println();
errors.printErrors(System.err); throw new CommandException();
System.exit(1);
} }
switch (options.getAction()) { switch (options.getAction()) {
case DEFAULT: case DEFAULT:
(new Main(errors, options)).run(); (new Main(errors, options)).run();
errors.printErrors(System.err);
return; return;
case HELP: case HELP:
Options.printHelp(System.out); Options.printHelp(System.out);
return; return;
} }
} catch (CommandException ex) {
// These are user errors, so don't show a stack trace
exitCode = 1;
} catch (Throwable ex) {
// These are programming errors in the code of this tool, so print the exception.
// We'll try to print this. If it's something unrecoverable, then we'll hope
// for the best. We will still print the errors below, because they can be useful
// for debugging.
ex.printStackTrace(System.err);
System.err.println();
exitCode = 1;
} finally {
// Print errors and warnings
errors.printErrors(System.err);
}
System.exit(exitCode);
} }
} }

View File

@@ -96,14 +96,14 @@ public class Options {
mIndex++; mIndex++;
} }
} catch (ParseException ex) { } catch (ParseException ex) {
mErrors.add(mErrors.ERROR_COMMAND_LINE, ex.getMessage()); mErrors.ERROR_COMMAND_LINE.add(ex.getMessage());
} }
return mResult; return mResult;
} }
private void addWarning(Errors.Category category, String message) { private void addWarning(Errors.Category category, String message) {
mErrors.add(category, message); category.add(message);
} }
private String getNextNonFlagArg() { private String getNextNonFlagArg() {
@@ -133,12 +133,11 @@ public class Options {
final int code = requireNextNumberArg(arg); final int code = requireNextNumberArg(arg);
final Errors.Category category = mErrors.getCategories().get(code); final Errors.Category category = mErrors.getCategories().get(code);
if (category == null) { if (category == null) {
mErrors.add(mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR, mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR.add("Unknown error code: " + code);
"Unknown error code: " + code);
return; return;
} }
if (!category.isLevelSettable()) { if (!category.isLevelSettable()) {
mErrors.add(mErrors.ERROR_COMMAND_LINE, "Can't set level for error " + code); mErrors.ERROR_COMMAND_LINE.add("Can't set level for error " + code);
return; return;
} }
category.setLevel(level); category.setLevel(level);

View File

@@ -30,7 +30,7 @@ public class ErrorReporterTest {
public void testAdding() { public void testAdding() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.ERROR, new Position("a", 12), "Errrororrrr"); errors.ERROR.add(new Position("a", 12), "Errrororrrr");
Assert.assertTrue(errors.hadWarningOrError()); Assert.assertTrue(errors.hadWarningOrError());
Assert.assertTrue(errors.hadError()); Assert.assertTrue(errors.hadError());
@@ -66,7 +66,7 @@ public class ErrorReporterTest {
public void testWarning() { public void testWarning() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.WARNING, "Waaaaarninggggg"); errors.WARNING.add("Waaaaarninggggg");
Assert.assertTrue(errors.hadWarningOrError()); Assert.assertTrue(errors.hadWarningOrError());
Assert.assertFalse(errors.hadError()); Assert.assertFalse(errors.hadError());
@@ -80,7 +80,7 @@ public class ErrorReporterTest {
public void testHidden() { public void testHidden() {
TestErrors errors = new TestErrors(); TestErrors errors = new TestErrors();
errors.add(errors.HIDDEN, "Hidddeennn"); errors.HIDDEN.add("Hidddeennn");
Assert.assertFalse(errors.hadWarningOrError()); Assert.assertFalse(errors.hadWarningOrError());
Assert.assertFalse(errors.hadError()); Assert.assertFalse(errors.hadError());

View File

@@ -0,0 +1,49 @@
/*
* 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.build.config;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
public class TestRunner {
public static void main(String[] args) {
JUnitCore junit = new JUnitCore();
junit.addListener(new RunListener() {
@Override
public void testStarted(Description description) {
System.out.println("\nSTARTING: " + description.getDisplayName());
}
@Override
public void testFailure(Failure failure) {
System.out.println("FAILED: "
+ failure.getDescription().getDisplayName());
System.out.println(failure.getTrace());
}
});
Result result = junit.run(ErrorReporterTest.class,
OptionsTest.class);
if (!result.wasSuccessful()) {
System.out.println("\n*** FAILED ***");
}
}
}