Skip to content

Commit

Permalink
Reduce level of verbose and option to disable ansi in action.yml (#109
Browse files Browse the repository at this point in the history
)

The commit updates the action.yml file by including a new 'extra' input
field that helps set individual options. Simultaneously, it eliminates
hardcoded `--ansi` and `-vvv` arguments from expected inputs, making the
configurations more flexible.
  • Loading branch information
SmetDenis authored Mar 29, 2024
1 parent d9d0ff9 commit 9897580
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 36 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# @see https://github.com/JBZoo/CI-Report-Converter
#

.git
.idea
.github
build
Expand All @@ -27,5 +26,4 @@ vendor
action.yml
box.json.dist
phpunit.xml.dist
Makefile
*.phar
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,6 @@ jobs:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- name: Save the current version
run: make build-version --no-print-directory

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ jobs:
fetch-depth: 0
ref: ${{ github.ref_name }}

- name: Save the current version
run: make build-version --no-print-directory

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand Down
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@
# @see https://github.com/JBZoo/Csv-Blueprint
#

FROM alpine:latest as preparatory
RUN apk add --no-cache make git
WORKDIR /tmp
COPY . /tmp
RUN make build-version

########################################################################################
FROM php:8.3-cli-alpine

# Install PHP extensions
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN install-php-extensions opcache @composer


# Install application
# run `make build-version` before!
ENV COMPOSER_ALLOW_SUPERUSER=1
COPY . /app
COPY ./.version /app/
COPY --from=preparatory /tmp/.version /app/.version
RUN cd /app \
&& composer install --no-dev \
--classmap-authoritative \
--no-progress \
--no-suggest \
--optimize-autoloader \
&& rm -rf /app/.git \
&& composer clear-cache \
&& chmod +x /app/csv-blueprint

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ build-version: ##@Project Save version info
$(eval LAST_COMMIT_DATE := $(shell git log -1 --format=%cI))
$(eval SHORT_COMMIT_HASH := $(shell git rev-parse --short HEAD))
$(eval STABLE_FLAG := $(shell git diff --quiet $(TAG) HEAD -- && echo "true" || echo "false"))
@echo "$(TAG)\n$(STABLE_FLAG)\n$(BRANCH)\n$(LAST_COMMIT_DATE)\n$(SHORT_COMMIT_HASH)" > `pwd`/.version
@echo "$(TAG) | $(STABLE_FLAG) | $(BRANCH) | $(LAST_COMMIT_DATE) | $(SHORT_COMMIT_HASH)" > `pwd`/.version
@echo "Version info saved to `pwd`/.version"
@cat `pwd`/.version

update: ##@Project Update dependencies
@echo "Composer flags: $(JBZOO_COMPOSER_UPDATE_FLAGS)"
Expand All @@ -73,7 +74,6 @@ demo-github: ##@Demo Run demo invalid CSV for GitHub Actions
# Docker ###############################################################################################################
docker-build: ##@Docker (Re-)build Docker image
$(call title,"Building Docker Image")
@make build-version
@docker build -t $(DOCKER_IMAGE) .

docker-demo: ##@Docker Run demo via Docker
Expand Down
14 changes: 10 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ inputs:
default: no
required: true

# Only for debbuging and profiling
extra:
description: >
ANSI output. You can disable ANSI colors if you want with `--no-ansi`.
Verbosity level: Available options: `-v`, `-vv`, `-vvv`
Add flag `--profile` if you want to see profiling info. Add details with `-vvv`.
Add flag `--debug` if you want to see more really deep details.
default: 'extra: --ansi'

runs:
using: 'docker'
image: 'docker://jbzoo/csv-blueprint'
env:
GITHUB_ACTIONS: 'true'
args:
- validate:csv
- '--csv'
Expand All @@ -58,5 +65,4 @@ runs:
- ${{ inputs.quick }}
- '--skip-schema'
- ${{ inputs.skip-schema }}
- '--ansi'
- '-vvv'
- ${{ inputs.extra }}
21 changes: 7 additions & 14 deletions csv-blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@
namespace JBZoo\CsvBlueprint;

\define('PATH_ROOT', __DIR__);
require_once __DIR__ . '/vendor/autoload.php';

$vendorPaths = [
__DIR__ . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php',
];

foreach ($vendorPaths as $file) {
if (\file_exists($file)) {
\define('JBZOO_AUTOLOAD_FILE', $file);
break;
}
}

require_once JBZOO_AUTOLOAD_FILE;
// Fix for GitHub actions. See action.yml
$_SERVER['argv'] = Utils::fixArgv($_SERVER['argv'] ?? []);
$_SERVER['argc'] = \count($_SERVER['argv']);

// Set default timezone
\date_default_timezone_set('UTC');

// Convert all errors to exceptions. Looks like we have critical case, and we need to stop or handle it.
// We have to do it becase tool uses 3rd-party libraries, and we can't trust them.
\set_error_handler(static function ($severity, $message, $file, $line): void {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
Expand Down
36 changes: 35 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ public static function getVersion(bool $showFull): string
return 'Version file not found';
}

$parts = \array_filter(\explode("\n", (string)\file_get_contents($versionFile)));
return self::parseVersion((string)\file_get_contents($versionFile), $showFull);
}

public static function parseVersion(string $content, bool $showFull): string
{
$parts = \array_map('trim', \explode('|', $content));
$expectedParts = 5;
if (\count($parts) < $expectedParts) {
return 'Invalid version file format';
Expand Down Expand Up @@ -367,6 +372,35 @@ public static function isPhpUnit(): bool
return \defined('PHPUNIT_COMPOSER_INSTALL') || \defined('__PHPUNIT_PHAR__');
}

public static function fixArgv(array $originalArgs): array
{
$newArgumens = [];

// Extract flags from the command line arguments `extra: --ansi --profile --debug -vvv`
foreach ($originalArgs as $argValue) {
$argValue = \trim($argValue);
if ($argValue === '') {
continue;
}

if (\str_starts_with($argValue, 'extra:')) {
$extraArgs = \str_replace('extra:', '', $argValue);
$flags = \array_filter(
\array_map('trim', \explode(' ', $extraArgs)),
static fn ($flag): bool => $flag !== '',
);

foreach ($flags as $flag) {
$newArgumens[] = $flag;
}
} else {
$newArgumens[] = $argValue;
}
}

return $newArgumens;
}

/**
* @param SplFileInfo[] $files
*/
Expand Down
13 changes: 8 additions & 5 deletions tests/GithubActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ public function testCreateCsvHelp(): void
$expectedArgs = ['validate:csv'];

foreach ($availableOptions as $option) {
$expectedArgs[] = "--{$option}";
$expectedArgs[] = '${{ inputs.' . $option . ' }}';
if ($option !== 'extra') {
$expectedArgs[] = "--{$option}";
}
$expectedArgs[] = "\${{ inputs.{$option} }}";
}

$expectedArgs[] = '--ansi';
$expectedArgs[] = '-vvv';

isSame($expectedArgs, $action->findArray('runs.args'));

isSame(
Expand All @@ -64,6 +63,10 @@ public function testGitHubActionsReadMe(): void
];

foreach ($inputs as $key => $input) {
if ($key === 'extra') {
continue;
}

$expectedMessage[] = ' # ' . \trim($input['description']);

if (isset($input['default'])) {
Expand Down
58 changes: 58 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,64 @@ public function testColorsTags(): void
}
}

public function testFixCliArguments(): void
{
isSame([], Utils::fixArgv([]));

isSame(['cmd'], Utils::fixArgv(['cmd']));
isSame(['cmd'], Utils::fixArgv(['cmd', '']));

isSame(['cmd', '-h'], Utils::fixArgv(['cmd', '', '-h']));
isSame(['cmd', '-h'], Utils::fixArgv(['cmd', '', '-h']));
isSame(['cmd', '-h'], Utils::fixArgv(['cmd', '', ' -h ']));
isSame(['cmd', '"-h"'], Utils::fixArgv(['cmd', '', ' "-h" ']));

isSame(
['cmd', '-h', '--ansi'],
Utils::fixArgv(['cmd', '', ' -h ', 'extra: --ansi']),
);
isSame(
['cmd', '-h'],
Utils::fixArgv(['cmd', '', ' -h ', 'extra:']),
);
isSame(
['cmd', '-h'],
Utils::fixArgv(['cmd', '', ' -h ', ' extra: ']),
);
isSame(
['cmd', '-h', '--ansi', '--no'],
Utils::fixArgv(['cmd', '', ' -h ', 'extra: --ansi --no']),
);
isSame(
['cmd', '-h', '--ansi', '--no'],
Utils::fixArgv(['cmd', '', ' -h ', 'extra: --ansi --no ']),
);
}

public function testParseVersion(): void
{
// Stable
isSame(
'<info>v0.34</info> 29 Mar 2024 18:09 UTC',
Utils::parseVersion("0.34 | true | master | 2024-03-29T22:09:21+04:00 | 03c14cc\n", true),
);
isSame('v0.34', Utils::parseVersion("0.34 | true | master | 2024-03-29T22:09:21+04:00 | 03c14cc\n", false));

// Development
isSame(
'<info>v0.34</info> 29 Mar 2024 18:09 UTC <comment>Experimental!</comment> Branch: branch (03c14cc)',
Utils::parseVersion("0.34 | false | branch | 2024-03-29T22:09:21+04:00 | 03c14cc\n", true),
);
isSame('v0.34', Utils::parseVersion("0.34 | false | branch | 2024-03-29T22:09:21+04:00 | 03c14cc\n", false));

// Night build
isSame(
'<info>v0.34</info> 29 Mar 2024 18:09 UTC <comment>Night build</comment> Branch: master (03c14cc)',
Utils::parseVersion("0.34 | false | master | 2024-03-29T22:09:21+04:00 | 03c14cc\n", true),
);
isSame('v0.34', Utils::parseVersion("0.34 | false | master | 2024-03-29T22:09:21+04:00 | 03c14cc\n", false));
}

public function testColorOfCellValue(): void
{
$packs = [
Expand Down

0 comments on commit 9897580

Please sign in to comment.