* [gentoo-commits] proj/metagen:master commit in: metagen/, /, metagen/tests/
@ 2025-01-31 17:41 Sebastian Pipping
0 siblings, 0 replies; only message in thread
From: Sebastian Pipping @ 2025-01-31 17:41 UTC (permalink / raw
To: gentoo-commits
commit: d697c3ac24cc324a62c68553e99f2d7aba491e5d
Author: Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq <DOT> in>
AuthorDate: Tue Nov 26 15:44:31 2024 +0000
Commit: Sebastian Pipping <sping <AT> gentoo <DOT> org>
CommitDate: Fri Jan 31 17:40:45 2025 +0000
URL: https://gitweb.gentoo.org/proj/metagen.git/commit/?id=d697c3ac
Add unit tests for testing CLI
This replaces custom shellscript runner.
Signed-off-by: Anna (cybertailor) Vyalkova <cyber+gentoo <AT> sysrq.in>
Signed-off-by: Sebastian Pipping <sping <AT> gentoo.org>
MANIFEST.in | 1 -
metagen/test_cli | 51 ---------------------------------------
metagen/tests/test_cli.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 52 deletions(-)
diff --git a/MANIFEST.in b/MANIFEST.in
index 00690d6..9a2248f 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,2 +1 @@
include docs/*
-include metagen/test_cli
diff --git a/metagen/test_cli b/metagen/test_cli
deleted file mode 100755
index fb394ca..0000000
--- a/metagen/test_cli
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/bin/bash
-metagen() {
- PYTHONPATH=. python3 -m metagen "$@"
-}
-
-handle_error() {
- ret=$?
- echo FAILED. >&2
- exit ${ret}
-}
-trap handle_error ERR
-
-unset ECHANGELOG_USER
-PS4='# '
-set -x
-########################################
-# BEGIN tests
-########################################
-
-
-#Should fail as -t ... is missing
-! metagen -m -Q
-! metagen -e mail@example.org -Q
-
-#Should fail if ECHANGELOG_USER not set:
-ECHANGELOG_USER='First Last <mail@example.org>' metagen -m -Q -t person
-! metagen -m -Q -t person
-
-metagen -e "someguy@gentoo.org" -d "Maint desc" -Q -t person
-
-metagen -e "someguy@gentoo.org" -n "Jon Doe" -d "Maint desc" -Q -t person
-
-#Should fail if ECHANGELOG_USER not set:
-ECHANGELOG_USER='First Last <mail@example.org>' metagen -m -e "foo@bar.com" -d "Foo bar.","Chow fun" -Q -t person
-! metagen -m -e "foo@bar.com" -d "Foo bar.","Chow fun" -Q -t person
-
-#Should fail:
-! metagen -Q -t person
-
-#Should fail:
-! metagen -l "Long desc" -Q -t person
-
-#Should fail:
-! metagen -d "Maintainer desc" -Q -t person
-
-
-########################################
-# END tests
-########################################
-echo
-echo PASSED.
diff --git a/metagen/tests/test_cli.py b/metagen/tests/test_cli.py
new file mode 100644
index 0000000..603f475
--- /dev/null
+++ b/metagen/tests/test_cli.py
@@ -0,0 +1,61 @@
+import os
+import sys
+import unittest
+from contextlib import redirect_stdout
+from io import StringIO
+from unittest.mock import patch
+
+from metagen.__main__ import main
+
+
+class TestCli(unittest.TestCase):
+
+ def test_missing_type(self):
+ self.assertExitFail("-m", "-Q")
+ self.assertExitFail("-e", "mail@example.org", "-Q")
+
+ def test_echangelog_user(self):
+ test_env = {"ECHANGELOG_USER": "First Last <mail@example.org>"}
+ test_args = ["-m", "-Q", "-t", "person"]
+
+ with patch.dict(os.environ, test_env):
+ self.assertExitSuccess(*test_args)
+
+ with patch.dict(os.environ, clear=True):
+ self.assertExitFail(*test_args)
+
+ def test_maint_desc(self):
+ self.assertExitSuccess("-e", "someguy@gentoo.org",
+ "-d", "Maint desc", "-Q", "-t", "person")
+ self.assertExitSuccess("-e", "someguy@gentoo.org",
+ "-d", "Maint desc", "-n", "Jon Doe",
+ "-Q", "-t", "person")
+
+ def test_echangelog_user_long(self):
+ test_env = {"ECHANGELOG_USER": "First Last <mail@example.org>"}
+ test_args = ["-m", "-e", "foo@bar.com",
+ "-d", "Foo bar.,Chow fun", "-Q", "-t", "person"]
+
+ with patch.dict(os.environ, test_env):
+ self.assertExitSuccess(*test_args)
+
+ with patch.dict(os.environ, clear=True):
+ self.assertExitFail(*test_args)
+
+ def test_missing_email(self):
+ self.assertExitFail("-Q", "-t", "person")
+ self.assertExitFail("-l", "Long desc", "-Q", "-t", "person")
+ self.assertExitFail("-d", "Maintainer desc", "-Q", "-t", "person")
+
+ def assertExitFail(self, *args):
+ with self.assertRaises(SystemExit) as cm:
+ self._run_metagen(*args)
+ self.assertEqual(cm.exception.code, 1)
+
+ def assertExitSuccess(self, *args):
+ self._run_metagen(*args)
+
+ def _run_metagen(self, *args):
+ with redirect_stdout(StringIO()):
+ with patch.object(sys, "argv", ["metagen", *args]):
+ main()
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2025-01-31 17:42 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-31 17:41 [gentoo-commits] proj/metagen:master commit in: metagen/, /, metagen/tests/ Sebastian Pipping
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox