Skip to content

Commit

Permalink
Update tests for entrypoint exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
gibfahn committed Oct 14, 2021
1 parent 4119f25 commit 45ce4d0
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 92 deletions.
21 changes: 18 additions & 3 deletions examples/pip_install/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ py_binary(

py_test(
name = "test",
size = "small",
srcs = ["test.py"],
deps = [":main"],
)

# 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.
# used from the generated `pip_parse` repository to access a runnable binary.
alias(
name = "yamllint",
actual = entry_point("yamllint"),
# If `pkg` and `script` are the same, passing a single string to
# `entry_point` would work as well: `entry_point("yamllint")`
actual = entry_point(
pkg = "yamllint",
script = "yamllint",
),
)

# Check that our compiled requirements are up-to-date
Expand All @@ -72,11 +78,19 @@ py_test(
":yamllint",
data_requirement("s3cmd"),
dist_info_requirement("boto3"),
entry_point(
pkg = "sphinx",
script = "sphinx-build",
),
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(entry_point(
pkg = "sphinx",
script = "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 Expand Up @@ -104,6 +118,7 @@ write_file(

diff_test(
name = "test_query_result",
size = "small",
file1 = "expected",
file2 = "yamllint_lib_by_version",
)
23 changes: 20 additions & 3 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,9 +19,26 @@ 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.assertTrue('returned non-zero exit status 2' in str(context.exception))
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")
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.

14 changes: 12 additions & 2 deletions examples/pip_parse/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ py_binary(

py_test(
name = "test",
size = "small",
srcs = ["test.py"],
deps = [":main"],
)
Expand All @@ -68,18 +69,27 @@ compile_pip_requirements(
requirements_txt = "requirements_lock.txt",
)

# Test the use of all pip_parse utilities in a single py_test
# Test the use of all pip_install utilities in a single py_test
py_test(
name = "pip_parse_test",
size = "small",
srcs = ["pip_parse_test.py"],
data = [
":yamllint",
data_requirement("s3cmd"),
dist_info_requirement("requests"),
entry_point(
pkg = "sphinx",
script = "sphinx-build",
),
],
env = {
"SPHINX_BUILD_ENTRY_POINT": "$(rootpath {})".format(entry_point(
pkg = "sphinx",
script = "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 45ce4d0

Please sign in to comment.