Skip to content

Commit

Permalink
Limit number of reported tests in extended mode (#11)
Browse files Browse the repository at this point in the history
* Add extendedMaxErrors config value
* Fix documented default value for extendedMaxErrors
  • Loading branch information
mrubiosan authored and ngraf committed Jul 16, 2019
1 parent 6350cfd commit 6e85572
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Enable and configure the extension in your `codeception.yaml`
# Limit the size of error messages in extended mode. 0 = unlimited. Default value: 80
extendedMaxLength: 80
# Limit the amount of reported errors in extended mode. 0 = unlimited. Default value: 0
extendedMaxErrors: 10
# customize your message with additional prefix and/or suffix
messagePrefix: '*Smoke-Test*'
Expand All @@ -66,4 +69,4 @@ Example

Dependencies
-----
This package uses the package [maknz/slack](https://github.com/maknz/slack) to communicate with the Slack API.
This package uses the package [maknz/slack](https://github.com/maknz/slack) to communicate with the Slack API.
24 changes: 23 additions & 1 deletion src/Extension/SlackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class SlackExtension extends Extension
*/
protected $extendedMaxLength = 80;

/**
* @var int|null The maximum amount of errors to send
*/
protected $extendedMaxErrors = 0;

/**
* Setup Slack client and message object.
*
Expand Down Expand Up @@ -185,6 +190,10 @@ public function _initialize()
$this->extended = true;
}

if (isset($this->config['extendedMaxErrors'])) {
$this->extendedMaxErrors = max((int) $this->config['extendedMaxErrors'], 0);
}

if (isset($this->config['extendedMaxLength'])) {
$this->extendedMaxLength = intval($this->config['extendedMaxLength']);
}
Expand Down Expand Up @@ -286,8 +295,14 @@ private function sendFailMessage(TestResult $result)
*/
private function attachExtendedInformation(Message &$message, TestResult $result) {
$fields = [];
$failures = array_merge($result->failures(), $result->errors());
$omittedFailures = 0;
if ($this->extendedMaxErrors > 0) {
$omittedFailures = count($failures) - $this->extendedMaxErrors;
$failures = array_slice($failures, 0, $this->extendedMaxErrors);
}

foreach (array_merge($result->failures(), $result->errors()) as $failure) {
foreach ($failures as $failure) {
/**
* @var $failure TestFailure
*/
Expand All @@ -309,6 +324,13 @@ private function attachExtendedInformation(Message &$message, TestResult $result
];
}

if ($omittedFailures > 0) {
$fields[] = [
'title' => sprintf('%d other tests...', $omittedFailures),
'value' => '',
];
}

$message->attach([
'color' => 'danger',
'fields' => $fields
Expand Down

0 comments on commit 6e85572

Please sign in to comment.