1#!/usr/bin/env python3 2# Copyright 2023 The Bazel Authors. All rights reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16 17import os 18import subprocess 19import unittest 20from pathlib import Path 21 22from rules_python.python.runfiles import runfiles 23 24 25class PipRepositoryEntryPointsTest(unittest.TestCase): 26 maxDiff = None 27 28 def test_entry_point_void_return(self): 29 env = os.environ.get("YAMLLINT_ENTRY_POINT") 30 self.assertIsNotNone(env) 31 32 r = runfiles.Create() 33 entry_point = Path(r.Rlocation(str(Path(*Path(env).parts[1:])))) 34 self.assertTrue(entry_point.exists()) 35 36 proc = subprocess.run( 37 [str(entry_point), "--version"], 38 check=True, 39 stdout=subprocess.PIPE, 40 stderr=subprocess.PIPE, 41 ) 42 self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.28.0") 43 44 # yamllint entry_point is of the form `def run(argv=None):` 45 with self.assertRaises(subprocess.CalledProcessError) as context: 46 subprocess.run( 47 [str(entry_point), "--option-does-not-exist"], 48 check=True, 49 stdout=subprocess.PIPE, 50 stderr=subprocess.PIPE, 51 ) 52 self.assertIn("returned non-zero exit status 2", str(context.exception)) 53 54 def test_entry_point_int_return(self): 55 env = os.environ.get("SPHINX_BUILD_ENTRY_POINT") 56 self.assertIsNotNone(env) 57 58 r = runfiles.Create() 59 entry_point = Path(r.Rlocation(str(Path(*Path(env).parts[1:])))) 60 self.assertTrue(entry_point.exists()) 61 62 proc = subprocess.run( 63 [str(entry_point), "--version"], 64 check=True, 65 stdout=subprocess.PIPE, 66 stderr=subprocess.PIPE, 67 ) 68 # sphinx-build uses args[0] for its name, only assert the version here 69 self.assertTrue(proc.stdout.decode("utf-8").strip().endswith("4.3.2")) 70 71 # sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:` 72 with self.assertRaises(subprocess.CalledProcessError) as context: 73 subprocess.run( 74 [entry_point, "--option-does-not-exist"], 75 check=True, 76 stdout=subprocess.PIPE, 77 stderr=subprocess.PIPE, 78 ) 79 self.assertIn("returned non-zero exit status 2", str(context.exception)) 80 81 82if __name__ == "__main__": 83 unittest.main() 84