Skip to content

Commit

Permalink
Merge pull request #17 from salahhusa9/Git-Failed-Exception
Browse files Browse the repository at this point in the history
Git failed exception
  • Loading branch information
salahhusa9 committed Nov 11, 2023
2 parents 183b83c + ef11259 commit 6f3b804
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 14 deletions.
18 changes: 18 additions & 0 deletions src/Exceptions/GitFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Salahhusa9\Updater\Exceptions;

use Exception;

class GitFailedException extends Exception
{
/**
* GitFailedException constructor.
*
* @param string $message
*/
public function __construct($message)
{
parent::__construct($message);
}
}
29 changes: 15 additions & 14 deletions src/Helpers/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Process;
use Salahhusa9\Updater\Exceptions\GitFailedException;

class Git
{
Expand All @@ -12,14 +13,14 @@ class Git
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function getCurrentCommit()
{
$process = Process::run(self::gitPath().' log --pretty="%h" -n1 HEAD');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -30,14 +31,14 @@ public static function getCurrentCommit()
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function getCurrentBranch()
{
$process = Process::run(self::gitPath().' rev-parse --abbrev-ref HEAD');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -48,14 +49,14 @@ public static function getCurrentBranch()
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function getCurrentTag()
{
$process = Process::run(self::gitPath().' describe --tags --abbrev=0');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -66,14 +67,14 @@ public static function getCurrentTag()
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function auth()
{
$process = Process::run(self::gitPath().' remote set-url origin https://'.config('updater.github_username').':'.config('updater.github_token').'@github.com/'.config('updater.github_username').'/'.config('updater.github_repository').'.git');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -84,14 +85,14 @@ public static function auth()
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function pull()
{
$process = Process::run(self::gitPath().' pull');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -103,14 +104,14 @@ public static function pull()
* @param mixed $branch
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function checkout($branch)
{
$process = Process::run(self::gitPath().' checkout '.$branch);

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand All @@ -121,14 +122,14 @@ public static function checkout($branch)
*
* @return string
*
* @throws \Exception
* @throws GitFailedException
*/
public static function fetch()
{
$process = Process::run(self::gitPath().' fetch');

if ($process->failed()) {
throw new \Exception($process->errorOutput());
throw new GitFailedException('this command failed: '.$process->command().' with message: '.$process->errorOutput());
}

return trim($process->output());
Expand Down
223 changes: 223 additions & 0 deletions tests/GitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php

use Illuminate\Support\Facades\Process;
use Salahhusa9\Updater\Tests\TestCase;

class GitTest extends TestCase
{
public function test_get_current_commit(): void
{
Process::fake([
'git log --pretty="%h" -n1 HEAD' => Process::result('commit-hash'),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

$this->assertEquals('commit-hash', Salahhusa9\Updater\Helpers\Git::getCurrentCommit());
}

public function test_get_current_commit_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git log --pretty="%h" -n1 HEAD' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::getCurrentCommit();
}

public function test_get_current_branch(): void
{
Process::fake([
'git rev-parse --abbrev-ref HEAD' => Process::result('branch-name'),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

$this->assertEquals('branch-name', Salahhusa9\Updater\Helpers\Git::getCurrentBranch());
}

public function test_get_current_branch_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::getCurrentBranch();
}

public function test_get_current_tag(): void
{
Process::fake([
'git describe --tags --abbrev=0' => Process::result('tag-name'),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

$this->assertEquals('tag-name', Salahhusa9\Updater\Helpers\Git::getCurrentTag());
}

public function test_get_current_tag_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git describe --tags --abbrev=0' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

// change config git_path to git, because git path changed between windows and linux
config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::getCurrentTag();
}

public function test_get_git_path(): void
{
config()->set('updater.git_path', 'git');

$this->assertEquals('git', Salahhusa9\Updater\Helpers\Git::gitPath());
}

public function test_git_auth(): void
{
Process::fake([
'git remote set-url origin https://'.config('updater.github_username').':'.config('updater.github_token').'@github.com/'.config('updater.github_username').'/'.config('updater.github_repository').'.git' => Process::result(''),
]);

config()->set('updater.git_path', 'git');

$this->assertEquals('', Salahhusa9\Updater\Helpers\Git::auth());
}

public function test_git_auth_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git remote set-url origin https://'.config('updater.github_username').':'.config('updater.github_token').'@github.com/'.config('updater.github_username').'/'.config('updater.github_repository').'.git' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::auth();
}

public function test_git_pull(): void
{
Process::fake([
'git pull' => Process::result(''),
]);

config()->set('updater.git_path', 'git');

$this->assertEquals('', Salahhusa9\Updater\Helpers\Git::pull());
}

public function test_git_pull_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git pull' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::pull();
}

public function test_git_checkout(): void
{
Process::fake([
'git checkout branch-name' => Process::result(''),
]);

config()->set('updater.git_path', 'git');

$this->assertEquals('', Salahhusa9\Updater\Helpers\Git::checkout('branch-name'));
}

public function test_git_checkout_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git checkout branch-name' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::checkout('branch-name');
}

public function test_git_fetch(): void
{
Process::fake([
'git fetch' => Process::result(''),
]);

config()->set('updater.git_path', 'git');

$this->assertEquals('', Salahhusa9\Updater\Helpers\Git::fetch());
}

public function test_git_fetch_failed(): void
{
$this->expectException(Salahhusa9\Updater\Exceptions\GitFailedException::class);
$this->expectExceptionMessage('Test error output');

Process::fake([
'git fetch' => Process::result(
output: 'Test output',
errorOutput: 'Test error output',
exitCode: 1,
),
]);

config()->set('updater.git_path', 'git');

Salahhusa9\Updater\Helpers\Git::fetch();
}
}

0 comments on commit 6f3b804

Please sign in to comment.