def test_valid(self): """ Tests to check that missing and invalid metadata is caught. """ md = Metadata() self.assertRaises(MetadataMissingError, md.validate) try: md.name = 'Foo Bar' except MetadataInvalidError: pass md.name = 'foo_bar' # Name now OK, but version and summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.version = '1.0a' except MetadataInvalidError: pass md.version = '1.0' # Name and version now OK, but summary to be checked self.assertRaises(MetadataMissingError, md.validate) try: md.summary = '' except MetadataInvalidError: pass try: md.summary = ' ' * 2048 except MetadataInvalidError: pass md.summary = ' ' * 2047 md.validate() md.summary = ' ' md.validate()
def make_metadata(project_dir): md = Metadata() md.name = input('name >>> ') md.version = input('version >>> ') md.summary = input('summary >>> ') dist_path = os.path.join(project_dir, 'dist-info') if not os.path.exists(dist_path): os.makedirs(dist_path) md.write(path=os.path.join(dist_path, 'pydist.json')) return md
def test_init(self): "Test initialisation" md = Metadata() self.assertIsNone(md._legacy) self.assertRaises(MetadataMissingError, md.validate) md.name = 'dummy' self.assertRaises(MetadataMissingError, md.validate) md.version = '0.1' self.assertRaises(MetadataMissingError, md.validate) md.summary = 'Summary' md.validate() self.assertEqual(md.name, 'dummy') self.assertEqual(md.version, '0.1') # Initialise from mapping md = Metadata( mapping={ 'metadata_version': '2.0', 'name': 'foo', 'version': '0.3.4', 'summary': 'Summary', }) md.validate() self.assertEqual(md.name, 'foo') self.assertEqual(md.version, '0.3.4') self.assertEqual(md.run_requires, []) self.assertEqual(md.meta_requires, []) self.assertEqual(md.provides, ['foo (0.3.4)']) # Initialise from legacy metadata fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', LEGACY_METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNotNone(md._legacy) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut'])) self.assertEqual(md.metadata_version, '1.2') self.assertEqual(md.version, '2.0.0.9') self.assertEqual(md.meta_requires, []) self.assertEqual(set(md.provides), set(['choxie (2.0.0.9)', 'truffles (1.0)'])) # Initialise from new metadata fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNone(md._legacy) self.assertEqual(md.metadata_version, '2.0') self.assertEqual(md.name, 'foobar') self.assertEqual(md.version, '0.1') self.assertEqual(md.provides, ['foobar (0.1)'])
def test_add_requirements(self): md = Metadata() md.name = 'bar' md.version = '0.5' md.add_requirements(['foo (0.1.2)']) self.assertEqual(md.run_requires, [{'requires': ['foo (0.1.2)']}]) fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', LEGACY_METADATA_FILENAME) md = Metadata(path=fn) md.add_requirements(['foo (0.1.2)']) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut', 'foo (0.1.2)']))
def test_add_requirements(self): md = Metadata() md.name = 'bar' md.version = '0.5' md.add_requirements(['foo (0.1.2)']) self.assertEqual(md.run_requires, [{ 'requires': ['foo (0.1.2)']}]) fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', 'METADATA') md = Metadata(path=fn) md.add_requirements(['foo (0.1.2)']) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut', 'foo (0.1.2)']))
def test_register(self): "Test registration" self.check_pypi_server_available() self.check_testdist_available() d = os.path.join(HERE, self.testdir) data = self.load_package_metadata(d) md = Metadata() self.assertRaises(MetadataMissingError, self.index.register, md) md.name = self.dist_project self.assertRaises(MetadataMissingError, self.index.register, md) md.version = data['version'] md.summary = data['summary'] response = self.index.register(md) self.assertEqual(response.code, 200)
def test_init(self): "Test initialisation" md = Metadata() self.assertIsNone(md._legacy) self.assertRaises(MetadataMissingError, md.validate) md.name = 'dummy' self.assertRaises(MetadataMissingError, md.validate) md.version = '0.1' self.assertRaises(MetadataMissingError, md.validate) md.summary = 'Summary' md.validate() self.assertEqual(md.name, 'dummy') self.assertEqual(md.version, '0.1') # Initialise from mapping md = Metadata(mapping={ 'metadata_version': '2.0', 'name': 'foo', 'version': '0.3.4', 'summary': 'Summary', }) md.validate() self.assertEqual(md.name, 'foo') self.assertEqual(md.version, '0.3.4') self.assertEqual(md.run_requires, []) self.assertEqual(md.meta_requires, []) self.assertEqual(md.provides, ['foo (0.3.4)']) # Initialise from legacy metadata fn = os.path.join(HERE, 'fake_dists', 'choxie-2.0.0.9.dist-info', 'METADATA') md = Metadata(path=fn) md.validate() self.assertIsNotNone(md._legacy) self.assertEqual(set(md.run_requires), set(['towel-stuff (0.1)', 'nut'])) self.assertEqual(md.metadata_version, '1.2') self.assertEqual(md.version, '2.0.0.9') self.assertEqual(md.meta_requires, []) self.assertEqual(set(md.provides), set(['choxie (2.0.0.9)', 'truffles (1.0)'])) # Initialise from new metadata fn = os.path.join(HERE, METADATA_FILENAME) md = Metadata(path=fn) md.validate() self.assertIsNone(md._legacy) self.assertEqual(md.metadata_version, '2.0') self.assertEqual(md.name, 'foobar') self.assertEqual(md.version, '0.1') self.assertEqual(md.provides, ['foobar (0.1)'])
def test_get_file(self): # Create a fake dist temp_site_packages = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_site_packages) dist_name = 'test' dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info') os.mkdir(dist_info) metadata_path = os.path.join(dist_info, 'pydist.json') resources_path = os.path.join(dist_info, 'RESOURCES') md = Metadata() md.name = 'test' md.version = '0.1' md.summary = 'test' md.write(path=metadata_path) test_path = 'test.cfg' fd, test_resource_path = tempfile.mkstemp() os.close(fd) self.addCleanup(os.remove, test_resource_path) fp = open(test_resource_path, 'w') try: fp.write('Config') finally: fp.close() fp = open(resources_path, 'w') try: fp.write('%s,%s' % (test_path, test_resource_path)) finally: fp.close() # Add fake site-packages to sys.path to retrieve fake dist self.addCleanup(sys.path.remove, temp_site_packages) sys.path.insert(0, temp_site_packages) # Try to retrieve resources paths and files d = DistributionPath() self.assertEqual(d.get_file_path(dist_name, test_path), test_resource_path) self.assertRaises(KeyError, d.get_file_path, dist_name, 'i-dont-exist')
def test_get_file(self): # Create a fake dist temp_site_packages = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, temp_site_packages) dist_name = "test" dist_info = os.path.join(temp_site_packages, "test-0.1.dist-info") os.mkdir(dist_info) metadata_path = os.path.join(dist_info, "pydist.json") resources_path = os.path.join(dist_info, "RESOURCES") md = Metadata() md.name = "test" md.version = "0.1" md.summary = "test" md.write(path=metadata_path) test_path = "test.cfg" fd, test_resource_path = tempfile.mkstemp() os.close(fd) self.addCleanup(os.remove, test_resource_path) fp = open(test_resource_path, "w") try: fp.write("Config") finally: fp.close() fp = open(resources_path, "w") try: fp.write("%s,%s" % (test_path, test_resource_path)) finally: fp.close() # Add fake site-packages to sys.path to retrieve fake dist self.addCleanup(sys.path.remove, temp_site_packages) sys.path.insert(0, temp_site_packages) # Try to retrieve resources paths and files d = DistributionPath() self.assertEqual(d.get_file_path(dist_name, test_path), test_resource_path) self.assertRaises(KeyError, d.get_file_path, dist_name, "i-dont-exist")
def wheel_modifier_ver(self, path_map): mdpath = path_map['dummy-0.1.dist-info/%s' % LEGACY_METADATA_FILENAME] md = Metadata(path=mdpath) md.version = '0.1+123' md.write(path=mdpath, legacy=True) return True
def wheel_modifier_ver(self, path_map): mdpath = path_map["dummy-0.1.dist-info/pydist.json"] md = Metadata(path=mdpath) md.version = "0.1+123" md.write(path=mdpath) return True
def wheel_modifier_ver(self, path_map): mdpath = path_map['dummy-0.1.dist-info/pydist.json'] md = Metadata(path=mdpath) md.version = '0.1+123' md.write(path=mdpath) return True