Skip to content

Commit dc29616

Browse files
Add more tests
Fix some default settings
1 parent 3510585 commit dc29616

3 files changed

Lines changed: 129 additions & 26 deletions

File tree

src/Console/Command/Info.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
use CaptainHook\App\Console\IOUtil;
1515
use CaptainHook\App\Runner\Config\Reader;
16-
use Exception;
1716
use Symfony\Component\Console\Input\InputArgument;
1817
use Symfony\Component\Console\Input\InputInterface;
1918
use Symfony\Component\Console\Input\InputOption;
2019
use Symfony\Component\Console\Output\OutputInterface;
20+
use Throwable;
2121

2222
/**
2323
* Command to display configuration information
@@ -64,7 +64,13 @@ protected function configure(): void
6464
'list-config',
6565
's',
6666
InputOption::VALUE_NONE,
67-
'List all config settings'
67+
'List all action settings'
68+
)
69+
->addOption(
70+
'application-config',
71+
null,
72+
InputOption::VALUE_NONE,
73+
'List all application settings'
6874
)
6975
->addOption(
7076
'extensive',
@@ -98,11 +104,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
98104
->display(Reader::OPT_ACTIONS, $input->getOption('list-actions'))
99105
->display(Reader::OPT_CONDITIONS, $input->getOption('list-conditions'))
100106
->display(Reader::OPT_OPTIONS, $input->getOption('list-options'))
107+
->display(Reader::OPT_CONFIG, $input->getOption('list-config'))
108+
->display(Reader::OPT_SETTINGS, $input->getOption('application-config'))
101109
->extensive($input->getOption('extensive'))
102110
->run();
103111

104112
return 0;
105-
} catch (Exception $e) {
113+
} catch (Throwable $e) {
106114
return $this->crash($output, $e);
107115
}
108116
}

src/Runner/Config/Reader.php

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Reader extends Runner\RepositoryAware
3333
public const OPT_ACTIONS = 'actions';
3434
public const OPT_CONDITIONS = 'conditions';
3535
public const OPT_OPTIONS = 'options';
36+
public const OPT_CONFIG = 'config';
37+
public const OPT_SETTINGS = 'settings';
3638

3739
/**
3840
* The hook to display
@@ -42,9 +44,17 @@ class Reader extends Runner\RepositoryAware
4244
private array $hooks = [];
4345

4446
/**
47+
* Default display options
48+
*
4549
* @var array<string, bool>
4650
*/
47-
private array $options = [];
51+
private array $options = [
52+
self::OPT_ACTIONS => true,
53+
self::OPT_CONDITIONS => false,
54+
self::OPT_OPTIONS => false,
55+
self::OPT_CONFIG => false,
56+
self::OPT_SETTINGS => false,
57+
];
4858

4959
/**
5060
* Show more detailed information
@@ -109,20 +119,42 @@ public function run(): void
109119
if (!$this->config->isLoadedFromFile()) {
110120
throw new RuntimeException('No configuration to read');
111121
}
122+
$this->displaySettings();
123+
$this->io->write('<fg=magenta>Hooks:</>');
112124
foreach ($this->config->getHookConfigs() as $hookConfig) {
113125
$this->displayHook($hookConfig);
114126
}
115127
}
116128

129+
/**
130+
* Display the application settings
131+
*
132+
* @return void
133+
*/
134+
private function displaySettings(): void
135+
{
136+
if (!$this->show(self::OPT_SETTINGS)) {
137+
return;
138+
}
139+
$this->io->write('<fg=magenta>Config:</>');
140+
$this->io->write(' - <fg=cyan>Verbosity:</fg=cyan> ' . $this->config->getVerbosity());
141+
$this->io->write(' - <fg=cyan>Use colors:</fg=cyan> ' . $this->yesOrNo($this->config->useAnsiColors()));
142+
$this->io->write(' - <fg=cyan>Allow failures:</fg=cyan> ' . $this->yesOrNo($this->config->isFailureAllowed()));
143+
$this->io->write(' - <fg=cyan>Git directory:</fg=cyan> ' . $this->config->getGitDirectory());
144+
$this->io->write(' - <fg=cyan>Bootstrap file:</fg=cyan> ' . $this->config->getBootstrap());
145+
$this->io->write(' - <fg=cyan>Install mode:</fg=cyan> ' . $this->config->getRunConfig()->getMode());
146+
}
147+
117148
/**
118149
* Display a hook configuration
150+
*
119151
* @param \CaptainHook\App\Config\Hook $config
120152
* @return void
121153
*/
122154
private function displayHook(Config\Hook $config): void
123155
{
124156
if ($this->shouldHookBeDisplayed($config->getName())) {
125-
$this->io->write('<info>' . $config->getName() . '</info>', !$this->extensive);
157+
$this->io->write(' <info>' . $config->getName() . '</info>', !$this->extensive);
126158
$this->displayExtended($config);
127159
$this->displayActions($config);
128160
}
@@ -138,7 +170,7 @@ private function displayExtended(Config\Hook $config): void
138170
{
139171
if ($this->extensive) {
140172
$this->io->write(
141-
' ' . str_repeat('-', 52 - strlen($config->getName())) .
173+
' ' . str_repeat('-', 50 - strlen($config->getName())) .
142174
'--[enabled: ' . $this->yesOrNo($config->isEnabled()) .
143175
', installed: ' . $this->yesOrNo($this->repository->hookExists($config->getName())) . ']'
144176
);
@@ -166,8 +198,9 @@ private function displayActions(Config\Hook $config): void
166198
*/
167199
private function displayAction(Config\Action $action): void
168200
{
169-
$this->io->write(' - <fg=cyan>' . $action->getAction() . '</>');
201+
$this->io->write(' - <fg=cyan>' . $action->getAction() . '</>');
170202
$this->displayOptions($action->getOptions());
203+
$this->displayConfig($action);
171204
$this->displayConditions($action->getConditions());
172205
}
173206

@@ -182,16 +215,45 @@ private function displayOptions(Config\Options $options): void
182215
if (empty($options->getAll())) {
183216
return;
184217
}
185-
if ($this->show(self::OPT_OPTIONS)) {
186-
$this->io->write(' <comment>Options:</comment>');
187-
foreach ($options->getAll() as $key => $value) {
188-
$this->displayOption($key, $value);
218+
if (!$this->show(self::OPT_OPTIONS)) {
219+
return;
220+
}
221+
222+
$this->io->write(' <comment>Options:</comment>');
223+
foreach ($options->getAll() as $key => $value) {
224+
$this->displayOption($key, $value);
225+
}
226+
}
227+
228+
/**
229+
* Display all action config values
230+
*
231+
* @param \CaptainHook\App\Config\Action $action
232+
* @return void
233+
*/
234+
private function displayConfig(Config\Action $action): void
235+
{
236+
if (!$this->show(self::OPT_CONFIG)) {
237+
return;
238+
}
239+
240+
$config = [];
241+
if ($action->getLabel() != $action->getAction()) {
242+
$config['label'] = $action->getLabel();
243+
}
244+
if ($action->isFailureAllowed()) {
245+
$config['failureAllowed'] = true;
246+
}
247+
if (!empty($config)) {
248+
$this->io->write(' <comment>Config:</comment>');
249+
foreach ($config as $key => $value) {
250+
$this->io->write(' - ' . $key . ': <fg=gray>' . $value . '</>');
189251
}
190252
}
191253
}
192254

193255
/**
194-
* Display a singe option
256+
* Display a single option
195257
*
196258
* @param mixed $key
197259
* @param mixed $value
@@ -203,7 +265,7 @@ private function displayOption(mixed $key, mixed $value, string $prefix = ''): v
203265
if (is_array($value)) {
204266
$value = implode(', ', $value);
205267
}
206-
$this->io->write($prefix . ' - ' . $key . ': ' . $value);
268+
$this->io->write($prefix . ' - ' . $key . ': <fg=gray>' . $value . '</>');
207269
}
208270

209271
/**
@@ -218,13 +280,15 @@ private function displayConditions(array $conditions, string $prefix = ''): void
218280
if (empty($conditions)) {
219281
return;
220282
}
221-
if ($this->show(self::OPT_CONDITIONS)) {
222-
if (empty($prefix)) {
223-
$this->io->write($prefix . ' <comment>Conditions:</comment>');
224-
}
225-
foreach ($conditions as $condition) {
226-
$this->displayCondition($condition, $prefix);
227-
}
283+
if (!$this->show(self::OPT_CONDITIONS)) {
284+
return;
285+
}
286+
287+
if (empty($prefix)) {
288+
$this->io->write($prefix . ' <comment>Conditions:</comment>');
289+
}
290+
foreach ($conditions as $condition) {
291+
$this->displayCondition($condition, $prefix);
228292
}
229293
}
230294

@@ -237,7 +301,7 @@ private function displayConditions(array $conditions, string $prefix = ''): void
237301
*/
238302
private function displayCondition(Config\Condition $condition, string $prefix = ''): void
239303
{
240-
$this->io->write($prefix . ' - ' . $condition->getExec());
304+
$this->io->write($prefix . ' - <fg=cyan>' . $condition->getExec() . '</>');
241305

242306
if (in_array(strtoupper($condition->getExec()), ['OR', 'AND'])) {
243307
$conditions = [];
@@ -251,7 +315,7 @@ private function displayCondition(Config\Condition $condition, string $prefix =
251315
if (empty($condition->getArgs())) {
252316
return;
253317
}
254-
$this->io->write($prefix . ' <comment>Args:</comment>');
318+
$this->io->write($prefix . ' <comment>Args:</comment>');
255319
foreach ($condition->getArgs() as $key => $value) {
256320
$this->displayOption($key, $value, $prefix . ' ');
257321
}
@@ -266,9 +330,6 @@ private function displayCondition(Config\Condition $condition, string $prefix =
266330
*/
267331
private function show(string $option): bool
268332
{
269-
if (empty($this->options)) {
270-
return true;
271-
}
272333
return $this->options[$option] ?? false;
273334
}
274335

tests/unit/Runner/Config/ReaderTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,30 @@ public function testDoesNotAllowInvalidHookNames(): void
5151
->run();
5252
}
5353

54+
public function testItDisplaysActionConfig(): void
55+
{
56+
$path = realpath(CH_PATH_FILES . '/config/valid.json');
57+
$config = Config\Factory::create($path);
58+
$io = $this->createIOMock();
59+
$repo = $this->createRepositoryMock();
60+
61+
$io->expects($this->atLeast(2))->method('write');
62+
63+
$runner = new Reader($io, $config, $repo);
64+
$runner->setHook('pre-commit')
65+
->display(Reader::OPT_ACTIONS, true)
66+
->display(Reader::OPT_CONFIG, true)
67+
->run();
68+
}
69+
5470
public function testItDisplaysOnlyActions(): void
5571
{
5672
$path = realpath(CH_PATH_FILES . '/config/valid.json');
5773
$config = Config\Factory::create($path);
5874
$io = $this->createIOMock();
5975
$repo = $this->createRepositoryMock();
6076

61-
$io->expects($this->exactly(2))->method('write');
77+
$io->expects($this->atLeast(2))->method('write');
6278

6379
$runner = new Reader($io, $config, $repo);
6480
$runner->setHook('pre-commit')
@@ -91,6 +107,24 @@ public function testItDisplaysAll(): void
91107

92108
$io->expects($this->atLeast(4))->method('write');
93109

110+
$runner = new Reader($io, $config, $repo);
111+
$runner->display(Reader::OPT_CONFIG, true);
112+
$runner->display(Reader::OPT_OPTIONS, true);
113+
$runner->display(Reader::OPT_CONDITIONS, true);
114+
$runner->display(Reader::OPT_SETTINGS, true);
115+
$runner->run();
116+
}
117+
118+
119+
public function testItDisplaysConditionsWithoutOptions(): void
120+
{
121+
$path = realpath(CH_PATH_FILES . '/config/valid-with-nested-and-conditions.json');
122+
$config = Config\Factory::create($path);
123+
$io = $this->createIOMock();
124+
$repo = $this->createRepositoryMock();
125+
126+
$io->expects($this->atLeast(4))->method('write');
127+
94128
$runner = new Reader($io, $config, $repo);
95129
$runner->run();
96130
}

0 commit comments

Comments
 (0)