Skip to content

Commit d008494

Browse files
committed
Fixes issue where no package name is set
1 parent d4d7d6e commit d008494

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

src/Commands/TreeCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ final class TreeCommand extends Command
1616
{
1717
private Tree $tree;
1818

19+
private const UNKNOWN_PACKAGE_NAME = 'unknown/unknown';
20+
1921
private string $directoryToOperateOn;
2022

2123
public function __construct(Tree $tree)
@@ -97,13 +99,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9799
protected function getPackageName(): string
98100
{
99101
if (!\file_exists($this->directoryToOperateOn . DIRECTORY_SEPARATOR . 'composer.json')) {
100-
return 'unknown/unknown';
102+
return self::UNKNOWN_PACKAGE_NAME;
101103
}
102104

103105
$composerContentAsJson = \json_decode(
104106
\file_get_contents($this->directoryToOperateOn . DIRECTORY_SEPARATOR . 'composer.json'),
105107
true
106108
);
109+
110+
if (!isset($composerContentAsJson['name'])) {
111+
return self::UNKNOWN_PACKAGE_NAME;
112+
}
113+
107114
return \trim($composerContentAsJson['name']);
108115
}
109116
}

src/Helpers/Str.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,4 @@ public function isWindows(string $os = PHP_OS): bool
1919

2020
return true;
2121
}
22-
23-
/**
24-
* Check if the operating system is macish.
25-
*
26-
* @param string $os
27-
*
28-
* @return boolean
29-
*/
30-
public function isMacOs(string $os = PHP_OS): bool
31-
{
32-
if (\strtoupper(\substr($os, 0, 3)) !== 'DAR') {
33-
return false;
34-
}
35-
36-
return true;
37-
}
3822
}

tests/Commands/TreeCommandTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Stolt\LeanPackage\Tests\CommandTester;
1212
use Stolt\LeanPackage\Tests\TestCase;
1313
use Stolt\LeanPackage\Tree;
14+
use Symfony\Component\Console\Command\Command;
1415

1516
class TreeCommandTest extends TestCase
1617
{
@@ -63,6 +64,49 @@ public function displaysExpectedSrcTree(): void
6364
]);
6465

6566
$this->assertStringContainsString('5 directories, 2 files', $commandTester->getDisplay());
67+
$this->assertStringContainsString('Package: test-src/package', $commandTester->getDisplay());
68+
$commandTester->assertCommandIsSuccessful();
69+
}
70+
71+
#[Test]
72+
public function displaysExpectedDefaultPackageName(): void
73+
{
74+
$command = $this->application->find('tree');
75+
$commandTester = new CommandTester($command);
76+
77+
$artifactFilenames = [
78+
'.gitattributes',
79+
'composer.json',
80+
];
81+
82+
$this->createTemporaryFiles(
83+
$artifactFilenames,
84+
['src', 'tests', '.github', 'docs', 'bin']
85+
);
86+
87+
$commandTester->execute([
88+
'command' => $command->getName(),
89+
'directory' => $this->temporaryDirectory,
90+
'--src' => true
91+
]);
92+
93+
$this->assertStringContainsString('unknown/unknown', $commandTester->getDisplay());
94+
$commandTester->assertCommandIsSuccessful();
95+
}
96+
97+
#[Test]
98+
public function displaysExpectedDistPackageTree(): void
99+
{
100+
$command = $this->application->find('tree');
101+
$commandTester = new CommandTester($command);
102+
103+
$commandTester->execute([
104+
'command' => $command->getName(),
105+
'directory' => '.'
106+
]);
107+
108+
$this->assertStringContainsString('2 directories, 1 file', $commandTester->getDisplay());
109+
$this->assertStringContainsString('Package: stolt/lean-package-validator', $commandTester->getDisplay());
66110
$commandTester->assertCommandIsSuccessful();
67111
}
68112
}

tests/TreeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stolt\LeanPackage\Tests;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Stolt\LeanPackage\Archive;
9+
use Stolt\LeanPackage\Exceptions\GitHeadNotAvailable;
10+
use Stolt\LeanPackage\Tree;
11+
12+
class TreeTest extends TestCase
13+
{
14+
#[Test]
15+
public function throwsExpectedExceptionWhenNoGitHeadAvailable(): void
16+
{
17+
$this->expectException(GitHeadNotAvailable::class);
18+
$this->expectExceptionMessage('No Git HEAD present to create an archive from.');
19+
20+
(new Tree(new Archive(\sys_get_temp_dir())))->getTreeForDistPackage(\sys_get_temp_dir());
21+
}
22+
}

0 commit comments

Comments
 (0)