How can I force a build process to fail through a Unity editor script? - unity3d

I want to force the build process to fail if some validation conditions are not met.
I've tried using an IPreprocessBuildWithReport with no success:
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class BuildProcessor : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
// Attempt 1
// Does not compile because the 'BuildSummary.result' is read only
report.summary.result = BuildResult.Failed;
// Attempt 2
// Causes a log in the Unity editor, but the build still succeeds
throw new BuildFailedException("Forced fail");
}
}
Is there any way to programmatically force the build process to fail?
I'm using Unity 2018.3.8f1.

As of 2019.2.14f1, the correct way to stop the build is to throw a BuildFailedException.
Other exception types do not interrupt the build.
Derived exception types do not interrupt the build.
Logging errors most certainly do not interrupt the build.
This is how Unity handles exceptions in PostProcessPlayer:
try
{
postprocessor.PostProcess(args, out props);
}
catch (System.Exception e)
{
// Rethrow exceptions during build postprocessing as BuildFailedException, so we don't pretend the build was fine.
throw new UnityEditor.Build.BuildFailedException(e);
}
Just for clarity, this will NOT stop the build.:
// This is not precisely a BuildFailedException. So the build will go on and succeed.
throw new CustomBuildFailedException();
...
public class CustomBuildFailedException: BuildException() {}

You can use OnValidate() which seems to be exactly what you're looking for.
Let's say you want to make sure a reference to a UI Text component is not null before building, in the script that should have the text reference, you add
private void OnValidate()
{
if (text == null)
{
Debug.LogError("Text reference is null!");
}
}
Having Debug.LogError calls during the build process actually cause the build to fail.

Related

Skip exceptions in spring-batch and commit error in database

I'm using Spring batch to write a batch process and I'm having issues handling the exceptions.
I have a reader that fetches items from a database with an specific state. The reader passes the item to the processor step that can launch the exception MyException.class. When this exception is thrown I want to skip the item that caused that exception and continue reading the next one.
The issue here is that I need to change the state of that item in the database so it's not fetched again by the reader.
This is what I tried:
return this.stepBuilderFactory.get("name")
.<Input, Output>chunk(1)
.reader(reader())
.processor(processor())
.faultTolerant()
.skipPolicy(skipPolicy())
.writer(writer())
.build();
In my SkipPolicy class I have the next code:
public boolean shouldSkip(Throwable throwable, int skipCount) throws SkipLimitExceededException {
if (throwable instanceof MyException.class) {
// log the issue
// update the item that caused the exception in database so the reader doesn't return it again
return true;
}
return false;
}
With this code the exception is skipped and my reader is called again, however the SkipPolicy didn't commit the change or did a rollback, so the reader fetches the item and tries to process it again.
I also tried with an ExceptionHandler:
return this.stepBuilderFactory.get("name")
.<Input, Output>chunk(1)
.reader(reader())
.processor(processor())
.faultTolerant()
.skip(MyException.class)
.exceptionHandler(myExceptionHandler())
.writer(writer())
.build();
In my ExceptionHandler class I have the next code:
public void handleException(RepeatContext context, Throwable throwable) throws Throwable {
if (throwable.getCause() instanceof MyException.class) {
// log the issue
// update the item that caused the exception in database so the reader doesn't return it again
} else {
throw throwable;
}
}
With this solution the state is changed in the database, however it doesn't call the reader, instead it calls the method process of the processor() again, getting in an infinite loop.
I imagine I can use a listener in my step to handle the exceptions, but I don't like that solution because I will have to clone a lot of code asumming this exception could be launched in different steps/processors of my code.
What am I doing wrong?
EDIT: After a lot of tests and using different listeners like SkipListener, I couldn't achieve what I wanted, Spring Batch is always doing a rollback of my UPDATE.
Debugging this is what I found:
Once my listener is invoked and I update my item, the program enters the method write in the class FaultTolerantChunkProcessor (line #327).
This method will try the next code (copied from github):
try {
doWrite(outputs.getItems());
} catch (Exception e) {
status = BatchMetrics.STATUS_FAILURE;
if (rollbackClassifier.classify(e)) {
throw e;
}
/*
* If the exception is marked as no-rollback, we need to
* override that, otherwise there's no way to write the
* rest of the chunk or to honour the skip listener
* contract.
*/
throw new ForceRollbackForWriteSkipException(
"Force rollback on skippable exception so that skipped item can be located.", e);
}
The method doWrite (line #151) inside the class SimpleChunkProcessor will try to write the list of output items, however, in my case the list is empty, so in the line #159 (method writeItems) will launch an IndexOutOfBoundException, causing the ForceRollbackForWriteSkipException and doing the rollback I'm suffering.
If I override the class FaultTolerantChunkProcessor and I avoid writing the items if the list is empty, then everything works as intended, the update is commited and the program skips the error and calls the reader again.
I don't know if this is actually a bug or it's caused by something I'm doing wrong in my code.
A SkipListener is better suited to your use case than an ExceptionHandler in my opinion, as it gives you access to the item that caused the exception. With the exception handler, you need to carry the item in the exception or the repeat context.
Moreover, the skip listener allows you to know in which phase the exception happened (ie in read, process or write), while with the exception handler you need to find a way to detect that yourself. If the skipping code is the same for all phases, you can call the same method that updates the item's status in all the methods of the listener.

Breakpoints in NUnit and stopping Debugging - How to kill a Test?

My apologies, it is an odd use case but please bear with me.
I have a simple TestFixture (as shown below) that outside of calling a PrimeService to check whether or not a number is prime, logs to a file whenever a routine is hit (ie. OneTimeSetup, Setup, OneTimeTearDown etc.)
Can someone please explain to me why If I run the following scenario, the TestFixture runs in it's entirety?
I place a breakpoint in my Test right before my Assert.
I start a "Debug Tests" process in VS (2019)
When the test pauses (and it will on the first test naturally), I press the "Stop Debugging" button (shift F5)
If I go and open my log file, I will see that all 3 tests ran, as did the TearDowns and final Teardown.
My apologies, I just want to understand what is going on under the hood, and whether or not there is a way to kill a paused Test run.
Thanks.
[TestFixture]
public class PrimeService_IsPrimeShould
{
private NerdAnalysis.PrimeService _primeService;
private System.IO.StreamWriter file;
[OneTimeSetUp]
public void OneTimeSetup()
{
file = new System.IO.StreamWriter(#"C:\Users\Fozzy Bear\source\repos\NunitTutorial\MyFile.txt");
file.WriteLine("One time SetUp");
}
[SetUp]
public void Setup()
{
_primeService = new NerdAnalysis.PrimeService();
file.WriteLine("Setup");
}
[TestCase(-1)]
[TestCase(0)]
[TestCase(1)]
public void IsPrime_lessthan2_pass(int value)
{
file.WriteLine("Running test for value " + value);
var result = _primeService.IsPrime(value);
Assert.IsFalse(result, "${ value} should not be prime");
var breakLine = "break";
Assert.Pass();
}
[OneTimeTearDown]
public void FinalTearDown() {
file.WriteLine("Final Teardown");
file.Close();
file.Dispose();
}
[TearDown]
public void TearDown()
{
file.WriteLine("Tear down");
}
from http://msdn.microsoft.com/en-us/library/406kfbs1.aspx
Stop Debugging terminates the process you are debugging if the program
was launched from Visual Studio. If you attached to the process,
instead of launching it from Visual Studio, the process continues
running. If you want to terminate attached processes, you can
terminate a single process from the Processes window or terminate all
attached process with the Terminate All command.

Unity3d JsonUtility.FromJson() read from TextAsset works in app, fails in Test Runner

The following code works fine in my application (Unity 2019.3.0f6). It reads from Assets/Resources/lesson-text.json and writes the expected logs to the console:
// file to read lessons from
public TextAsset jsonFile;
internal JsonLessonList LoadLessonFromFile()
{
JsonLessonList testLessonList = JsonUtility.FromJson<JsonLessonList>(jsonFile.text);
foreach (JsonLesson lesson in testLessonList.jsonLessonList)
{
Debug.Log("Found lesson: " + lesson.Name);
}
return testLessonList;
}
I'm wanting to read the same file when using Unity's Test Runner:
[UnityTest]
public IEnumerator TestFileParsesOkTest()
{
JsonLessonList testLessonList = jsonReader.LoadLessonFromFile();
Assert.IsNotNull(testLessonList);
yield return null;
}
but I keep getting this exception:
TestFileParsesOkTest (0.019s)
Unhandled log message: '[Exception] NullReferenceException: Object reference not set to an instance of an object'. Use UnityEngine.TestTools.LogAssert.Expect
JsonReader.LoadLessonFromFile () (at Assets/Scripts/JsonReader.cs:68)
JsonReader.Start () (at Assets/Scripts/JsonReader.cs:37)
NullReferenceException: Object reference not set to an instance of an object
I know the file format is ok because it works from the app. I think the problem is that "TextAsset jsonFile" that is set through the unity editor is not being seen by the Test Runner. How do I make this work?
[Test]
public void JsonFileResourceTest()
{
Assert.IsNotNull(jsonReader.jsonFile);
}
results in:
JsonFileResourceTest (0.020s)
Expected: not null
But was: null
(The test driven development tag is because I got the very simplest read of a file with one field working, and now I want to back up and write a unit test for it and then write tests before coding going forward.)
I figured it out:
[SetUp]
public void Setup()
{
jsonReader = new GameObject().AddComponent<JsonReader>();
jsonReader.jsonFile = Resources.Load("lesson-test") as TextAsset;
}
// Verify class exists
[Test]
public void JsonReaderClassExists()
{
Assert.IsNotNull(jsonReader);
Assert.IsNotNull(jsonReader.jsonFile);
}

Eclipse (JDT) - performFinish method in wizard

I need to do something with wizards in Eclipse so I checked JDT how they have implemented wizards and found this weird code I don't understand.
It ignores the wizard scheduling rule (returned from getSchedulingRule) in case the code is called from already executing Job (it uses the scheduling rule of that Job). So if wizard needs the scheduling rule of entire workspace but the current thread is already executing any job, than the scheduling rule of this job is used instead, which can cause problems when the new runnable is executed in workspace. I added some comments to code so it is more clear.
Could any Eclipse expert explain why the try block is implemented as is (not just using getSchedulingRule)?
NewElementWizard
/**
* Returns the scheduling rule for creating the element.
* #return returns the scheduling rule
*/
protected ISchedulingRule getSchedulingRule() {
return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
}
/*
* #see Wizard#performFinish
*/
#Override
public boolean performFinish() {
IWorkspaceRunnable op= new IWorkspaceRunnable() {
#Override
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
finishPage(monitor);
} catch (InterruptedException e) {
throw new OperationCanceledException(e.getMessage());
}
}
};
try {
//TODO: i need explanation of this block. Wizard should be used
// from UI thread, so the code Job.getJobManager().currentJob()
// means that there is possible Job currently executed by UI thread.
// Ok now if there is a job, use its scheduling rule ignoring getSchedulingRule.
// This could be maybe to force that this new runnable isn't executed until this thread finishes
// its current Job. Okb but if the current Job rule isn't so powerfull as this wizard needs, what than?
// It will cause error when executing op, because the runnable will not have enough access
// cause ignoring getSchedulingRule...
ISchedulingRule rule= null;
Job job= Job.getJobManager().currentJob();
if (job != null)
rule= job.getRule();
IRunnableWithProgress runnable= null;
if (rule != null)
runnable= new WorkbenchRunnableAdapter(op, rule, true);
else
runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule());
getContainer().run(canRunForked(), true, runnable);
} catch (InvocationTargetException e) {
handleFinishException(getShell(), e);
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}
I'm not sure I can explain all of this but a key thing to note is that
Job.getJobManager().currentJob();
only returns the current job in the current thread. Since performFinish is normally run in the UI thread this would not be an ordinary background job. UIJob jobs run in the UI thread. It looks to me like this code is trying to pick up the rule from some UI job that the wizard or associated code has already started.
The true arguments on the call:
new WorkbenchRunnableAdapter(op, rule, true)
will cause WorkbenchRunnableAdapter to call
Job.getJobManager().transferRule(fRule, thread);
if the thread changes. I think this means the code is trying to keep the same rule in use throughout the execution of the runnable and whatever job was previously running.

How to hide the output of a console view?

I'm writing an Eclipse plug-in in which the user can interact with another process via the Console view (in this case, an interpreter), for example, evaluate expressions and so on.
Sometimes the program needs to ask the interpreter for certain values. These interactions however, shouldn't be shown in the console view to the user.
I have following instances:
private IProcess process;
private ILaunch launch;
private IStreamsProxy proxy;
the queries my program do are made via adding an IStreamListener to the proxy:
proxy.getOutputStreamMonitor().addListener(new IStreamListener(){
#Override
public void streamAppended(String response, IStreamMonitor arg1) {
doSomeStuffWiththeRepsonse(response);
}
});
while the listener is listening to the OutputStreamMonitor of the proxy, I don't want the response to pop up in the console view of the plugin.
How can I do that?
Okay, here is how I did it.
The launch system of Eclipse works as follows:
1. Implement a ILaunchConfigurationDelegate, the only method in this interface is launch, which recieves an ILaunchConfiguration, a mode, an ILaunch and an IProgressMonitor.
In my program, launch starts an inferiorProcess using DebugPlugin.exec() using a commandline argument. Then a new Process is created by calling DebugPlugin.newProcess() with the ILaunch, the inferiorProcess, the name for the interpreter and some attributes.
This method creates a new RuntimeProcess and adds it to the ILaunch and vice versa.
2. Define a LaunchConfigurationType by using the extension point org.eclipse.debug.core.launchConfigurationTypes and add it to the plugin.xml:
<extension
point="org.eclipse.debug.core.launchConfigurationTypes">
<launchConfigurationType
delegate="myplugin.MyLaunchConfigurationDelegate" (1)
id="myplugin.myExternalProgram" (2)
modes="run" (3)
name="MyExternalProgram" (4)
public="false"> (5)
</launchConfigurationType>
</extension>
The extension point gives the exact path to the ILaunchConfigurationDelegate class created as above (1) and an unqiue identifier (2) to retrieve the instance of ILaunchConfigurationType from the LaunchManager used to launch the program. (3) defines the modes it can run as, run and debug. The name (4) is later shown in the top bar of the console view. If you only want to access and launch your external program programmatically in your plug-in (and not via the Run drop-down menu) (5) must be set to false.
3. Create a class that stores the Instances of IProcess, ILaunch and IStreamsProxy and which calls apropiate methods to start the process and to write onto the streamsproxy. A method for starting the process could look like this:
// is the process already running?
public boolean isRunning() {
boolean result = false;
try {
if (this.process != null) {
result = true;
this.process.getExitValue();
result = false;
}
}
catch (DebugException exception) {
}
return result;
}
// start the process
public void start() {
try {
if (!isRunning()) {
// get the ILaunchConfigurationType from the platform
ILaunchConfigurationType configType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(myplugin.myExternalProgram);
// the ILaunchConfigurationType can't be changed or worked with, so get a WorkingCopy
ILaunchConfigurationWorkingCopy copy = configType.newInstance(null, "myExternalProgram");
this.launch = copy.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
IProcess[] processes = this.launch.getProcesses();
if (processes.length > 0) {
// get the IProcess instance from the launch
this.process = this.launch.getProcesses()[0];
// get the streamsproxy from the process
this.proxy = this.process.getStreamsProxy();
}
}
}
catch (CoreException exception) {
}
if (isRunning())
// bring up the console and show it in the workbench
showConsole();
}
public void showConsole() {
if (this.process != null && this.process.getLaunch() != null) {
IConsole console = DebugUITools.getConsole(this.process);
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewPart view = page.findView("org.eclipse.ui.console.ConsoleView");
if (view != null)
view.setFocus();
}
}
Now to the initial problem of the question
The IStreamsListener of the console view, which listens to the OutputStreamMonitor of the IStreamsProxy could not be retrieved and thus not being stopped of listening. Prints to the console could not be prevented. OutputStreamMonitor doesn't provide methods to get the current listeners. It is not possible to just subclass it and override/add some methods, because the important fields and methods are private.
http://www.java2s.com/Open-Source/Java-Document/IDE-Eclipse/debug/org/eclipse/debug/internal/core/OutputStreamMonitor.java.htm
Just copy the code and add a get-method for the fListeners field and change some method modifiers to public.
In order to get your own OutputStreamMonitor into the system, you need to create your own IStreamsProxy. Again only subclassing wont work, you need to copy the code again and make some changes.
http://www.java2s.com/Open-Source/Java-Document/IDE-Eclipse/debug/org/eclipse/debug/internal/core/StreamsProxy.java.htm
Important:
public class MyStreamsProxy implements IStreamsProxy, IStreamsProxy2 {
/**
* The monitor for the output stream (connected to standard out of the process)
*/
private MyOutputStreamMonitor fOutputMonitor;
/**
* The monitor for the error stream (connected to standard error of the process)
*/
private MyOutputStreamMonitor fErrorMonitor;
(...)
public MyStreamsProxy(Process process) {
if (process == null) {
return;
}
fOutputMonitor = new MyOutputStreamMonitor(process
.getInputStream());
fErrorMonitor = new MyOutputStreamMonitor(process
.getErrorStream());
fInputMonitor = new InputStreamMonitor(process
.getOutputStream());
fOutputMonitor.startMonitoring();
fErrorMonitor.startMonitoring();
fInputMonitor.startMonitoring();
}
The only thing remaining is providing your own IProcess that uses your IStreamsProxy. This time subclassing RuntimeProcess and overriding the method createStreamsProxy() is enough:
public class MyProcess extends RuntimeProcess {
public MyProcess(ILaunch launch, Process process, String name,
Map attributes) {
super(launch, process, name, attributes);
}
#Override
protected IStreamsProxy createStreamsProxy() {
String encoding = getLaunch().getAttribute(DebugPlugin.ATTR_CONSOLE_ENCODING);
return new MyStreamsProxy(getSystemProcess());
}
}
MyProcess is integrated by creating a new instance of it in the launch method in the ILaunchConfigurationDelegate instead of using DebugPlugin.newProcess().
Now it is possible to hide and expose the output of the console view.
/**
* Storage field for the console listener
*/
private IStreamListener oldListener;
/**
* Hides the output coming from the process so the user doesn't see it.
*/
protected void hideConsoleOutput() {
MyOutputStreamMonitor out
= (MyOutputStreamMonitor) this.process.getStreamsProxy().getOutputStreamMonitor();
List<IStreamListener> listeners = out.getListeners();
// the console listener
this.oldListener = listeners.get(0);
out.removeListener(this.oldListener);
}
/**
* Reverts the changes made by hideConsoleOutput() so the user sees the response from the process again.
*/
protected void exposeConsoleOutput() {
MyOutputStreamMonitor out
= (MyOutputStreamMonitor) this.process.getStreamsProxy().getOutputStreamMonitor();
out.addListener(oldListener);
this.oldListener = null;
}
The hide and expose methods have to be called before any other listeners are added. There might be a better solution, however, this works.
Previous answer does the trick and I was going with something similar first after hours of trying to solve this. Finally I ended up doing something a bit simpler, but also somewhat nastier...basically:
...
ILaunch launch = launcconf.launch(
ILaunchManager.RUN_MODE, monitor);
DebugUIPlugin.getDefault().
getProcessConsoleManager().launchRemoved(launch);
...
So, I'm basically telling the console manager listener methods that this lauch has already been removed and it removes the console. Seems to do the trick for me atleast.
i don't want the response to pop up in the console view of the plugin. how can i do that?
Well since that is your actual concern, then just toggle the button on the console view called "Show console when standard output changes". Way more of a simpler approach than all of this, and it can be turned back on/off.