Repository: pypa/twine (python)

Issue:
check: Error message No METADATA in archive when it's there
### Is there an existing issue for this?

- [X] I have searched the existing issues (open and closed), and could not find an existing issue

### What keywords did you use to search existing issues?

No Metadata in archive

### What operating system are you using?

macOS

### If you selected 'Other', describe your Operating System here

_No response_

### What version of Python are you running?

```console
3.12.3
```


### How did you install twine? Did you use your operating system's package manager or pip or something else?

```console
pipx install twine
```


### What version of twine do you have installed (include the complete output)

```console
5.0.0
```


### Which package repository are you using?

pypi.org

### Please describe the issue that you are experiencing

When running twine check on the attached wheel, it reports No METADATA in archive, even though it does have a METADATA file. Probably the issue is that the METADATA file is missing the requisite Metadata-Version field.

[coherent_build-0.0.1.dev0-py3-none-any.whl.zip](https://github.com/pypa/twine/files/15190464/coherent_build-0.0.1.dev0-py3-none-any.whl.zip)

Better would be to report an error message that indicates the true cause of the problem.


### Please list the steps required to reproduce this behaviour

Build an invalid wheel without minimal metadata. Run `twine check` on it.

### Anything else you'd like to mention?

_No response_

Pull request description:
Indicate which files failed to find metadata info
When showing users an error, explain that we couldn't find a file with Metadata-Version in it and list the names of files we searched in the archive.

Closes #1100

Patch:
diff --git a/twine/wheel.py b/twine/wheel.py
index 7af1d80..a2a8ba8 100644
--- a/twine/wheel.py
+++ b/twine/wheel.py
@@ -73,16 +73,21 @@ class Wheel(distribution.Distribution):
                 "Not a known archive format for file: %s" % fqn
             )
 
+        searched_files: List[str] = []
         try:
             for path in self.find_candidate_metadata_files(names):
                 candidate = "/".join(path)
                 data = read_file(candidate)
                 if b"Metadata-Version" in data:
                     return data
+                searched_files.append(candidate)
         finally:
             archive.close()
 
-        raise exceptions.InvalidDistribution("No METADATA in archive: %s" % fqn)
+        raise exceptions.InvalidDistribution(
+            "No METADATA in archive or METADATA missing 'Metadata-Version': "
+            "%s (searched %s)" % (fqn, ",".join(searched_files))
+        )
 
     def parse(self, data: bytes) -> None:
         super().parse(data)


Test patch:
diff --git a/tests/test_wheel.py b/tests/test_wheel.py
index 485c89f..eae3bd9 100644
--- a/tests/test_wheel.py
+++ b/tests/test_wheel.py
@@ -96,6 +96,8 @@ def test_read_wheel_empty_metadata(tmpdir):
 
     with pytest.raises(
         exceptions.InvalidDistribution,
-        match=re.escape(f"No METADATA in archive: {whl_file}"),
+        match=re.escape(
+            f"No METADATA in archive or METADATA missing 'Metadata-Version': {whl_file}"
+        ),
     ):
         wheel.Wheel(whl_file)