def _assert_dist_and_wheel_identity(self, expected_name, expected_version, expected_platform, dist_target, **kwargs): context, synthetic_target, fingerprint_suffix = self._create_distribution_synthetic_target( dist_target, **kwargs) resulting_dist_req = assert_single_element( synthetic_target.requirements.value) expected_snapshot_version = '{}+{}'.format(expected_version, fingerprint_suffix) self.assertEquals( '{}=={}'.format(expected_name, expected_snapshot_version), str(resulting_dist_req.requirement)) local_wheel_products = context.products.get('local_wheels') local_wheel = self.retrieve_single_product_at_target_base( local_wheel_products, dist_target) dist, version, platform = name_and_platform(local_wheel) self.assertEquals(dist, expected_name) self.assertEquals(version, expected_snapshot_version) expected_platform = expected_platform.match({ BuildLocalPythonDistributionsTestBase.ExpectedPlatformType.any: "any", BuildLocalPythonDistributionsTestBase.ExpectedPlatformType.current: normalized_current_platform(), }) self.assertEquals(platform, expected_platform)
def _assert_dist_and_wheel_identity(self, expected_name, expected_version, expected_platform, dist_target, **kwargs): context, synthetic_target, fingerprint_suffix = self._create_distribution_synthetic_target( dist_target, **kwargs) resulting_dist_req = assert_single_element( synthetic_target.requirements.value) expected_snapshot_version = '{}+{}'.format(expected_version, fingerprint_suffix) self.assertEquals( '{}=={}'.format(expected_name, expected_snapshot_version), str(resulting_dist_req.requirement)) local_wheel_products = context.products.get('local_wheels') local_wheel = self.retrieve_single_product_at_target_base( local_wheel_products, dist_target) dist, version, platform = name_and_platform(local_wheel) self.assertEquals(dist, expected_name) self.assertEquals(version, expected_snapshot_version) expected_platform = expected_platform.resolve_for_enum_variant({ 'any': 'any', 'current': normalized_current_platform(), }) self.assertEquals(platform, expected_platform)
def test_python_create_universal_distribution(self): universal_dist = self.target_dict['universal'] context, synthetic_target = self._create_distribution_synthetic_target(universal_dist) self.assertEquals(['universal_dist==0.0.0'], [str(x.requirement) for x in synthetic_target.requirements.value]) local_wheel_products = context.products.get('local_wheels') local_wheel = self._retrieve_single_product_at_target_base(local_wheel_products, universal_dist) _, _, wheel_platform = name_and_platform(local_wheel) self.assertEqual('any', wheel_platform)
def test_python_create_universal_distribution(self): universal_dist = self.target_dict['universal'] context, synthetic_target, snapshot_version = self._create_distribution_synthetic_target( universal_dist) self.assertEquals(['universal_dist==0.0.0+{}'.format(snapshot_version)], [str(x.requirement) for x in synthetic_target.requirements.value]) local_wheel_products = context.products.get('local_wheels') local_wheel = self._retrieve_single_product_at_target_base(local_wheel_products, universal_dist) _, _, wheel_platform = name_and_platform(local_wheel) self.assertEqual('any', wheel_platform)
def _assert_ctypes_binary_creation(self, toolchain_variant): with temporary_dir() as tmp_dir: pants_run = self.run_pants(command=['binary', self._binary_target], config={ GLOBAL_SCOPE_CONFIG_SECTION: { 'pants_distdir': tmp_dir, }, 'native-build-step': { 'toolchain_variant': toolchain_variant, }, }) self.assert_success(pants_run) # Check that we have selected the appropriate compilers for our selected toolchain variant, # for both C and C++ compilation. # TODO(#6866): don't parse info logs for testing! for compiler_name in self._compiler_names_for_variant[toolchain_variant]: self.assertIn("selected compiler exe name: '{}'".format(compiler_name), pants_run.stdout_data) for linker_name in self._linker_names_for_variant[toolchain_variant]: self.assertIn("selected linker exe name: '{}'".format(linker_name), pants_run.stdout_data) # Check for the pex and for the wheel produced for our python_dist(). pex = os.path.join(tmp_dir, 'bin.pex') self.assertTrue(is_executable(pex)) # The + is because we append the target's fingerprint to the version. We test this version # string in test_build_local_python_distributions.py. wheel_glob = os.path.join(tmp_dir, 'ctypes_test-0.0.1+*.whl') wheel_dist_with_path = assert_single_element(glob.glob(wheel_glob)) wheel_dist = re.sub('^{}{}'.format(re.escape(tmp_dir), os.path.sep), '', wheel_dist_with_path) dist_name, dist_version, wheel_platform = name_and_platform(wheel_dist) self.assertEqual(dist_name, 'ctypes_test') contains_current_platform = Platform.create().resolve_platform_specific({ 'darwin': lambda: wheel_platform.startswith('macosx'), 'linux': lambda: wheel_platform.startswith('linux'), }) self.assertTrue(contains_current_platform) # Verify that the wheel contains our shared libraries. wheel_files = ZipFile(wheel_dist_with_path).namelist() dist_versioned_name = '{}-{}.data'.format(dist_name, dist_version) for shared_lib_filename in ['libasdf-c_ctypes.so', 'libasdf-cpp_ctypes.so']: full_path_in_wheel = os.path.join(dist_versioned_name, 'data', shared_lib_filename) self.assertIn(full_path_in_wheel, wheel_files) # Execute the binary and ensure its output is correct. binary_run_output = invoke_pex_for_output(pex) self.assertEqual(b'x=3, f(x)=17\n', binary_run_output)
def _assert_dist_and_wheel_identity(self, expected_name, expected_version, expected_platform, dist_target, **kwargs): context, synthetic_target, fingerprint_suffix = self._create_distribution_synthetic_target( dist_target, **kwargs) resulting_dist_req = assert_single_element(synthetic_target.requirements.value) expected_snapshot_version = '{}+{}'.format(expected_version, fingerprint_suffix) self.assertEquals( '{}=={}'.format(expected_name, expected_snapshot_version), str(resulting_dist_req.requirement)) local_wheel_products = context.products.get('local_wheels') local_wheel = self.retrieve_single_product_at_target_base(local_wheel_products, dist_target) dist, version, platform = name_and_platform(local_wheel) self.assertEquals(dist, expected_name) self.assertEquals(version, expected_snapshot_version) self.assertEquals(platform, expected_platform)
def test_ctypes_binary(self): with temporary_dir() as tmp_dir: pants_run = self.run_pants(command=['binary', self._binary_target], config={ GLOBAL_SCOPE_CONFIG_SECTION: { 'pants_distdir': tmp_dir, } }) self.assert_success(pants_run) # Check for the pex and for the wheel produced for our python_dist(). pex = os.path.join(tmp_dir, 'bin.pex') self.assertTrue(is_executable(pex)) # The + is because we append the target's fingerprint to the version. We test this version # string in test_build_local_python_distributions.py. wheel_glob = os.path.join(tmp_dir, 'ctypes_test-0.0.1+*.whl') wheel_dist_with_path = assert_single_element(glob.glob(wheel_glob)) wheel_dist = re.sub( '^{}{}'.format(re.escape(tmp_dir), os.path.sep), '', wheel_dist_with_path) dist_name, dist_version, wheel_platform = name_and_platform( wheel_dist) self.assertEqual(dist_name, 'ctypes_test') contains_current_platform = Platform.create( ).resolve_platform_specific({ 'darwin': lambda: wheel_platform.startswith('macosx'), 'linux': lambda: wheel_platform.startswith('linux'), }) self.assertTrue(contains_current_platform) # Verify that the wheel contains our shared libraries. wheel_files = ZipFile(wheel_dist_with_path).namelist() dist_versioned_name = '{}-{}.data'.format(dist_name, dist_version) for shared_lib_filename in ['libasdf-c.so', 'libasdf-cpp.so']: full_path_in_wheel = os.path.join(dist_versioned_name, 'data', shared_lib_filename) self.assertIn(full_path_in_wheel, wheel_files) # Execute the binary and ensure its output is correct. binary_run_output = invoke_pex_for_output(pex) self.assertEqual('x=3, f(x)=17\n', binary_run_output)
def test_binary(self): with temporary_dir() as tmp_dir: pants_run = self.run_pants(command=['binary', self._binary_target], config={ GLOBAL_SCOPE_CONFIG_SECTION: { 'pants_distdir': tmp_dir, } }) self.assert_success(pants_run) # Check for the pex and for the wheel produced for our python_dist(). pex = os.path.join(tmp_dir, 'bin.pex') self.assertTrue(is_executable(pex)) wheel_glob = os.path.join(tmp_dir, 'ctypes_test-0.0.1-*.whl') globbed_wheel = glob.glob(wheel_glob) self.assertEqual(len(globbed_wheel), 1) wheel_dist = globbed_wheel[0] _, _, wheel_platform = name_and_platform(wheel_dist) contains_current_platform = Platform.create().resolve_platform_specific({ 'darwin': lambda: wheel_platform.startswith('macosx'), 'linux': lambda: wheel_platform.startswith('linux'), }) self.assertTrue(contains_current_platform) # Verify that the wheel contains our shared libraries. wheel_files = ZipFile(wheel_dist).namelist() for shared_lib_filename in ['libasdf-c.so', 'libasdf-cpp.so']: full_path_in_wheel = os.path.join('ctypes_test-0.0.1.data', 'data', shared_lib_filename) self.assertIn(full_path_in_wheel, wheel_files) # Execute the binary and ensure its output is correct. binary_run_output = invoke_pex_for_output(pex) self.assertEqual('x=3, f(x)=17\n', binary_run_output)
def test_ctypes_binary_creation(self, toolchain_variant): """Create a python_binary() with all native toolchain variants, and test the result.""" with temporary_dir() as tmp_dir: pants_run = self.run_pants(command=['binary', self._binary_target], config={ GLOBAL_SCOPE_CONFIG_SECTION: { 'pants_distdir': tmp_dir, }, 'native-build-step': { 'toolchain_variant': toolchain_variant.value, }, }) self.assert_success(pants_run) # Check that we have selected the appropriate compilers for our selected toolchain variant, # for both C and C++ compilation. # TODO(#6866): don't parse info logs for testing! There is a TODO in test_cpp_compile.py # in the native backend testing to traverse the PATH to find the selected compiler. compiler_names_to_check = toolchain_variant.resolve_for_enum_variant({ 'gnu': ['gcc', 'g++'], 'llvm': ['clang', 'clang++'], }) for compiler_name in compiler_names_to_check: self.assertIn("selected compiler exe name: '{}'".format(compiler_name), pants_run.stdout_data) # All of our toolchains currently use the C++ compiler's filename as argv[0] for the linker, # so there is only one name to check. linker_names_to_check = toolchain_variant.resolve_for_enum_variant({ 'gnu': ['g++'], 'llvm': ['clang++'], }) for linker_name in linker_names_to_check: self.assertIn("selected linker exe name: '{}'".format(linker_name), pants_run.stdout_data) # Check for the pex and for the wheel produced for our python_dist(). pex = os.path.join(tmp_dir, 'bin.pex') self.assertTrue(is_executable(pex)) # The + is because we append the target's fingerprint to the version. We test this version # string in test_build_local_python_distributions.py. wheel_glob = os.path.join(tmp_dir, 'ctypes_test-0.0.1+*.whl') wheel_dist_with_path = assert_single_element(glob.glob(wheel_glob)) wheel_dist = re.sub('^{}{}'.format(re.escape(tmp_dir), os.path.sep), '', wheel_dist_with_path) dist_name, dist_version, wheel_platform = name_and_platform(wheel_dist) self.assertEqual(dist_name, 'ctypes_test') contains_current_platform = Platform.current.resolve_for_enum_variant({ 'darwin': wheel_platform.startswith('macosx'), 'linux': wheel_platform.startswith('linux'), }) self.assertTrue(contains_current_platform) # Verify that the wheel contains our shared libraries. wheel_files = ZipFile(wheel_dist_with_path).namelist() dist_versioned_name = '{}-{}.data'.format(dist_name, dist_version) for shared_lib_filename in ['libasdf-c_ctypes.so', 'libasdf-cpp_ctypes.so']: full_path_in_wheel = os.path.join(dist_versioned_name, 'data', shared_lib_filename) self.assertIn(full_path_in_wheel, wheel_files) # Execute the binary and ensure its output is correct. binary_run_output = invoke_pex_for_output(pex) self.assertEqual(b'x=3, f(x)=17\n', binary_run_output)
def test_ctypes_binary_creation(self, toolchain_variant): """Create a python_binary() with all native toolchain variants, and test the result.""" with temporary_dir() as tmp_dir: pants_run = self.run_pants(command=['binary', self._binary_target], config={ GLOBAL_SCOPE_CONFIG_SECTION: { 'pants_distdir': tmp_dir, }, 'native-build-step': { 'toolchain_variant': toolchain_variant.value, }, }) self.assert_success(pants_run) # Check that we have selected the appropriate compilers for our selected toolchain variant, # for both C and C++ compilation. # TODO(#6866): don't parse info logs for testing! There is a TODO in test_cpp_compile.py # in the native backend testing to traverse the PATH to find the selected compiler. compiler_names_to_check = toolchain_variant.match({ ToolchainVariant.gnu: ['gcc', 'g++'], ToolchainVariant.llvm: ['clang', 'clang++'], }) for compiler_name in compiler_names_to_check: self.assertIn( "selected compiler exe name: '{}'".format(compiler_name), pants_run.stdout_data) # All of our toolchains currently use the C++ compiler's filename as argv[0] for the linker, # so there is only one name to check. linker_names_to_check = toolchain_variant.match({ ToolchainVariant.gnu: ['g++'], ToolchainVariant.llvm: ['clang++'], }) for linker_name in linker_names_to_check: self.assertIn( "selected linker exe name: '{}'".format(linker_name), pants_run.stdout_data) # Check for the pex and for the wheel produced for our python_dist(). pex = os.path.join(tmp_dir, 'bin.pex') self.assertTrue(is_executable(pex)) # The + is because we append the target's fingerprint to the version. We test this version # string in test_build_local_python_distributions.py. wheel_glob = os.path.join(tmp_dir, 'ctypes_test-0.0.1+*.whl') wheel_dist_with_path = assert_single_element(glob.glob(wheel_glob)) wheel_dist = re.sub( '^{}{}'.format(re.escape(tmp_dir), os.path.sep), '', wheel_dist_with_path) dist_name, dist_version, wheel_platform = name_and_platform( wheel_dist) self.assertEqual(dist_name, 'ctypes_test') contains_current_platform = Platform.current.match({ Platform.darwin: wheel_platform.startswith('macosx'), Platform.linux: wheel_platform.startswith('linux'), }) self.assertTrue(contains_current_platform) # Verify that the wheel contains our shared libraries. wheel_files = ZipFile(wheel_dist_with_path).namelist() dist_versioned_name = '{}-{}.data'.format(dist_name, dist_version) for shared_lib_filename in [ 'libasdf-c_ctypes.so', 'libasdf-cpp_ctypes.so' ]: full_path_in_wheel = os.path.join(dist_versioned_name, 'data', shared_lib_filename) self.assertIn(full_path_in_wheel, wheel_files) # Execute the binary and ensure its output is correct. binary_run_output = invoke_pex_for_output(pex) self.assertEqual(b'x=3, f(x)=17\n', binary_run_output)