Skip to content

Commit

Permalink
pip: entry_point: Add support for exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
gibfahn committed Nov 5, 2021
1 parent 98ffe06 commit ed55f1e
Show file tree
Hide file tree
Showing 11 changed files with 481 additions and 97 deletions.
13 changes: 12 additions & 1 deletion examples/pip_install/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ py_test(

# For pip dependencies which have entry points, the `entry_point` macro can be
# used from the generated `pip_install` repository to access a runnable binary.

alias(
name = "sphinx-build",
actual = entry_point(
pkg = "sphinx",
script = "sphinx-build",
),
)

alias(
name = "yamllint",
actual = entry_point("yamllint"),
Expand All @@ -68,14 +77,16 @@ py_test(
name = "pip_install_test",
srcs = ["pip_install_test.py"],
data = [
":sphinx-build",
":yamllint",
data_requirement("s3cmd"),
dist_info_requirement("boto3"),
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath :sphinx-build)",
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("boto3")),
"WHEEL_ENTRY_POINT": "$(rootpath :yamllint)",
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
},
)

Expand Down
25 changes: 23 additions & 2 deletions examples/pip_install/pip_install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class PipInstallTest(unittest.TestCase):
maxDiff = None

def test_entry_point(self):
env = os.environ.get("WHEEL_ENTRY_POINT")
def test_entry_point_void_return(self):
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
Expand All @@ -19,6 +19,27 @@ def test_entry_point(self):
proc = subprocess.run([entry_point, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")

# yamllint entry_point is of the form `def run(argv=None):`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run([entry_point, "--option-does-not-exist"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertIn('returned non-zero exit status 2', str(context.exception))

def test_entry_point_int_return(self):
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
self.assertTrue(entry_point.exists())

proc = subprocess.run([entry_point, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# sphinx-build uses args[0] for its name, only assert the version here
self.assertTrue(proc.stdout.decode("utf-8").strip().endswith('4.2.0'))

# sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run([entry_point, "--option-does-not-exist"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertIn('returned non-zero exit status 2', str(context.exception))

def test_data(self):
env = os.environ.get("WHEEL_DATA_CONTENTS")
self.assertIsNotNone(env)
Expand Down
3 changes: 2 additions & 1 deletion examples/pip_install/requirements.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
boto3==1.14.51
s3cmd==2.1.0
sphinx==4.2.0
yamllint==1.26.3
s3cmd==2.1.0
218 changes: 182 additions & 36 deletions examples/pip_install/requirements.txt

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions examples/pip_parse/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ py_test(

# For pip dependencies which have entry points, the `entry_point` macro can be
# used from the generated `pip_parse` repository to access a runnable binary.

alias(
name = "yamllint",
# If `pkg` and `script` are the same, passing a single string to
# `entry_point` would work as well: `entry_point("yamllint")`
name = "sphinx-build",
actual = entry_point(
pkg = "yamllint",
script = "yamllint",
pkg = "sphinx",
script = "sphinx-build",
),
)

alias(
name = "yamllint",
actual = entry_point("yamllint"),
)

# This rule adds a convenient way to update the requirements file.
compile_pip_requirements(
name = "requirements",
Expand All @@ -73,13 +77,15 @@ py_test(
name = "pip_parse_test",
srcs = ["pip_parse_test.py"],
data = [
":sphinx-build",
":yamllint",
data_requirement("s3cmd"),
dist_info_requirement("requests"),
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath :sphinx-build)",
"WHEEL_DATA_CONTENTS": "$(rootpaths {})".format(data_requirement("s3cmd")),
"WHEEL_DIST_INFO_CONTENTS": "$(rootpaths {})".format(dist_info_requirement("requests")),
"WHEEL_ENTRY_POINT": "$(rootpath :yamllint)",
"YAMLLINT_ENTRY_POINT": "$(rootpath :yamllint)",
},
)
25 changes: 23 additions & 2 deletions examples/pip_parse/pip_parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class PipInstallTest(unittest.TestCase):
maxDiff = None

def test_entry_point(self):
env = os.environ.get("WHEEL_ENTRY_POINT")
def test_entry_point_void_return(self):
env = os.environ.get("YAMLLINT_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
Expand All @@ -19,6 +19,27 @@ def test_entry_point(self):
proc = subprocess.run([entry_point, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertEqual(proc.stdout.decode("utf-8").strip(), "yamllint 1.26.3")

# yamllint entry_point is of the form `def run(argv=None):`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run([entry_point, "--option-does-not-exist"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertIn('returned non-zero exit status 2', str(context.exception))

def test_entry_point_int_return(self):
env = os.environ.get("SPHINX_BUILD_ENTRY_POINT")
self.assertIsNotNone(env)

entry_point = Path(env)
self.assertTrue(entry_point.exists())

proc = subprocess.run([entry_point, "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# sphinx-build uses args[0] for its name, only assert the version here
self.assertTrue(proc.stdout.decode("utf-8").strip().endswith('4.2.0'))

# sphinx-build entry_point is of the form `def main(argv: List[str] = sys.argv[1:]) -> int:`
with self.assertRaises(subprocess.CalledProcessError) as context:
subprocess.run([entry_point, "--option-does-not-exist"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.assertIn('returned non-zero exit status 2', str(context.exception))

def test_data(self):
env = os.environ.get("WHEEL_DATA_CONTENTS")
self.assertIsNotNone(env)
Expand Down
3 changes: 2 additions & 1 deletion examples/pip_parse/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests==2.25.1
s3cmd==2.1.0
sphinx==4.2.0
yamllint==1.26.3
s3cmd==2.1.0
Loading

0 comments on commit ed55f1e

Please sign in to comment.