Add tests
Some checks failed
Run Limnoria Plugin Tests / test (push) Failing after 17s

This commit is contained in:
Stanislas Daniel Claude Dolcini 2024-08-25 14:18:26 +02:00
parent 9079dd805f
commit 49b92534a7
Signed by: Stan
GPG Key ID: 244943DFF8370D60
6 changed files with 190 additions and 19 deletions

29
.gitea/workflows/test.yml Normal file
View File

@ -0,0 +1,29 @@
name: Run Limnoria Plugin Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the code from the repository
- name: Checkout code
uses: actions/checkout@v4
# Step 2: Set up Python environment
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9' # Use the Python version you need
# Step 3: Install dependencies
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt # Adjust if you don't have a requirements.txt
# Step 4: Run tests
- name: Run Limnoria Plugin Tests
run: |
supybot-test -v --plugins-dir=./plugins/ --no-network

View File

@ -33,27 +33,29 @@
"""
import supybot
from . import config
from . import plugin
from importlib import reload
# Use this for the version of this plugin.
__version__ = ""
__version__ = "1.0.0"
# XXX Replace this with an appropriate author or supybot.Author instance.
__author__ = supybot.authors.unknown
__author__ = supybot.Author('Stanislas Daniel Claude Dolcini', 'Stan', 'stan@wildfiregames.com')
# This is a dictionary mapping supybot.Author instances to lists of
# contributions.
__contributors__ = {}
# This is a url where the most recent plugin package can be downloaded.
__url__ = ''
__url__ = 'https://gitea.wildfiregames.com/Stan/limnoria-GitHistoryChannelLogger'
from . import config
from . import plugin
from importlib import reload
# In case we're being reloaded.
reload(config)
reload(plugin)
if supybot.world.testing:
from . import test
Class = plugin.Class
configure = config.configure

View File

@ -36,9 +36,16 @@ def configure(advanced):
GitHistoryChannelLogger = conf.registerPlugin('GitHistoryChannelLogger')
conf.registerGlobalValue(GitHistoryChannelLogger, 'repos',
registry.SpaceSeparatedListOfStrings([], """Channels to log commits to."""))
registry.SpaceSeparatedSetOfStrings([], """Channels to log commits to."""))
# Register dynamic configuration for repositories
def registerRepos():
repos = GitHistoryChannelLogger.repos()
for repo in repos:
registerRepo(repo)
# Register dynamic configuration for repositories
def registerRepo(name):
group = conf.registerGroup(GitHistoryChannelLogger, name)
conf.registerGlobalValue(group, 'url',
@ -52,7 +59,8 @@ def registerRepo(name):
# Register existing repositories from configuration
repos = GitHistoryChannelLogger.repos()
for repo in repos:
registerRepo(repo)
if len(repos) > 0:
for repo in repos:
registerRepo(repo)
GitHistoryChannelLogger.repos.addCallback(registerRepo)
GitHistoryChannelLogger.repos.addCallback(registerRepos)

View File

@ -49,8 +49,8 @@ def __init__(self, irc):
def do315(self, irc, msg):
print("GitHistoryChannelLogger - do315 in ", msg.args[1])
print("current channels:", irc.state.channels.items())
self.log.info("GitHistoryChannelLogger - do315 in ", msg.args[1])
self.log.info("current channels:", irc.state.channels.items())
self.syncedChannels.append(msg.args[1])
@ -59,11 +59,11 @@ def do315(self, irc, msg):
if channel not in self.syncedChannels:
return
print("Git History - all channels synced", msg.args[1])
self.log.info("Git History - all channels synced", msg.args[1])
# Notify about recent phabricator stories
if self.commit_watcher:
print("thread still already running")
self.log.info("thread still already running")
return
self.commit_watcher = threading.Thread(target=self.watchCommits, args=(irc,), daemon=True)
@ -99,7 +99,6 @@ def checkCommits(self, repo, config, irc):
repo_path = config['url']
branch = config['branch']
channels = config['channels']
try:
localRepo = Repo(repo_path)
o = localRepo.remotes.origin
@ -109,7 +108,6 @@ def checkCommits(self, repo, config, irc):
return
commits = list(localRepo.iter_commits(branch, max_count=5))
commits.reverse()
if(len(commits) == 0):
self.log.info(f"No commits found for repo {repo}")
return
@ -142,7 +140,7 @@ def checkCommits(self, repo, config, irc):
def __loadHash(self, repo, branch):
if not os.path.isfile(f"{repo}.{branch}.txt"):
print(f"{repo}.{branch}.txt", "not found, starting at 0")
self.log.info(f"{repo}.{branch}.txt", "not found, starting at 0")
return "0"
return open(f"{repo}.{branch}.txt", 'r').read().strip()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
GitPython==3.1.43
limnoria==2024.7.24

132
test.py Normal file
View File

@ -0,0 +1,132 @@
# 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)