Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally validate entry point at build time #521

Merged
merged 37 commits into from
Jul 23, 2018
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6a45d41
initial
wisechengyi Jul 7, 2018
df5e696
can verify file existence
wisechengyi Jul 8, 2018
1e5c2c4
can verify both
wisechengyi Jul 8, 2018
67e7010
works fine
wisechengyi Jul 9, 2018
bf58ade
cmt
wisechengyi Jul 9, 2018
fec3647
rev init
wisechengyi Jul 9, 2018
89eaf6d
nit
wisechengyi Jul 9, 2018
b07e726
use import to check entry point validity
wisechengyi Jul 18, 2018
1756370
fmt
wisechengyi Jul 18, 2018
f9136a9
option
wisechengyi Jul 18, 2018
883d13a
flag works
wisechengyi Jul 18, 2018
ca5fa38
reorder sig
wisechengyi Jul 18, 2018
a2cd12a
diff module and method
wisechengyi Jul 18, 2018
738dd48
fmt
wisechengyi Jul 18, 2018
1e23269
add tests
wisechengyi Jul 18, 2018
394bb7f
fix comment
wisechengyi Jul 18, 2018
973cdf1
refactor tests
wisechengyi Jul 18, 2018
c255c28
minor fix
wisechengyi Jul 18, 2018
5fd3791
pr
wisechengyi Jul 18, 2018
3af0997
style
wisechengyi Jul 18, 2018
b49056a
back
wisechengyi Jul 18, 2018
c04c2b1
move into pex
wisechengyi Jul 18, 2018
a2403f3
minor fix
wisechengyi Jul 18, 2018
b60cc7b
fmt
wisechengyi Jul 20, 2018
4d07117
minor
wisechengyi Jul 20, 2018
70701bb
execute a file containing import
wisechengyi Jul 20, 2018
6ef2990
remove dup code
wisechengyi Jul 20, 2018
94de45c
privatize do_entry_point_verification
wisechengyi Jul 20, 2018
08bb959
fmt
wisechengyi Jul 20, 2018
469ea27
test_pex.py starts to work
wisechengyi Jul 21, 2018
7f1c478
pex tests
wisechengyi Jul 21, 2018
f60595a
integration tests
wisechengyi Jul 21, 2018
ac0bf83
style
wisechengyi Jul 21, 2018
23eb42e
encoding
wisechengyi Jul 21, 2018
9903ec0
further fix encoding
wisechengyi Jul 21, 2018
8ccf87c
skip pypy
wisechengyi Jul 21, 2018
9ec096a
for pr
wisechengyi Jul 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pex tests
wisechengyi committed Jul 21, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7f1c478196725777017f4c984266381413559dcd
8 changes: 3 additions & 5 deletions pex/pex.py
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ class PEX(object): # noqa: T000

class Error(Exception): pass
class NotFound(Error): pass
class InvalidEntryPoint(Error): pass

@classmethod
def clean_environment(cls):
@@ -525,9 +526,6 @@ def run(self, args=(), with_chroot=False, blocking=True, setsid=False, **kwargs)

def _do_entry_point_verification(self):

class InvalidEntryPoint(Exception):
pass

entry_point = self._pex_info.entry_point
ep_split = entry_point.split(':')

@@ -544,13 +542,13 @@ class InvalidEntryPoint(Exception):
ep_method = ep_split[1]
import_statement = 'from {} import {}'.format(ep_module, ep_method)
else:
raise InvalidEntryPoint("Failed to parse: `{}`".format(entry_point))
raise self.InvalidEntryPoint("Failed to parse: `{}`".format(entry_point))

with named_temporary_file() as fp:
fp.write(import_statement)
fp.close()
retcode = self.run([fp.name], env={'PEX_INTERPRETER': '1'})
if retcode != 0:
raise InvalidEntryPoint('Invalid entry point: `{}`\n'
raise self.InvalidEntryPoint('Invalid entry point: `{}`\n'
'Entry point verification failed: `{}`'
.format(entry_point, import_statement))
42 changes: 20 additions & 22 deletions tests/test_pex.py
Original file line number Diff line number Diff line change
@@ -262,7 +262,6 @@ def test_pex_paths():
@contextmanager
def _add_test_hello_to_pex(ep):
with temporary_dir() as td:
target = os.path.join(td, 'foo.pex')
hello_file = "\n".join([
"def hello():",
" print('hello')",
@@ -275,39 +274,38 @@ def _add_test_hello_to_pex(ep):
pex_builder.add_source(tf.name, 'test.py')
pex_builder.set_entry_point(ep)
pex_builder.freeze()
yield (target, pex_builder)
yield pex_builder


def test_pex_verify_entry_point_method_should_pass():
with _add_test_hello_to_pex('test:hello') as (target, pex_builder):
with _add_test_hello_to_pex('test:hello') as pex_builder:
# No error should happen here because `test:hello` is correct
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)


def test_pex_verify_entry_point_module_should_pass():
with _add_test_hello_to_pex('test') as (target, pex_builder):
with _add_test_hello_to_pex('test') as pex_builder:
# No error should happen here because `test` is correct
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)

#
#
# def test_pex_builder_verify_entry_point_method_should_fail():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('test:invalid_entry_point')
#
# # Expect InvalidEntryPoint due to invalid entry point method
# with pytest.raises(PEXBuilder.InvalidEntryPoint):
# pex_builder.build(target, verify_entry_point=True)
#
#
# def test_pex_builder_verify_entry_point_module_should_fail():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('invalid.module')
#
# # Expect InvalidEntryPoint due to invalid entry point module
# with pytest.raises(PEXBuilder.InvalidEntryPoint):
# pex_builder.build(target, verify_entry_point=True)

def test_pex_verify_entry_point_method_should_fail():
with _add_test_hello_to_pex('test:invalid_entry_point') as pex_builder:
# Expect InvalidEntryPoint due to invalid entry point method
with pytest.raises(PEX.InvalidEntryPoint):
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)


def test_pex_verify_entry_point_module_should_fail():
with _add_test_hello_to_pex('invalid.module') as pex_builder:
# Expect InvalidEntryPoint due to invalid entry point module
with pytest.raises(PEX.InvalidEntryPoint):
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)
56 changes: 1 addition & 55 deletions tests/test_pex_builder.py
Original file line number Diff line number Diff line change
@@ -3,10 +3,9 @@

import os
import stat
from contextlib import contextmanager

import pytest
from twitter.common.contextutil import temporary_dir, temporary_file
from twitter.common.contextutil import temporary_dir
from twitter.common.dirutil import safe_mkdir

from pex.common import open_zip
@@ -180,56 +179,3 @@ def build_and_check(path, copy):

build_and_check(td2, False)
build_and_check(td3, True)


@contextmanager
def _add_test_hello_to_pex():
with temporary_dir() as td:
target = os.path.join(td, 'foo.pex')
hello_file = "\n".join([
"def hello():",
" print('hello')",
])
with temporary_file(root_dir=td) as tf:
with open(tf.name, 'w') as handle:
handle.write(hello_file)

pex_builder = PEXBuilder()
pex_builder.add_source(tf.name, 'test.py')
yield (target, pex_builder)

#
# def test_pex_builder_verify_entry_point_method_should_pass():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('test:hello')
# # Nothing should happen here because `test:hello` is correct
# pex_builder.build(target, verify_entry_point=True)
# assert os.path.exists(target)
#
#
# def test_pex_builder_verify_entry_point_module_should_pass():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('test')
#
# # Nothing should happen here because `test` is correct
# pex_builder.build(target, verify_entry_point=True)
#
# assert os.path.exists(target)
#
#
# def test_pex_builder_verify_entry_point_method_should_fail():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('test:invalid_entry_point')
#
# # Expect InvalidEntryPoint due to invalid entry point method
# with pytest.raises(PEXBuilder.InvalidEntryPoint):
# pex_builder.build(target, verify_entry_point=True)
#
#
# def test_pex_builder_verify_entry_point_module_should_fail():
# with _add_test_hello_to_pex() as (target, pex_builder):
# pex_builder.set_entry_point('invalid.module')
#
# # Expect InvalidEntryPoint due to invalid entry point module
# with pytest.raises(PEXBuilder.InvalidEntryPoint):
# pex_builder.build(target, verify_entry_point=True)