Skip to content

Commit

Permalink
[TEST] Add functional test to ensure working -n (--no-interactive) mode
Browse files Browse the repository at this point in the history
EditorConfigCLI will ask the user to continue, if more than 500 files are found.
This needs to get confirmed interactively.

When --no-interaction (-n) is given, this check should get bypassed. Then, all files
no matter how much, will get processed.

Closes: #20
  • Loading branch information
a-r-m-i-n committed Oct 28, 2023
1 parent f16f5e7 commit e233b47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ The ``ec`` binary supports the following options:
| ``--compact`` | ``-c`` | Only shows only files with issues, not the issues itself. |
| ``--uncovered`` | ``-u`` | Lists all files which are not covered by .editorconfig. |
| ``--verbose`` | ``-v`` | Shows additional informations, like detailed info about internal time tracking and which binary files have been skipped. |
| ``--no-interaction`` | ``-n`` | Do not ask for confirmation, if more than 500 files found. |
| ``--no-interaction`` | ``-n`` | Do not ask for confirmation, if more than 500 files found and continue scanning. |
| ``--no-error-on-exit`` | | By default ``ec`` returns code 2 when issues occurred. With this option set return code is always 0. |

**Tip:** The "usage" section on ``ec``'s help page shows some examples.
Expand Down
2 changes: 0 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ protected function executing(Input $input, Output $output): int
}

if ($count > 500 && !$input->getOption('no-interaction') && !$io->confirm('Continue?', false)) {
// @codeCoverageIgnoreStart
$io->writeln('Canceled.');

return $returnValue; // Early return
// @codeCoverageIgnoreEnd
}

if (!empty($this->scanner->getSkippingRules())) {
Expand Down
64 changes: 64 additions & 0 deletions tests/Functional/EditorConfig/CommandNoInteractionModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);
namespace Armin\EditorconfigCli\Tests\Functional\EditorConfig;

use Armin\EditorconfigCli\Application;
use Symfony\Component\Console\Tester\CommandTester;

class CommandNoInteractionModeTest extends AbstractTestCase
{
protected $editorConfig = <<<TXT
root = true
[*]
insert_final_newline = true
TXT;

protected $files = [
'invalid.txt' => <<<TXT
Missing new line in this file.
TXT,
];

public function setUp(): void
{
parent::setUp();
// Create 500 valid text files
for ($i = 1; $i <= 500; $i++) {
file_put_contents($this->workspacePath . '/' . 'valid' . $i . '.txt', 'Test file no. ' . $i . PHP_EOL);
}
}

public function testAskForConfirmation(): void
{
$command = new Application();
$command->setAutoExit(false);
$commandTester = new CommandTester($command);
$commandTester->setInputs(['n']);
$commandTester->execute(['-d' => $this->workspacePath]);
self::assertStringContainsString('Found 501 files to scan.', $commandTester->getDisplay());
self::assertStringContainsString('Canceled.', $commandTester->getDisplay());

$commandTester = new CommandTester($command);
$commandTester->setInputs(['yes']);
$commandTester->execute(['-d' => $this->workspacePath]);
self::assertSame(2, $commandTester->getStatusCode());
self::assertStringContainsString('Found 1 issue in 1 file', $commandTester->getDisplay());
}

public function testSkipAskingForConfirmationInNoInteractionMode(): void
{
$command = new Application();
$command->setAutoExit(false);
$commandTester = new CommandTester($command);
$commandTester->execute(['-d' => $this->workspacePath, '-n' => true]);
self::assertStringContainsString('Found 501 files to scan.', $commandTester->getDisplay());
self::assertSame(2, $commandTester->getStatusCode());
self::assertStringContainsString('Found 1 issue in 1 file', $commandTester->getDisplay());

$commandTester = new CommandTester($command);
$commandTester->execute(['-d' => $this->workspacePath, '--no-interaction' => true]);
self::assertStringContainsString('Found 501 files to scan.', $commandTester->getDisplay());
self::assertSame(2, $commandTester->getStatusCode());
self::assertStringContainsString('Found 1 issue in 1 file', $commandTester->getDisplay());
}
}

0 comments on commit e233b47

Please sign in to comment.