Skip to content

Commit

Permalink
Error on change
Browse files Browse the repository at this point in the history
* return a status code 1 when a file has been changed, so that it will
  be failure in CI
* change the status updates
  • Loading branch information
oliverxchen committed Jun 5, 2022
1 parent 6fab9cc commit c98f29e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
22 changes: 16 additions & 6 deletions sqlean/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,25 @@ def main(
elif path.is_file():
sqlean_file(path, stats, sql_parser, options)
stats.print_summary(options)
is_passed = stats.is_passed()
if is_passed:
rprint("🧘 [bold white on green]All files passed[/bold white on green] 🧘")
elif is_passed is False:
rprint("🙀 [bold white on red]Some files failed[/bold white on red] 🙀")

red_style = "bold white on red"
orange_style = "bold white on #e6a902"
green_style = "bold white on green"
if stats.num_changed > 0:
rprint(f"👍 [{orange_style}]Some files were changed[/{orange_style}] 👍")
if stats.num_unparsable > 0:
rprint(f"🙀 [{red_style}]Some files were unparsable[/{red_style}] 🙀")
if stats.num_dirty > 0:
rprint(f"🙀 [{red_style}]Some files were dirty[/{red_style}] 🙀")
if stats.num_changed > 0 or stats.num_unparsable > 0 or stats.num_dirty > 0:
raise typer.Exit(code=1)
elif is_passed is None:

if stats.num_files == 0:
typer.echo("🤷 No files found 🤷")
raise typer.Exit(code=1)

rprint(f"🧘 [{green_style}]All files passed[/{green_style}] 🧘")


def sqlean_recursive(
target: Path, stats: Stats, sql_parser: Parser, options: Settings
Expand Down Expand Up @@ -133,6 +142,7 @@ def sqlean_unignored_file(
if options.write_ignore:
write_ignore_header(target)
stats.num_ignored += 1
stats.num_changed += 1
stats.newly_ignored_files.append(target)
else:
stats.num_unparsable += 1
Expand Down
8 changes: 1 addition & 7 deletions sqlean/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass, field
from pathlib import Path
import time
from typing import List, Optional
from typing import List

from rich import print as rprint
from rich.console import Console
Expand Down Expand Up @@ -38,12 +38,6 @@ def get_time_elapsed(self) -> str:
"""Return the time elapsed since the start"""
return f"{round(time.time() - self.start_time, 3)}s"

def is_passed(self) -> Optional[bool]:
"""Return True if all files are now ok"""
if self.num_files == 0:
return None
return self.num_clean + self.num_changed + self.num_ignored == self.num_files

def print_summary(self, options: Settings) -> None:
"""Prints a summary of the stats."""
if self.num_files == 0:
Expand Down
9 changes: 6 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_target__invalid() -> None:
def test_dryrun__runs_on_directory() -> None:
result = runner.invoke(app, ["-d", "."])
assert result.exit_code == 1
assert "Some files failed" in result.stdout
assert "Some files were unparsable" in result.stdout
assert "Some files were dirty" in result.stdout


def test_dryrun__missing_target() -> None:
Expand Down Expand Up @@ -69,7 +70,8 @@ def test_dryrun__fail() -> None:
assert "── Unparsable files ──" in result.stdout
assert "│ tests/fixtures/fail/dir_1/unparsable.sql" in result.stdout
assert "Summary" in result.stdout
assert "Some files failed" in result.stdout
assert "Some files were unparsable" in result.stdout
assert "Some files were dirty" in result.stdout


def test_dryrun_verbose__fail() -> None:
Expand Down Expand Up @@ -130,8 +132,9 @@ def test_write_ignore() -> None:

# run write-ignore once
result = runner.invoke(app, ["--write-ignore", tmpdir])
assert result.exit_code == 0
assert result.exit_code == 1
assert "── Newly ignored files ──" in result.stdout
assert "Changed/sqleaned files 50.0" in result.stdout
assert "Ignored files 33.3%" in result.stdout
assert "Unparsable files 0.0%" in result.stdout
with open(unparsable_file, "rt", encoding="utf-8") as reader:
Expand Down
12 changes: 0 additions & 12 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@
from sqlean.stats import Stats


def test_stats__is_passed() -> None:
stats = Stats()
assert stats.is_passed() is None
stats.num_files = 2
assert not stats.is_passed()
stats.num_clean = 2
assert stats.is_passed()
stats.num_clean = 1
stats.num_ignored = 1
assert stats.is_passed()


def test_stats__print_summary__no_files(capsys: CaptureFixture[str]) -> None:
stats = Stats()
stats.print_summary(options=Settings())
Expand Down

0 comments on commit c98f29e

Please sign in to comment.