|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +class PipRepositoryEntryPointsTest(unittest.TestCase): |
| 10 | + maxDiff = None |
| 11 | + |
| 12 | + def test_entry_point_void_return(self): |
| 13 | + env = os.environ.get("YAMLLINT_ENTRY_POINT") |
| 14 | + self.assertIsNotNone(env) |
| 15 | + |
| 16 | + entry_point = Path(env) |
| 17 | + self.assertTrue(entry_point.exists()) |
| 18 | + |
| 19 | + proc = subprocess.run( |
| 20 | + [str(entry_point), "--version"], |
| 21 | + check=True, |
| 22 | + stdout=subprocess.PIPE, |
| 23 | + stderr=subprocess.PIPE, |
| 24 | + ) |
| 25 | + self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3") |
| 26 | + |
| 27 | + # yamllint entry_point is of the form `def run(argv=None):` |
| 28 | + with self.assertRaises(subprocess.CalledProcessError) as context: |
| 29 | + subprocess.run( |
| 30 | + [str(entry_point), "--option-does-not-exist"], |
| 31 | + check=True, |
| 32 | + stdout=subprocess.PIPE, |
| 33 | + stderr=subprocess.PIPE, |
| 34 | + ) |
| 35 | + self.assertIn("returned non-zero exit status 2", str(context.exception)) |
| 36 | + |
| 37 | + def test_entry_point_int_return(self): |
| 38 | + env = os.environ.get("SPHINX_BUILD_ENTRY_POINT") |
| 39 | + self.assertIsNotNone(env) |
| 40 | + |
| 41 | + entry_point = Path(env) |
| 42 | + self.assertTrue(entry_point.exists()) |
| 43 | + |
| 44 | + proc = subprocess.run( |
| 45 | + [str(entry_point), "--version"], |
| 46 | + check=True, |
| 47 | + stdout=subprocess.PIPE, |
| 48 | + stderr=subprocess.PIPE, |
| 49 | + ) |
| 50 | + # sphinx-build uses args[0] for its name, only assert the version here |
| 51 | + self.assertTrue(proc.stdout.decode("utf-8").strip().endswith("4.3.2")) |
| 52 | + |
| 53 | + # sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:` |
| 54 | + with self.assertRaises(subprocess.CalledProcessError) as context: |
| 55 | + subprocess.run( |
| 56 | + [entry_point, "--option-does-not-exist"], |
| 57 | + check=True, |
| 58 | + stdout=subprocess.PIPE, |
| 59 | + stderr=subprocess.PIPE, |
| 60 | + ) |
| 61 | + self.assertIn("returned non-zero exit status 2", str(context.exception)) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + unittest.main() |
0 commit comments