commit:     047ac2bdcfe019107b13646825818a0bc5339b9d
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Thu Aug 24 17:25:48 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Thu Aug 24 17:48:51 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgdev.git/commit/?id=047ac2bd

push: `--ask` stops for confirmation on warnings

Resolves: https://github.com/pkgcore/pkgdev/issues/150
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgdev/scripts/pkgdev_push.py |  9 ++++++++-
 tests/scripts/test_pkgdev_push.py | 23 +++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/pkgdev/scripts/pkgdev_push.py 
b/src/pkgdev/scripts/pkgdev_push.py
index 7948336..cf510ab 100644
--- a/src/pkgdev/scripts/pkgdev_push.py
+++ b/src/pkgdev/scripts/pkgdev_push.py
@@ -2,6 +2,7 @@ import argparse
 import shlex
 
 from pkgcheck import reporters, scan
+from pkgcheck.results import Warning as PkgcheckWarning
 from snakeoil.cli import arghparse
 from snakeoil.cli.input import userquery
 
@@ -58,9 +59,12 @@ def _push(options, out, err):
 
     # scan commits for QA issues
     pipe = scan(options.scan_args)
+    has_warnings = False
     with reporters.FancyReporter(out) as reporter:
         for result in pipe:
             reporter.report(result)
+            if result.level == PkgcheckWarning.level:
+                has_warnings = True
 
     # fail on errors unless they're ignored
     if pipe.errors:
@@ -68,7 +72,10 @@ def _push(options, out, err):
             out.write(out.bold, out.fg("red"), "\nFAILURES", out.reset)
             for result in sorted(pipe.errors):
                 reporter.report(result)
-        if not (options.ask and userquery("Push commits anyway?", out, err)):
+        if not (options.ask and userquery("Push commits anyway?", out, err, 
default_answer=False)):
+            return 1
+    elif has_warnings and options.ask:
+        if not userquery("warnings detected, push commits anyway?", out, err, 
default_answer=False):
             return 1
 
     # push commits upstream

diff --git a/tests/scripts/test_pkgdev_push.py 
b/tests/scripts/test_pkgdev_push.py
index f1f0a0b..fb4faa3 100644
--- a/tests/scripts/test_pkgdev_push.py
+++ b/tests/scripts/test_pkgdev_push.py
@@ -124,3 +124,26 @@ class TestPkgdevPush:
         ), pytest.raises(SystemExit) as excinfo, 
chdir(self.child_git_repo.path):
             self.script()
         assert excinfo.value.code == 0
+
+    def test_warnings(self, capsys):
+        pkgdir = os.path.dirname(self.child_repo.create_ebuild("cat/pkg-1"))
+        os.makedirs((filesdir := pjoin(pkgdir, "files")), exist_ok=True)
+        with open(pjoin(filesdir, "foo"), "w") as f:
+            f.write("")
+        self.child_git_repo.add_all("cat/pkg-1")
+
+        # scans with warnings ask for confirmation before pushing with "--ask"
+        with patch("sys.argv", self.args + ["--ask"]), patch(
+            "sys.stdin", StringIO("n\n")
+        ), pytest.raises(SystemExit) as excinfo, 
chdir(self.child_git_repo.path):
+            self.script()
+        assert excinfo.value.code == 1
+        out, err = capsys.readouterr()
+        assert "EmptyFile" in out
+
+        # but without "--ask" it still pushes
+        with patch("sys.argv", self.args), pytest.raises(SystemExit) as 
excinfo, chdir(
+            self.child_git_repo.path
+        ):
+            self.script()
+        assert excinfo.value.code == 0

Reply via email to