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

#164 Transform common project URLs to repository #169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

TD-Base
Copy link
Collaborator

@TD-Base TD-Base commented Jul 13, 2024

I added the previous issue i contributed as well

@coveralls
Copy link

Coverage Status

coverage: 95.979% (+0.03%) from 95.951%
when pulling dbdc0b3 on 164-transform-common-project-urls-to-repository
into 6697e79 on main.

@@ -27,7 +27,11 @@
from pygount.common import mapped_repr
from pygount.git_storage import GitStorage, git_remote_url_and_revision_if_any

GIT_REPO_REGEX = re.compile(r"^(https?://|git@)")
HAS_URL_PREFIX = re.compile(r"^(https?://)")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: It's not a boolean rather a regex, so: URL_REGEX.

We might also want to be a bit more precise because a log of things can be an URL, so possibly even better: HTTP_URL_REGEX

# TODO#113: Find a way to exclude the ugly temp folder from the source path.
result.extend(self._paths_and_group_to_analyze(git_storage.temp_folder))
else:
has_url_prefix = re.match(HAS_URL_PREFIX, source_pattern)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor inconsistency: The result of result.match() is a Matchobject, not a boolean. To be more precise:

has_url_prefix = re.match(HAS_URL_PREFIX, source_pattern) is not None

HAS_URL_PREFIX = re.compile(r"^(https?://)")
_ALLOWED_GIT_PLATFORMS = ["github.com", "bitbucket.org", "gitlab.com"]
GIT_REPO_REGEX = re.compile(
r"^(https?://|git@)({})/[\w-]+/[\w-]+".format("|".join(map(re.escape, _ALLOWED_GIT_PLATFORMS)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of \w- is enough, there might be all kinds of cases where this could fail for legitimate URLs, e.g. when using URL escaping based on %. Maybe we should go with something more lenient, that would accept a few non-URLs. In that case, it will fail later anyway when trying to interact with the URL, which should cause some system function to fail and report a somewhat descriptive error message. Which would be good enough from my point of view.

Also, we should be able to avoid the format() using f-strings. How about this?

_ALLOWED_GIT_PLATFORM_CHOICES_PATTERN = "|".join(map(re.escape, _ALLOWED_GIT_PLATFORMS)
GIT_REPO_REGEX = re.compile(
    fr"^(https?://|git@)({_ALLOWED_GIT_PLATFORM_CHOICES_PATTERN})/[^/]+/[^/]+"
)

def test_fails_on_non_git_urls(self):
non_repo_urls = [["https://no/git/url"], ["https://google.com/nogit"]]
for non_repo_url in non_repo_urls:
with analysis.SourceScanner(non_repo_url) as scanner, pytest.raises(pygount.Error):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pytest.raises(pygout.Error, match="...") to also check the error message. You can use a regex here.

If the expected error message is long, I typically only check the first part of the sentence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants