<commit_before>package com.jvoegele.gradle.tasks.android

import com.jvoegele.gradle.tasks.android.exceptions.InstrumentationTestsFailedException;

class InstrumentationTestsTask extends AdbExec {

  def InstrumentationTestsTask() {
    logger.info("Running instrumentation tests...")
    
    //TODO: make the test runner configurable or parse it from the manifest
    def testRunner  = 'android.test.InstrumentationTestRunner'
    def testPackage = ant['manifest.package']
    
    args "shell", "am instrument", "-w", "$testPackage/$testRunner"
  } 
  
  @Override
  def checkForErrors(stream) {
    def reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()))
    reader.eachLine {
      if (it.matches("FAILURES!!!")) {
        throw new InstrumentationTestsFailedException();
      }
    }<commit_msg>only check stdout for build failures; turn test failure check into a positive check (i.e. test for success), since the ADB tool currently reports errors via stdout.<commit_after>package com.jvoegele.gradle.tasks.android

import com.jvoegele.gradle.tasks.android.exceptions.InstrumentationTestsFailedException;
import com.jvoegele.gradle.tasks.android.exceptions.AdbErrorException;

class InstrumentationTestsTask extends AdbExec {

  def InstrumentationTestsTask() {
    logger.info("Running instrumentation tests...")
    
    //TODO: make the test runner configurable or parse it from the manifest
    def testRunner  = 'android.test.InstrumentationTestRunner'
    def testPackage = ant['manifest.package']
    
    args "shell", "am instrument", "-w", "$testPackage/$testRunner"
  } 
  
  @Override
  def checkForErrors(stream) {
    def reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray()))
    if (stream == stdout) {
      detectBuildFailures(reader)
    } else { // stderr
      detectAdbErrors(reader);
    }
  }
  
  private void detectBuildFailures(def reader) {
    def success = false
    // ADB currently fails with errors on stdout, so we literally have to check
    // for 'OK' to 
    reader.eachLine {
      if (it.matches("^OK \\(([0-9]+ tests){1}\\)")) {
        success = true
        return
      }
    }
    
    if (!success) {
      throw new InstrumentationTestsFailedException();
    }
  }
  
  // at the moment, ADB errors are actually reported via stdout instead of stderr.
  // this method may become useful though once Google fixes that.
  private void detectAdbErrors(def reader) {
    if (reader.getText().trim()) {
      // at the moment, we treat any non-empty text on stderr as an error
      throw new AdbErrorException("There was an error while trying to run the instrumentation tests")
    }
{'fraction_non_alphanumeric': 0.09690644800596347, 'fraction_numerical': 0.0011181513231457323, 'mean_word_length': 3.465890183028286, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144221', 'n_tokens_mistral': 770, 'n_tokens_neox': 746, 'n_words': 222}