Addon Manager: Refactor to test git and eliminate GitPython

This commit is contained in:
Chris Hennes
2022-08-18 09:49:37 -05:00
parent b6da08ef8b
commit ee162ff3a8
6 changed files with 295 additions and 143 deletions

View File

@@ -26,6 +26,7 @@ import os
import shutil
import stat
import tempfile
import time
from zipfile import ZipFile
import FreeCAD
@@ -40,6 +41,7 @@ class TestGit(unittest.TestCase):
def setUp(self):
"""Set up the test case: called by the unit test system"""
self.cwd = os.getcwd()
test_data_dir = os.path.join(
FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data"
)
@@ -49,7 +51,9 @@ class TestGit(unittest.TestCase):
)
os.makedirs(self.test_dir, exist_ok=True)
self.test_repo_remote = os.path.join(self.test_dir, "TEST_REPO_REMOTE")
self._rmdir(self.test_repo_remote)
if os.path.exists(self.test_repo_remote):
# Make sure any old copy that got left around is deleted
self._rmdir(self.test_repo_remote)
if not os.path.exists(git_repo_zip):
self.skipTest("Can't find test repo")
@@ -66,13 +70,16 @@ class TestGit(unittest.TestCase):
def tearDown(self):
"""Clean up after the test"""
# self._rmdir(self.test_dir)
os.chdir(self.cwd)
#self._rmdir(self.test_dir)
os.rename(self.test_dir, self.test_dir + ".old." + str(time.time()))
def test_clone(self):
"""Test git clone"""
checkout_dir = self._clone_test_repo()
self.assertTrue(os.path.exists(checkout_dir))
self.assertTrue(os.path.exists(os.path.join(checkout_dir, ".git")))
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def test_checkout(self):
"""Test git checkout"""
@@ -82,6 +89,7 @@ class TestGit(unittest.TestCase):
status = self.git.status(checkout_dir).strip()
expected_status = "## HEAD (no branch)"
self.assertEqual(status, expected_status)
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def test_update(self):
"""Test using git to update the local repo"""
@@ -91,6 +99,7 @@ class TestGit(unittest.TestCase):
self.assertTrue(self.git.update_available(checkout_dir))
self.git.update(checkout_dir)
self.assertFalse(self.git.update_available(checkout_dir))
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def test_tag_and_branch(self):
"""Test checking the currently checked-out tag"""
@@ -108,12 +117,40 @@ class TestGit(unittest.TestCase):
self.assertEqual(found_branch, expected_branch)
self.assertFalse(self.git.update_available(checkout_dir))
expected_branch = "master"
expected_branch = "main"
self.git.checkout(checkout_dir, expected_branch)
found_branch = self.git.current_branch(checkout_dir)
self.assertEqual(found_branch, expected_branch)
self.assertFalse(self.git.update_available(checkout_dir))
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def test_get_remote(self):
""" Test getting the remote location """
checkout_dir = self._clone_test_repo()
expected_remote = self.test_repo_remote
returned_remote = self.git.get_remote(checkout_dir)
self.assertEqual(expected_remote, returned_remote)
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def test_repair(self):
""" Test the repair feature (and some exception throwing) """
checkout_dir = self._clone_test_repo()
remote = self.git.get_remote(checkout_dir)
git_dir = os.path.join(checkout_dir,".git")
self.assertTrue(os.path.exists(git_dir))
self._rmdir(git_dir)
# Make sure that we've truly broken the install
with self.assertRaises(GitFailed):
self.git.status(checkout_dir)
self.git.repair(remote, checkout_dir)
status = self.git.status(checkout_dir)
self.assertEqual(status,"## main...origin/main\n")
self.assertEqual(os.getcwd(),self.cwd, "We should be left in the same CWD we started")
def _rmdir(self, path):
try:
shutil.rmtree(path, onerror=self._remove_readonly)

View File

@@ -26,6 +26,13 @@ import json
import unittest
import os
import tempfile
have_git = True
try:
import git
except ImportError:
have_git = False
import FreeCAD
from PySide2 import QtCore
@@ -36,6 +43,11 @@ from addonmanager_workers_startup import (
CreateAddonListWorker,
LoadPackagesFromCacheWorker,
LoadMacrosFromCacheWorker,
CheckSingleUpdateWorker,
)
from addonmanager_workers_installation import (
InstallWorkbenchWorker,
)
class TestWorkersStartup(unittest.TestCase):
@@ -159,8 +171,49 @@ class TestWorkersStartup(unittest.TestCase):
def test_update_checker(self):
""" Test the code that checks a single addon for available updates. """
# First, install a specific Addon of each kind
if not have_git:
return
# Populate the test's Addon List:
self.test_create_addon_list_worker()
# First, install a specific Addon of each kind into a temp location
location = os.path.join(tempfile.gettempdir(),"FreeCADTesting")
self._test_workbench_update_checker(location)
# Preference Pack
# Macro
# Arrange for those addons to be out-of-date
# Check for updates
# Check for updates
def _test_workbench_update_checker(self, location):
# Workbench: use the FreeCAD-Help workbench for testing purposes
help_addon = None
for addon in self.addon_list:
if addon.name == "Help":
help_addon = addon
break
if not help_addon:
print("Unable to locate the FreeCAD-Help addon to test with")
return
addon_location = os.path.join(location, help_addon.name)
worker = InstallWorkbenchWorker(addon, addon_location)
worker.run() # Synchronous call, blocks until complete
gitrepo = git.Git(addon_location)
gitrepo.reset("--hard", "HEAD^")
print (addon_location)
# At this point the addon should be "out of date", checked out to one revision behind
# the most recent.
worker = CheckSingleUpdateWorker(help_addon)
worker.do_work() # Synchronous call
self.assertEqual(help_addon.status(), Addon.Status.UPDATE_AVAILABLE)