limnoria-GitHistoryChannelL.../test.py
Stanislas Daniel Claude Dolcini f2313e3eca
All checks were successful
Run Limnoria Plugin Tests / test (push) Successful in 6s
Add tests
2024-08-25 14:42:17 +02:00

132 lines
5.3 KiB
Python

# Copyright (C) 2024 Stanislas Daniel Claude Dolcini.
# SPDX-License-Identifier: BSD 3-Clause "New" or "Revised" License
import os
import git
import shutil
from supybot.test import ChannelPluginTestCase
import supybot.ircmsgs as ircmsgs
class GitHistoryChannelLoggerTestCase(ChannelPluginTestCase):
plugins: tuple[str] = ('GitHistoryChannelLogger',)
config: dict[str, any] = {
'supybot.plugins.GitHistoryChannelLogger': True,
'supybot.plugins.GitHistoryChannelLogger.repos': ["test"],
'supybot.plugins.GitHistoryChannelLogger.test.url': "test/",
'supybot.plugins.GitHistoryChannelLogger.test.branch': "main",
'supybot.plugins.GitHistoryChannelLogger.test.channels': ["#test"],
}
def tearDown(self):
"""Clean up all files and directories created by setup_fake_git_remote."""
bare_repo_path = os.getcwd() + "/fake-remote.git"
local_repo_path = os.getcwd() + "/" + self.config['supybot.plugins.GitHistoryChannelLogger.test.url']
if os.path.exists(local_repo_path):
shutil.rmtree(local_repo_path)
if os.path.exists(bare_repo_path):
shutil.rmtree(bare_repo_path)
if os.path.exists(os.getcwd() + "/test.main.txt"):
os.remove(os.getcwd() + "/test.main.txt")
def setUp(self):
"""Set up a fake Git remote and perform operations to push and pull from it."""
super(GitHistoryChannelLoggerTestCase, self).setUp()
bare_repo_path = os.getcwd() + "/fake-remote.git"
local_repo_path = os.getcwd() + "/test"
if not os.path.exists(bare_repo_path):
os.makedirs(bare_repo_path)
bare_repo = git.Repo.init(bare_repo_path, bare=True)
if not os.path.exists(local_repo_path):
os.makedirs(local_repo_path)
local_repo = git.Repo.init(local_repo_path)
file_path = os.path.join(local_repo_path, 'hello.txt')
with open(file_path, 'w') as file:
file.write("Hello, world!")
local_repo.index.add([file_path])
local_repo.index.commit("Initial commit")
# Check if the remote already exists
if 'origin' in [remote.name for remote in local_repo.remotes]:
local_repo.delete_remote('origin')
remote_url = os.path.abspath(bare_repo_path)
local_repo.create_remote('origin', remote_url)
if 'main' not in local_repo.heads:
main_branch = local_repo.create_head('main')
local_repo.head.reference = main_branch
local_repo.head.reset(index=True, working_tree=True)
# Push to the remote, ensuring that all refs are pushed
local_repo.git.push('--set-upstream', 'origin', 'main')
def testLoadHash(self) -> None:
for repo in self.config['supybot.plugins.GitHistoryChannelLogger.repos']:
obj = self.irc.getCallback('GitHistoryChannelLogger')
self.assertEqual(obj._GitHistoryChannelLogger__loadHash(repo, "main"), '0')
obj._GitHistoryChannelLogger__saveHash(repo, "main", "3")
self.assertEqual(obj._GitHistoryChannelLogger__loadHash(repo, "main"), '3')
obj._GitHistoryChannelLogger__saveHash(repo, "main", "0")
def testCheckCommits(self) -> None:
for repo in self.config['supybot.plugins.GitHistoryChannelLogger.repos']:
obj = self.irc.getCallback('GitHistoryChannelLogger')
obj.checkCommits(repo, {
'url': self.config['supybot.plugins.GitHistoryChannelLogger.test.url'],
'branch': self.config['supybot.plugins.GitHistoryChannelLogger.test.branch'],
'channels': self.config['supybot.plugins.GitHistoryChannelLogger.test.channels'],
}, self.irc)
count = 0
msg = self.irc.takeMsg()
while msg is not None:
msg = self.irc.takeMsg()
count += 1
self.assertEquals(count, 1)
path = self.config['supybot.plugins.GitHistoryChannelLogger.test.url']
local_repo = git.Repo(path)
file_path = os.path.join(os.getcwd(), path, 'README.md')
with open(file_path, 'w') as file:
file.write("# Introduction")
local_repo.index.add([file_path])
local_repo.index.commit("Add README")
local_repo.git.push('--set-upstream', 'origin', 'main')
obj.checkCommits(repo, {
'url': self.config['supybot.plugins.GitHistoryChannelLogger.test.url'],
'branch': self.config['supybot.plugins.GitHistoryChannelLogger.test.branch'],
'channels': self.config['supybot.plugins.GitHistoryChannelLogger.test.channels'],
}, self.irc)
count = 0
msg = self.irc.takeMsg()
while msg is not None:
msg = self.irc.takeMsg()
count += 1
path = self.config['supybot.plugins.GitHistoryChannelLogger.test.url']
local_repo = git.Repo(path)
file_path = os.path.join(os.getcwd(), path, 'LICENSE.md')
with open(file_path, 'w') as file:
file.write("# Introduction")
local_repo.index.add([file_path])
local_repo.index.commit("Add LICENSE")
local_repo.git.push('--set-upstream', 'origin', 'main')
self.assertEquals(count, 1)