git_command: refactor User-Agent settings

Convert the RepoUserAgent function into a UserAgent class.  This
makes it cleaner to hold internal state, and will make it easier
to add a separate git User-Agent, although we don't do it here.

We make the RepoSourceVersion independent of GitCommand so that
it can be called by the class (later).

Bug: https://crbug.com/gerrit/11144
Change-Id: Iab4e1f974b8733a36b243b2d03f5085a96effa19
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/239232
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger
2019-09-30 22:39:49 -04:00
parent 369814b4a7
commit 71b0f312b1
3 changed files with 90 additions and 52 deletions

View File

@ -50,12 +50,20 @@ class GitCallUnitTest(unittest.TestCase):
self.assertNotEqual('', ver.full)
class RepoUserAgentUnitTest(unittest.TestCase):
"""Tests the RepoUserAgent function."""
class UserAgentUnitTest(unittest.TestCase):
"""Tests the UserAgent function."""
def test_smoke(self):
"""Make sure it returns something useful."""
ua = git_command.RepoUserAgent()
def test_smoke_os(self):
"""Make sure UA OS setting returns something useful."""
os_name = git_command.user_agent.os
# We can't dive too deep because of OS/tool differences, but we can check
# the general form.
m = re.match(r'^[^ ]+$', os_name)
self.assertIsNotNone(m)
def test_smoke_repo(self):
"""Make sure repo UA returns something useful."""
ua = git_command.user_agent.repo
# We can't dive too deep because of OS/tool differences, but we can check
# the general form.
m = re.match(r'^git-repo/[^ ]+ ([^ ]+) git/[^ ]+ Python/[0-9.]+', ua)