def test_stub(self): valid_cases = [ # Empty list [ b""" python_imports = '' PYTHON_BINARY = '/usr/bin/python' """, ([], '/usr/bin/python') ], # Single import [ b""" python_imports = 'myworkspace/spam/eggs' PYTHON_BINARY = '/usr/bin/python' """, (['myworkspace/spam/eggs'], '/usr/bin/python') ], # Multiple imports [ b""" python_imports = 'myworkspace/spam/eggs:otherworkspace' PYTHON_BINARY = '/usr/bin/python' """, (['myworkspace/spam/eggs', 'otherworkspace'], '/usr/bin/python') ], # Relative path to interpreter [ b""" python_imports = '' PYTHON_BINARY = 'mydir/python' """, ([], 'mydir/python') ], # Search for interpreter on $PATH [ b""" python_imports = '' PYTHON_BINARY = 'python' """, ([], '/usr/bin/env python') ], ] for content, expected in valid_cases: with test_utils.temp_file(content) as stub_file: actual = cli.parse_stub(stub_file.name) self.assertEqual(actual, expected) invalid_cases = [ b'', b'\n\n', # No interpreter b" python_imports = 'myworkspace/spam/eggs'", # No imports b"PYTHON_BINARY = 'python'\n", # Interpreter is label b""" python_imports = '' PYTHON_BINARY = '//mypackage:python' """, ] for content in invalid_cases: with test_utils.temp_file(content) as stub_file: with self.assertRaises(error.Error): cli.parse_stub(stub_file.name)
def test_stub(self): valid_cases = [ # Empty list [b""" python_imports = '' PYTHON_BINARY = '/usr/bin/python' """, ([], '/usr/bin/python')], # Single import [b""" python_imports = 'myworkspace/spam/eggs' PYTHON_BINARY = '/usr/bin/python' """, (['myworkspace/spam/eggs'], '/usr/bin/python')], # Multiple imports [b""" python_imports = 'myworkspace/spam/eggs:otherworkspace' PYTHON_BINARY = '/usr/bin/python' """, (['myworkspace/spam/eggs', 'otherworkspace'], '/usr/bin/python')], # Relative path to interpreter [b""" python_imports = '' PYTHON_BINARY = 'mydir/python' """, ([], 'mydir/python')], # Search for interpreter on $PATH [b""" python_imports = '' PYTHON_BINARY = 'python' """, ([], '/usr/bin/env python')], ] for content, expected in valid_cases: with test_utils.temp_file(content) as stub_file: actual = cli.parse_stub(stub_file.name) self.assertEqual(actual, expected) invalid_cases = [ b'', b'\n\n', # No interpreter b" python_imports = 'myworkspace/spam/eggs'", # No imports b"PYTHON_BINARY = 'python'\n", # Interpreter is label b""" python_imports = '' PYTHON_BINARY = '//mypackage:python' """, ] for content in invalid_cases: with test_utils.temp_file(content) as stub_file: with self.assertRaises(error.Error): cli.parse_stub(stub_file.name)
def test_StoredFile(self): expected_content = b'Contents of foo/bar' name = 'foo/bar' f = test_utils.temp_file(expected_content) resource = stored_resource.StoredFile( name, self.date_time_tuple, f.name) self._write_and_check(resource, name, expected_content)
def test_generate_main(self): par = self._construct() boilerplate = 'BOILERPLATE\n' cases = [ # Insert at beginning (b'spam = eggs\n', b'BOILERPLATE\nspam = eggs\n'), # Insert in the middle (b'# a comment\nspam = eggs\n', b'# a comment\nBOILERPLATE\nspam = eggs\n'), # Insert after the end (b'# a comment\n', b'# a comment\nBOILERPLATE\n'), # Blank lines (b'\n \t\n', b'\n \t\nBOILERPLATE\n'), # Future import (b'from __future__ import print_function\n', b'from __future__ import print_function\nBOILERPLATE\n'), # Module docstrings (b"'Single-quote Module docstring'\n", b"'Single-quote Module docstring'\nBOILERPLATE\n"), (b'"Double-quote Module docstring"\n', b'"Double-quote Module docstring"\nBOILERPLATE\n'), (b"'''Triple-single-quote module \"'\n\n docstring'''\n", b"'''Triple-single-quote module \"'\n\n docstring'''\nBOILERPLATE\n"), (b'"""Triple-double-quote module "\'\n\n docstring"""\n', b'"""Triple-double-quote module "\'\n\n docstring"""\nBOILERPLATE\n'), ] for main_content, expected in cases: with test_utils.temp_file(main_content) as main_file: actual = par.generate_main(main_file.name, boilerplate) self.assertEqual(expected, actual.content)
def test_generate_main(self): par = self._construct() boilerplate = 'BOILERPLATE\n' cases = [ # Insert at beginning (b'spam = eggs\n', b'BOILERPLATE\nspam = eggs\n'), # Insert in the middle (b'# a comment\nspam = eggs\n', b'# a comment\nBOILERPLATE\nspam = eggs\n'), # Insert after the end (b'# a comment\n', b'# a comment\nBOILERPLATE\n'), # Blank lines (b'\n \t\n', b'\n \t\nBOILERPLATE\n'), # Future import (b'from __future__ import print_function\n', b'from __future__ import print_function\nBOILERPLATE\n'), # Module docstrings (b"'Single-quote Module docstring'\n", b"'Single-quote Module docstring'\nBOILERPLATE\n"), (b'"Double-quote Module docstring"\n', b'"Double-quote Module docstring"\nBOILERPLATE\n'), (b"'''Triple-single-quote module \"'\n\n docstring'''\n", b"'''Triple-single-quote module \"'\n\n docstring'''\nBOILERPLATE\n" ), (b'"""Triple-double-quote module "\'\n\n docstring"""\n', b'"""Triple-double-quote module "\'\n\n docstring"""\nBOILERPLATE\n' ), ] for main_content, expected in cases: with test_utils.temp_file(main_content) as main_file: actual = par.generate_main(main_file.name, boilerplate) self.assertEqual(expected, actual.content)
def setUp(self): # Setup directory structure and files self.tmpdir = test_utils.mkdtemp() self.input_dir = os.path.join(self.tmpdir, 'input') if not os.path.exists(self.input_dir): os.makedirs(self.input_dir) self.manifest_filename = os.path.join(self.input_dir, 'manifest') self.main_file = test_utils.temp_file(b'print("Hello World!")', suffix='.py') manifest_content = '%s %s\n' % ( os.path.basename(self.main_file.name), self.main_file.name) self.manifest_file = test_utils.temp_file( manifest_content.encode('utf8')) self.output_dir = os.path.join(self.tmpdir, 'output') if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) self.output_filename = os.path.join(self.output_dir, 'output.par') self.interpreter = '/usr/bin/python2' self.import_roots = []
def setUp(self): # Setup directory structure and files self.tmpdir = test_utils.mkdtemp() self.input_dir = os.path.join(self.tmpdir, 'input') if not os.path.exists(self.input_dir): os.makedirs(self.input_dir) self.manifest_filename = os.path.join(self.input_dir, 'manifest') self.main_file = test_utils.temp_file(b'print("Hello World!")', suffix='.py') manifest_content = '%s %s\n' % (os.path.basename( self.main_file.name), self.main_file.name) self.manifest_file = test_utils.temp_file( manifest_content.encode('utf8')) self.output_dir = os.path.join(self.tmpdir, 'output') if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) self.output_filename = os.path.join(self.output_dir, 'output.par') self.interpreter = '/usr/bin/python2' self.import_roots = []
def test_stub(self): valid_cases = [ # Absolute path to interpreter [b""" PYTHON_BINARY = '/usr/bin/python' """, '/usr/bin/python'], # Search for interpreter on $PATH [b""" PYTHON_BINARY = 'python' """, '/usr/bin/env python'], # Default toolchain wrapped python3 interpreter [ b""" PYTHON_BINARY = 'bazel_tools/tools/python/py3wrapper.sh' """, '/usr/bin/env python3' ], # Default toolchain wrapped python2 interpreter [ b""" PYTHON_BINARY = 'bazel_tools/tools/python/py2wrapper.sh' """, '/usr/bin/env python2' ], ] for content, expected in valid_cases: with test_utils.temp_file(content) as stub_file: actual = cli.parse_stub(stub_file.name) self.assertEqual(actual, expected) invalid_cases = [ # No interpreter b'', b'\n\n', # Relative interpreter path b"PYTHON_BINARY = 'mydir/python'", # Interpreter is label b""" PYTHON_BINARY = '//mypackage:python' """, ] for content in invalid_cases: with test_utils.temp_file(content) as stub_file: with self.assertRaises(error.Error): cli.parse_stub(stub_file.name)
def test_scan_manifest_has_collision(self): par = self._construct() # Support file already present in manifest, use manifest version with test_utils.temp_file(b'blah blah\n') as shadowing_support_file: manifest = { 'foo.py': '/something/foo.py', 'subpar/runtime/support.py': shadowing_support_file.name, } resources = par.scan_manifest(manifest) self.assertEqual( resources['subpar/runtime/support.py'].local_filename, shadowing_support_file.name)
def test_scan_manifest_has_collision(self): par = self._construct() # Support file already present in manifest, use manifest version with test_utils.temp_file(b'blah blah\n') as shadowing_support_file: manifest = { 'foo.py': '/something/foo.py', 'subpar/runtime/support.py': shadowing_support_file.name, } resources = par.scan_manifest(manifest) self.assertEqual( resources['subpar/runtime/support.py'].local_filename, shadowing_support_file.name)
def setUp(self): # Setup directory structure and files self.tmpdir = test_utils.mkdtemp() self.input_dir = os.path.join(self.tmpdir, 'input') if not os.path.exists(self.input_dir): os.makedirs(self.input_dir) self.manifest_filename = os.path.join(self.input_dir, 'manifest') self.main_file = test_utils.temp_file(b'print("Hello World!")', suffix='.py') manifest_content = '%s %s\n' % ( os.path.basename(self.main_file.name), self.main_file.name) self.manifest_file = test_utils.temp_file( manifest_content.encode('utf8')) self.output_dir = os.path.join(self.tmpdir, 'output') if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) self.output_filename = os.path.join(self.output_dir, 'output.par') self.interpreter = sys.executable self.import_roots = [] self.date_time_tuple = (1980, 1, 1, 0, 0, 0) self.timestamp = 315532800 self.zip_safe = True
def setUp(self): # Setup directory structure and files self.tmpdir = test_utils.mkdtemp() self.input_dir = os.path.join(self.tmpdir, 'input') if not os.path.exists(self.input_dir): os.makedirs(self.input_dir) self.manifest_filename = os.path.join(self.input_dir, 'manifest') self.main_file = test_utils.temp_file(b'print("Hello World!")', suffix='.py') manifest_content = '%s %s\n' % (os.path.basename( self.main_file.name), self.main_file.name) self.manifest_file = test_utils.temp_file( manifest_content.encode('utf8')) self.output_dir = os.path.join(self.tmpdir, 'output') if not os.path.exists(self.output_dir): os.makedirs(self.output_dir) self.output_filename = os.path.join(self.output_dir, 'output.par') self.interpreter = sys.executable self.import_roots = [] self.date_time_tuple = (1980, 1, 1, 0, 0, 0) self.timestamp = 315532800 self.zip_safe = True
def test_parse_manifest_invalid(self): invalids = [ # Repeated name (b'ccccc/__init__.py \n' + b'ccccc/ddddd/__init__.py \n' + b'ccccc/__init__.py \n'), # Too many spaces b'ccccc/__init__.py foo bar\n', # Not enough spaces b'\n\n', ] for invalid in invalids: with test_utils.temp_file(invalid) as t: with self.assertRaises(error.Error): manifest_parser.parse(t.name)
def test_parse_manifest_invalid(self): invalids = [ # Repeated name (b'ccccc/__init__.py \n' + b'ccccc/ddddd/__init__.py \n' + b'ccccc/__init__.py \n'), # Too many spaces b'ccccc/__init__.py foo bar\n', # Not enough spaces b'\n\n', ] for invalid in invalids: with test_utils.temp_file(invalid) as t: with self.assertRaises(error.Error): manifest_parser.parse(t.name)
def test_parse_manifest_valid(self): valid = ( # 1 field, no trailing space b'ccccc/__init__.py\n' + # 1 field, trailing space b'ccccc/ddddd/__init__.py \n' + # 2 fields b'ccccc/ddddd/eeeee /code/rrrrr/ccccc/ddddd/eeeee\n') expected = { 'ccccc/__init__.py': None, 'ccccc/ddddd/__init__.py': None, 'ccccc/ddddd/eeeee': '/code/rrrrr/ccccc/ddddd/eeeee', } with test_utils.temp_file(valid) as t: manifest = manifest_parser.parse(t.name) self.assertEqual(manifest, expected)
def test_parse_manifest_valid(self): valid = ( # 1 field, no trailing space b'ccccc/__init__.py\n' + # 1 field, trailing space b'ccccc/ddddd/__init__.py \n' + # 2 fields b'ccccc/ddddd/eeeee /code/rrrrr/ccccc/ddddd/eeeee\n' ) expected = { 'ccccc/__init__.py': None, 'ccccc/ddddd/__init__.py': None, 'ccccc/ddddd/eeeee': '/code/rrrrr/ccccc/ddddd/eeeee', } with test_utils.temp_file(valid) as t: manifest = manifest_parser.parse(t.name) self.assertEqual(manifest, expected)
def test_generate_main(self): par = self._construct() boilerplate = 'BOILERPLATE\n' cases = [ # Insert at beginning (b'spam = eggs\n', b'BOILERPLATE\nspam = eggs\n'), # Insert in the middle (b'# a comment\nspam = eggs\n', b'# a comment\nBOILERPLATE\nspam = eggs\n'), # Insert after the end (b'# a comment\n', b'# a comment\nBOILERPLATE\n'), # Blank lines (b'\n \t\n', b'\n \t\nBOILERPLATE\n'), # Future import (b'from __future__ import print_function\n', b'from __future__ import print_function\nBOILERPLATE\n'), ] for main_content, expected in cases: with test_utils.temp_file(main_content) as main_file: actual = par.generate_main(main_file.name, boilerplate) self.assertEqual(expected, actual.content)
def test_generate_main(self): par = self._construct() boilerplate = 'BOILERPLATE\n' cases = [ # Insert at beginning (b'spam = eggs\n', b'BOILERPLATE\nspam = eggs\n'), # Insert in the middle (b'# a comment\nspam = eggs\n', b'# a comment\nBOILERPLATE\nspam = eggs\n'), # Insert after the end (b'# a comment\n', b'# a comment\nBOILERPLATE\n'), # Blank lines (b'\n \t\n', b'\n \t\nBOILERPLATE\n'), # Future import (b'from __future__ import print_function\n', b'from __future__ import print_function\nBOILERPLATE\n'), ] for main_content, expected in cases: with test_utils.temp_file(main_content) as main_file: actual = par.generate_main(main_file.name, boilerplate) self.assertEqual(expected, actual.content)
def test_create_source_file_not_found(self): with test_utils.temp_file( b'foo.py doesnotexist.py\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises((IOError, OSError)): par.create()
def test_create_manifest_contains___main___py(self): with test_utils.temp_file(b'__main__.py\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises(error.Error): par.create()
def test_create_manifest_parse_error(self): with test_utils.temp_file(b'blah blah blah\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises(error.Error): par.create()
def test_StoredFile(self): expected_content = b'Contents of foo/bar' name = 'foo/bar' f = test_utils.temp_file(expected_content) resource = stored_resource.StoredFile(name, f.name) self._write_and_check(resource, name, expected_content)
def test_create_manifest_parse_error(self): with test_utils.temp_file(b'blah blah blah\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises(error.Error): par.create()
def test_create_source_file_not_found(self): with test_utils.temp_file(b'foo.py doesnotexist.py\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises(OSError): par.create()
def test_create_manifest_contains___main___py(self): with test_utils.temp_file(b'__main__.py\n') as manifest_file: par = self._construct(manifest_filename=manifest_file.name) with self.assertRaises(error.Error): par.create()