Skip to content
Permalink
Browse files

[FIXED JENKINS-13932] Build can now be failed if package installation…

… fails.
  • Loading branch information...
orrc committed Apr 11, 2013
1 parent bcb3412 commit 59e2ec54506437d93054f86babe99b65b7ee2960
@@ -14,10 +14,14 @@
import hudson.plugins.android_emulator.sdk.Tool;
import hudson.plugins.android_emulator.util.Utils;
import hudson.tasks.Builder;
import hudson.util.ForkOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

@@ -32,11 +36,15 @@
/** Whether the APK should be uninstalled from the device before installation. */
private final boolean uninstallFirst;

/** Whether to fail the build if installation isn't successful. */
private final boolean failOnInstallFailure;

@DataBoundConstructor
@SuppressWarnings("hiding")
public InstallBuilder(String apkFile, boolean uninstallFirst) {
public InstallBuilder(String apkFile, boolean uninstallFirst, boolean failOnInstallFailure) {
this.apkFile = Util.fixEmptyAndTrim(apkFile);
this.uninstallFirst = uninstallFirst;
this.failOnInstallFailure = failOnInstallFailure;
}

public String getApkFile() {
@@ -47,6 +55,10 @@ public boolean shouldUninstallFirst() {
return uninstallFirst;
}

public boolean shouldFailBuildOnFailure() {
return failOnInstallFailure;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
@@ -86,11 +98,17 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

// Execute installation
AndroidEmulator.log(logger, Messages.INSTALLING_APK(apkPath.getName()));
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ForkOutputStream forkStream = new ForkOutputStream(logger, stdout);
String args = String.format("%s install -r \"%s\"", deviceIdentifier, apkPath.getName());
Utils.runAndroidTool(launcher, build.getEnvironment(TaskListener.NULL), logger, logger,
Utils.runAndroidTool(launcher, build.getEnvironment(TaskListener.NULL), forkStream, logger,
androidSdk, Tool.ADB, args, apkPath.getParent());

// TODO: Evaluate success/failure and fail the build (if the user said we should do so)
Pattern p = Pattern.compile("^Success$", Pattern.MULTILINE);
boolean success = p.matcher(stdout.toString()).find();
if (!success && failOnInstallFailure) {
return false;
}
return true;
}

@@ -7,8 +7,14 @@

<f:entry help="${resURL}/plugin/android-emulator/help-uninstallFirst.html">
<f:checkbox id="android-emulator.uninstallFirst" name="android-emulator.uninstallFirst"
title="${%Uninstall existing APK first}"
checked="${instance.shouldUninstallFirst()}" />
<label class="attach-previous">${%Uninstall existing APK first}</label>
</f:entry>

<f:entry help="${resURL}/plugin/android-emulator/help-failOnInstallFailure.html">
<f:checkbox id="android-emulator.failOnInstallFailure" name="android-emulator.failOnInstallFailure"
title="${%Fail the build if installation fails}"
checked="${h.defaultToTrue(instance.shouldFailBuildOnFailure())}" />
</f:entry>

</j:jelly>
@@ -0,0 +1,2 @@
If the given APK fails to be installed (i.e. the Android package manager does not return "Success")
and this option is enabled, then the build will be marked as failed.

0 comments on commit 59e2ec5

Please sign in to comment.
You can’t perform that action at this time.