Skip to content

Commit

Permalink
Remove colors from CLI output on CI
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Moennich <[email protected]>
  • Loading branch information
OmeGak and ThiefMaster committed Sep 20, 2023
1 parent 98c49b7 commit 0491d28
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Renamed CLI `--ci` flag to `--check`.
- Removed Unicode interpuct from CLI output.
- Removed colors from CLI output when CI envvar is set.

## v1.1.1

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ To run Unbeheader in `check` mode, use the `--check` flag:
unbehead --check
```

It is possible to disable colors in the output by setting the `CI` environment variable to a truthy value:

```sh
export CI=1
```

## Configuration

Unbeheader reads its configuration from `.header.yaml` files placed in the file tree of the project. It is possible to override configuration values by placing `.header.yaml` files in subdirectories. This is useful when different headers are needed for different parts of the project. It is also possible to exclude a directory by placing an empty `.no-header` file in it.
Expand Down
22 changes: 17 additions & 5 deletions src/unbeheader/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,24 @@ def _do_update_header(file_path: Path, config: dict, regex: Pattern[str], commen
if content == orig_content:
return False
# Print header update results
if found:
msg = 'Incorrect header in %{white!}{}' if check else 'Updating header in %{white!}{}'
else:
msg = 'Missing header in %{white!}{}' if check else 'Adding header in %{white!}{}'
print(cformat(msg).format(os.path.relpath(file_path)))
_print_results(file_path, found=found, check=check)
# Write the updated file to disk
if not check:
file_path.write_text(content)
return True


def _print_results(file_path: Path, found: bool, check: bool):
ci = os.environ.get('CI') in {'1', 'true'}
if found:
check_msg = 'Incorrect header in {}' if ci else 'Incorrect header in %{white!}{}'
fix_msg = 'Updating header in {}' if ci else 'Updating header in %{white!}{}'
msg = check_msg if check else fix_msg
else:
check_msg = 'Missing header in {}' if ci else 'Missing header in %{white!}{}'
fix_msg = 'Adding header in {}' if ci else 'Adding header in %{white!}{}'
msg = check_msg if check else fix_msg
if ci:
print(msg.format(os.path.relpath(file_path)))
else:
print(cformat(msg).format(os.path.relpath(file_path)))
49 changes: 37 additions & 12 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
from unittest import mock

import pytest
from colorclass import Color

from unbeheader import SUPPORTED_FILES
from unbeheader.config import DEFAULT_SUBSTRING
from unbeheader.headers import _do_update_header
from unbeheader.headers import _generate_header
from unbeheader.headers import _print_results
from unbeheader.headers import update_header

COLOR_RESET = Color('{/all}')


@pytest.fixture
def config():
Expand Down Expand Up @@ -91,6 +95,7 @@ def test_update_header_for_current_dir(_do_update_header, get_config, tmp_path):
assert _do_update_header.call_count == 0


@mock.patch('unbeheader.headers._print_results')
@pytest.mark.parametrize(('before_content', 'after_content'), (
# Test that files with only header are kept empty
('''
Expand Down Expand Up @@ -171,16 +176,16 @@ def test_update_header_for_current_dir(_do_update_header, get_config, tmp_path):
print('Beware of the knowledge you will gain.')
'''),
))
def test_do_update_header(before_content, after_content, capsys, config, create_py_file, py_files_settings):
def test_do_update_header(_print_results, before_content, after_content, config, create_py_file, py_files_settings):
content = dedent(before_content)[1:] # Remove indentation and leading newline
file_path = create_py_file(content)
result = _do_update_header(file_path, config, check=False, **py_files_settings)
captured = capsys.readouterr()
assert result is True
assert 'Updating header' in captured.out
assert file_path.read_text() == dedent(after_content).lstrip()
_print_results.assert_called_once_with(file_path, found=True, check=False)


@mock.patch('unbeheader.headers._print_results')
@pytest.mark.parametrize(('before_content', 'after_content'), (
# Test that header is added in file missing it
('''
Expand All @@ -206,15 +211,14 @@ def test_do_update_header(before_content, after_content, capsys, config, create_
print('Beware of the knowledge you will gain.')
'''),
))
def test_do_update_header_for_not_found(before_content, after_content, capsys, config,
def test_do_update_header_for_not_found(_print_results, before_content, after_content, config,
create_py_file, py_files_settings):
content = dedent(before_content)[1:] # Remove indentation and leading newline
file_path = create_py_file(content)
result = _do_update_header(file_path, config, check=False, **py_files_settings)
captured = capsys.readouterr()
assert result is True
assert 'Adding header' in captured.out
assert file_path.read_text() == dedent(after_content).lstrip()
_print_results.assert_called_once_with(file_path, found=False, check=False)


def test_do_update_header_for_no_changes(config, create_py_file, py_files_settings):
Expand All @@ -229,6 +233,7 @@ def test_do_update_header_for_no_changes(config, create_py_file, py_files_settin
assert result is False


@mock.patch('unbeheader.headers._print_results')
@pytest.mark.parametrize(('file_content', 'header_found'), (
('''
# This file is part of Thelema.
Expand All @@ -238,17 +243,14 @@ def test_do_update_header_for_no_changes(config, create_py_file, py_files_settin
print('Beware of the knowledge you will gain.')
''', False),
))
def test_do_update_header_for_check(file_content, header_found, capsys, config, create_py_file, py_files_settings):
def test_do_update_header_for_check(_print_results, file_content, header_found, config,
create_py_file, py_files_settings):
file_content = dedent(file_content).lstrip()
file_path = create_py_file(file_content)
result = _do_update_header(file_path, config, check=True, **py_files_settings)
captured = capsys.readouterr()
assert result is True
assert open(file_path).read() == file_content
if header_found:
assert 'Incorrect header' in captured.out
else:
assert 'Missing header' in captured.out
_print_results.assert_called_once_with(file_path, found=header_found, check=True)


def test_do_update_header_for_empty_file(create_py_file, py_files_settings):
Expand Down Expand Up @@ -314,3 +316,26 @@ def test_generate_header_for_invalid_placeholder(template, config):
with pytest.raises(SystemExit) as exc:
_generate_header(data)
assert exc.value.code == 1


@pytest.mark.parametrize(('found', 'check', 'expected'), (
(True, True, 'Incorrect header'),
(True, False, 'Updating header'),
(False, True, 'Missing header'),
(False, False, 'Adding header'),
))
def test_print_results(found, check, expected, monkeypatch, tmp_path, capsys):
monkeypatch.delenv('CI', raising=False)
_print_results(tmp_path, found, check)
captured = capsys.readouterr()
assert expected in captured.out
assert COLOR_RESET in captured.out

@pytest.mark.parametrize(('envvar'), (
('1'), ('true'),
))
def test_print_results_for_ci(envvar, monkeypatch, tmp_path, capsys):
monkeypatch.setenv('CI', envvar)
_print_results(tmp_path, True, True)
captured = capsys.readouterr()
assert COLOR_RESET not in captured.out

0 comments on commit 0491d28

Please sign in to comment.