Merge changes Icd25d2d0,I39ef10b0,I06bb80fe

* changes:
  Rearrange the methods on ErrorReporter to be more convenient.
  Add glue to run product-config-test as a standalone commandline executable.
  CommandException to cleanly exit product-config on error.
This commit is contained in:
Treehugger Robot 2021-02-12 04:03:44 +00:00 committed by Gerrit Code Review
commit d3f5268dfb
8 changed files with 184 additions and 36 deletions

View file

@ -18,6 +18,7 @@ java_test_host {
static_libs: [
"junit"
],
manifest: "TEST_MANIFEST.MF",
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;
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.
*/
@ -127,6 +137,35 @@ public class ErrorReporter {
public String getHelp() {
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() {
return mMessage;
}
@Override
public String toString() {
return mPosition
+ "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] "
+ mMessage;
}
}
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.
*/
public void add(Category category, Position pos, String message) {
private void add(Category category, boolean fatal, Position pos, String message) {
synchronized (mEntries) {
initLocked();
if (mCategories.get(category.getCode()) != category) {
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();
if (level == Level.WARNING || level == Level.ERROR) {
mHadWarningOrError = true;
@ -214,6 +254,9 @@ public class ErrorReporter {
if (level == Level.ERROR) {
mHadError = true;
}
if (fatal) {
throw new FatalException(entry.toString());
}
}
}
@ -250,13 +293,10 @@ public class ErrorReporter {
public void printErrors(PrintStream out) {
synchronized (mEntries) {
for (Entry entry: mEntries) {
final Category category = entry.getCategory();
final Level level = category.getLevel();
if (level == Level.HIDDEN) {
if (entry.getCategory().getLevel() == Level.HIDDEN) {
continue;
}
out.println(entry.getPosition() + "[" + level.getLabel() + " "
+ category.getCode() + "] " + entry.getMessage());
out.println(entry.toString());
}
}
}

View file

@ -38,27 +38,45 @@ public class Main {
// TODO: Get the variables that were defined in starlark and use that to write
// out the make, soong and bazel input files.
mErrors.ERROR_COMMAND_LINE.add("asdf");
throw new RuntimeException("poop");
}
public static void main(String[] args) {
Errors errors = new Errors();
int exitCode = 0;
Options options = Options.parse(errors, args);
if (errors.hadError()) {
Options.printHelp(System.err);
try {
Options options = Options.parse(errors, args);
if (errors.hadError()) {
Options.printHelp(System.err);
System.err.println();
throw new CommandException();
}
switch (options.getAction()) {
case DEFAULT:
(new Main(errors, options)).run();
return;
case HELP:
Options.printHelp(System.out);
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(1);
}
switch (options.getAction()) {
case DEFAULT:
(new Main(errors, options)).run();
errors.printErrors(System.err);
return;
case HELP:
Options.printHelp(System.out);
return;
}
System.exit(exitCode);
}
}

View file

@ -96,14 +96,14 @@ public class Options {
mIndex++;
}
} catch (ParseException ex) {
mErrors.add(mErrors.ERROR_COMMAND_LINE, ex.getMessage());
mErrors.ERROR_COMMAND_LINE.add(ex.getMessage());
}
return mResult;
}
private void addWarning(Errors.Category category, String message) {
mErrors.add(category, message);
category.add(message);
}
private String getNextNonFlagArg() {
@ -133,12 +133,11 @@ public class Options {
final int code = requireNextNumberArg(arg);
final Errors.Category category = mErrors.getCategories().get(code);
if (category == null) {
mErrors.add(mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR,
"Unknown error code: " + code);
mErrors.WARNING_UNKNOWN_COMMAND_LINE_ERROR.add("Unknown error code: " + code);
return;
}
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;
}
category.setLevel(level);

View file

@ -30,7 +30,7 @@ public class ErrorReporterTest {
public void testAdding() {
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.hadError());
@ -66,7 +66,7 @@ public class ErrorReporterTest {
public void testWarning() {
TestErrors errors = new TestErrors();
errors.add(errors.WARNING, "Waaaaarninggggg");
errors.WARNING.add("Waaaaarninggggg");
Assert.assertTrue(errors.hadWarningOrError());
Assert.assertFalse(errors.hadError());
@ -80,7 +80,7 @@ public class ErrorReporterTest {
public void testHidden() {
TestErrors errors = new TestErrors();
errors.add(errors.HIDDEN, "Hidddeennn");
errors.HIDDEN.add("Hidddeennn");
Assert.assertFalse(errors.hadWarningOrError());
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 ***");
}
}
}