<commit_before>// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.

library testing.log;

import 'chain.dart' show
    Result,
    Step;

import 'suite.dart' show
    Suite;

import 'test_description.dart' show
    TestDescription;

import 'test_dart/status_file_parser.dart' show
    Expectation;

/// ANSI escape code for moving cursor one line up.
/// See [CSI codes](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes).
const String cursorUp = "\u001b[1A";

/// ANSI escape code for erasing the entire line.
/// See [CSI codes](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes).
const String eraseLine = "\u001b[2K";

final Stopwatch wallclock = new Stopwatch()..start();

bool _isVerbose = const bool.fromEnvironment("verbose");

bool get isVerbose => _isVerbose;

void enableVerboseOutput() {
  _isVerbose = true;
}

void logTestComplete(int completed, int failed, int total,
    Suite suite, TestDescription description) {
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)}";
  }
  logProgress(message);
}

void logStepStart(int completed, int failed, int total,
    Suite suite, TestDescription description, Step step) {
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)} ${step.name}";
    if (step.isAsync) {
      message += "...";
    }
  }
  logProgress(message);
}

void logStepComplete(int completed, int failed, int total,
    Suite suite, TestDescription description, Step step) {
  if (!step.isAsync) return;
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)} ${step.name}!";
  }
  logProgress(message);
}

void logProgress(String message) {
  if (isVerbose) {
    print(message);
  } else {
    print("$eraseLine$message$cursorUp");
  }
}

String formatProgress(int completed, int failed, int total) {
  Duration elapsed = wallclock.elapsed;
  String percent = pad((completed / total * 100.0).toStringAsFixed(1), 5);
  String good = pad(completed, 5);
  String bad = pad(failed, 5);
  String minutes = pad(elapsed.inMinutes, 2, filler: "0");
  String seconds = pad(elapsed.inSeconds % 60, 2, filler: "0");
  return "[ $minutes:$seconds | $percent% | +$good | -$bad ]";
}

String formatTestDescription(Suite suite, TestDescription description) {
  return "${suite.name}/${description.shortName}";
}

void logMessage(Object message) {
  if (isVerbose) {
    print("$message");
  }
}

void logNumberedLines(String text) {
  if (isVerbose) {
    print(numberedLines(text));
  }
}

void logUnexpectedResult(Suite suite, TestDescription description,
    Result result, Set<Expectation> expectedOutcomes) {
  print("${eraseLine}UNEXPECTED: ${suite.name}/${description.shortName}");
  Uri statusFile = suite.statusFile;
  if (statusFile != null) {
    String path = statusFile.toFilePath();
    if (result.outcome == Expectation.PASS) {
      print("The test unexpectedly passed, please update $path.");
    } else {
      print("The test had the outcome ${result.outcome}, but the status file "
          "($path) allows these outcomes: ${expectedOutcomes.join(' ')}");
    }
  }
  String log = result.log;
  if (log.isNotEmpty) {
    print(log);
  }
  print(result.error);
  if (result.trace != null) {
    print(result.trace);
  }
}

void logSuiteComplete() {
  if (!isVerbose) {
    print("");
  }
}

void logUncaughtError(error, StackTrace stackTrace) {<commit_msg>Don't print errors that are null.<commit_after>// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.

library testing.log;

import 'chain.dart' show
    Result,
    Step;

import 'suite.dart' show
    Suite;

import 'test_description.dart' show
    TestDescription;

import 'test_dart/status_file_parser.dart' show
    Expectation;

/// ANSI escape code for moving cursor one line up.
/// See [CSI codes](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes).
const String cursorUp = "\u001b[1A";

/// ANSI escape code for erasing the entire line.
/// See [CSI codes](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes).
const String eraseLine = "\u001b[2K";

final Stopwatch wallclock = new Stopwatch()..start();

bool _isVerbose = const bool.fromEnvironment("verbose");

bool get isVerbose => _isVerbose;

void enableVerboseOutput() {
  _isVerbose = true;
}

void logTestComplete(int completed, int failed, int total,
    Suite suite, TestDescription description) {
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)}";
  }
  logProgress(message);
}

void logStepStart(int completed, int failed, int total,
    Suite suite, TestDescription description, Step step) {
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)} ${step.name}";
    if (step.isAsync) {
      message += "...";
    }
  }
  logProgress(message);
}

void logStepComplete(int completed, int failed, int total,
    Suite suite, TestDescription description, Step step) {
  if (!step.isAsync) return;
  String message = formatProgress(completed, failed, total);
  if (suite != null) {
    message += ": ${formatTestDescription(suite, description)} ${step.name}!";
  }
  logProgress(message);
}

void logProgress(String message) {
  if (isVerbose) {
    print(message);
  } else {
    print("$eraseLine$message$cursorUp");
  }
}

String formatProgress(int completed, int failed, int total) {
  Duration elapsed = wallclock.elapsed;
  String percent = pad((completed / total * 100.0).toStringAsFixed(1), 5);
  String good = pad(completed, 5);
  String bad = pad(failed, 5);
  String minutes = pad(elapsed.inMinutes, 2, filler: "0");
  String seconds = pad(elapsed.inSeconds % 60, 2, filler: "0");
  return "[ $minutes:$seconds | $percent% | +$good | -$bad ]";
}

String formatTestDescription(Suite suite, TestDescription description) {
  return "${suite.name}/${description.shortName}";
}

void logMessage(Object message) {
  if (isVerbose) {
    print("$message");
  }
}

void logNumberedLines(String text) {
  if (isVerbose) {
    print(numberedLines(text));
  }
}

void logUnexpectedResult(Suite suite, TestDescription description,
    Result result, Set<Expectation> expectedOutcomes) {
  print("${eraseLine}UNEXPECTED: ${suite.name}/${description.shortName}");
  Uri statusFile = suite.statusFile;
  if (statusFile != null) {
    String path = statusFile.toFilePath();
    if (result.outcome == Expectation.PASS) {
      print("The test unexpectedly passed, please update $path.");
    } else {
      print("The test had the outcome ${result.outcome}, but the status file "
          "($path) allows these outcomes: ${expectedOutcomes.join(' ')}");
    }
  }
  String log = result.log;
  if (log.isNotEmpty) {
    print(log);
  }
  if (result.error != null) {
    print(result.error);
    if (result.trace != null) {
      print(result.trace);
    }
  }
}

void logSuiteComplete() {
  if (!isVerbose) {
    print("");
  }
}

void logUncaughtError(error, StackTrace stackTrace) {
{'fraction_non_alphanumeric': 0.1361111111111111, 'fraction_numerical': 0.0068783068783068784, 'mean_word_length': 4.06769436997319, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '7172987', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2340, 'n_words': 648}