Skip to content

Check solution file exists in run command #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace PhpSchool\PhpWorkshop\Command;

use PhpSchool\PhpWorkshop\Check\FileExistsCheck;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
use PhpSchool\PhpWorkshop\ExerciseRepository;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshop\Result\Success;
use PhpSchool\PhpWorkshop\UserState;

/**
Expand Down Expand Up @@ -60,6 +62,11 @@ public function __construct(
*/
public function __invoke(Input $input): void
{
if (!file_exists($input->getRequiredArgument('program'))) {
$this->output->printError(sprintf('File: "%s" does not exist', $input->getRequiredArgument('program')));
return;
}

$exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise());
$this->exerciseDispatcher->run($exercise, $input, $this->output);
}
Expand Down
30 changes: 28 additions & 2 deletions test/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PhpSchool\PhpWorkshopTest\Command;

use Colors\Color;
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
use PhpSchool\PhpWorkshopTest\BaseTest;
use PhpSchool\Terminal\Terminal;
use PhpSchool\PhpWorkshop\Command\RunCommand;
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
Expand All @@ -13,11 +15,11 @@
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
use PHPUnit\Framework\TestCase;

class RunCommandTest extends TestCase
class RunCommandTest extends BaseTest
{
public function test(): void
{
$input = new Input('appName', ['program' => 'solution.php']);
$input = new Input('appName', ['program' => $this->getTemporaryFile('solution.php')]);

$exercise = new CliExerciseImpl();
$repo = new ExerciseRepository([$exercise]);
Expand All @@ -37,4 +39,28 @@ public function test(): void
$command = new RunCommand($repo, $dispatcher, $state, $output);
$command->__invoke($input);
}

public function testWithNonExistingFile(): void
{
$input = new Input('appName', ['program' => 'solution.php']);

$exercise = new CliExerciseImpl();
$repo = new ExerciseRepository([$exercise]);

$state = new UserState();
$state->setCurrentExercise('my-exercise');
$color = new Color();
$color->setForceStyle(true);
$output = new StdOutput($color, $this->createMock(Terminal::class));

$dispatcher = $this->createMock(ExerciseDispatcher::class);
$dispatcher
->expects($this->never())
->method('run');

$this->expectOutputString(file_get_contents(__DIR__ . '/../res/app-run-missing-solution-expected.txt'));

$command = new RunCommand($repo, $dispatcher, $state, $output);
$command->__invoke($input);
}
}
5 changes: 5 additions & 0 deletions test/res/app-run-missing-solution-expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

 
 File: "solution.php" does not exist