~ fixed : /var/nicobot & /etc/nicobot where in the wrong order

+ more unit tests
This commit is contained in:
nicobo 2021-02-07 22:24:35 +01:00
parent b4a2c78c7b
commit 97ff373a42
No known key found for this signature in database
GPG key ID: 2581E71C5FA5285F
7 changed files with 123 additions and 1 deletions

View file

@ -1,5 +1,20 @@
build:
python3 setup.py build sdist bdist_wheel
build-docker-debian:
docker build -t nicolabs/nicobot:debian -f debian.Dockerfile .
test:
python3 -m unittest discover -v -s tests
askbot:
python3 -m nicobot.askbot $(ARGS)
transbot:
python3 -m nicobot.transbot $(ARGS)
docker-askbot:
docker run --rm -it nicolabs/nicobot:debian askbot $(ARGS)
docker-transbot:
docker run --rm -it nicolabs/nicobot:debian transbot $(ARGS)

View file

@ -72,7 +72,7 @@ case "${opt_bot}" in
askbot|transbot)
#exec python3 -m "nicobot.${opt_bot}" "$@"
# TODO Allow to override config dirs with the docker command line
exec "${opt_bot}" "--config-dirs" /etc/nicobot /var/nicobot "$@"
exec "${opt_bot}" "--config-dirs" /var/nicobot /etc/nicobot "$@"
;;
*)
echo "Unknown bot : '*{opt_bot}'" >2

View file

@ -0,0 +1,8 @@
locale: en
backend: console
#backend: jabber
#backend: signal
# Activates stealth mode
#stealth: on

View file

@ -0,0 +1,21 @@
en:
# If present, this is a template to use for all messages
all_messages: 🤖 %{message}
# If a message matches this pattern, bot will try to translate <message> into the configured locale
# See https://docs.python.org/3/howto/regex.html#regex-howto for details on the format
translate_default_locale: ^(?P<message>.+)$
# If a message matches this pattern, bot will try to translate <message> into <language>
# See https://docs.python.org/3/howto/regex.html#regex-howto for details on the format
translate: ^(?P<message>.+)\s+in\s+(?P<language>.+)$
# Sent when the bot cannot translate a given message
IDontKnow: I don't know
# Key sentence that shuts the bot down
Shutdown: bye nicobot
# Set the following banners explicitely to the empty string "" to disable them
Hello: nicobot ready 🤟
Goodbye: See you later 👋

View file

@ -0,0 +1,21 @@
fr:
# Si présent, ce motif sera utilisé pour formater tous les messages envoyés
all_messages: 🤖 %{message}
# Si un message correspond à ce motif, le bot tentera de le traduire dans la langue locale configurée
# Voir https://docs.python.org/3/howto/regex.html#regex-howto pour le format
translate_default_locale: ^(?P<message>.+)$
# Si un message correspond à ce motif, le bot tentera de traduire <message> dans la langue <language>
# Voir https://docs.python.org/3/howto/regex.html#regex-howto pour le format
translate: ^(?P<message>.+)\s+en\s+(?P<language>.+)$
# Ce message est envoyé lorsque le bot ne sait pas traduire le message demandé
IDontKnow: Je ne sais pas
# Phrase clé pour éteindre le bot
Shutdown: couché nicobot
# Remplacez ces messages par une chaîne vide "" afin de les désactiver
Hello: nicobot paré 🤟
Goodbye: A+ 👋

View file

@ -0,0 +1,8 @@
locale: en
backend: console
#backend: jabber
#backend: signal
# Activates stealth mode
#stealth: on

View file

@ -4,12 +4,15 @@
import unittest
import argparse
import os
import tempfile
import shutil
# Own classes
from nicobot.helpers import *
from nicobot.bot import Bot
from nicobot.bot import ArgsHelper as BotArgsHelper
from nicobot.askbot import Config as AskbotConfig
from nicobot.transbot import Config as TransbotConfig
from nicobot.jabber import arg_parser as jabber_arg_parser
from nicobot.signalcli import ArgsHelper as SignalArgsHelper
@ -50,6 +53,52 @@ class TestOptions(unittest.TestCase):
self.assertEqual( os.path.realpath('/etc/nicobot'), os.path.realpath(config.config_dirs[0]) )
self.assertEqual( os.path.realpath('/tmp/nicobot'), os.path.realpath(config.config_dirs[1]) )
def test_config_path_with_docker_file_tree(self):
"""
Tests the default configuration tree of the docker image
"""
with tempfile.TemporaryDirectory() as tmpdir:
dir_var_nicobot = os.path.join(tmpdir,'var/nicobot')
dir_etc_nicobot = os.path.join(tmpdir,'etc/nicobot')
# 1. Reproduces the initial Docker environment
shutil.copytree('tests/fixtures/docker_file_tree', tmpdir, dirs_exist_ok=True)
config = TransbotConfig()
# Mimics the command line parameters in the docker image
args = [ '--config-dirs', dir_var_nicobot, dir_etc_nicobot ]
# 2. Test begins
config = parse_args_2pass( self.parser, args, config )
# Directories should be present in the same order
self.assertEqual( 2, len(config.config_dirs) )
self.assertEqual( os.path.realpath(dir_var_nicobot), os.path.realpath(config.config_dirs[0]) )
self.assertEqual( os.path.realpath(dir_etc_nicobot), os.path.realpath(config.config_dirs[1]) )
# In this fixture there is no '/var/nicobot' directory so /etc/nicobot should be elected
self.assertEqual( os.path.realpath(os.path.join(dir_etc_nicobot,'config.yml')), os.path.realpath(config.config_file) )
self.assertEqual( 'console', config.backend )
def test_config_path_with_docker_var_mount(self):
"""
Tests a common configuration tree with docker where the user bind-mounts the /var/nicobot directory
"""
with tempfile.TemporaryDirectory() as tmpdir:
dir_var_nicobot = os.path.join(tmpdir,'var/nicobot')
dir_etc_nicobot = os.path.join(tmpdir,'etc/nicobot')
# 1. Reproduces the initial Docker environment
shutil.copytree('tests/fixtures/docker_with_var_mount', tmpdir, dirs_exist_ok=True)
config = TransbotConfig()
# Mimics the command line parameters in the docker image
args = [ '--config-dirs', dir_var_nicobot, dir_etc_nicobot ]
# 2. Test begins
config = parse_args_2pass( self.parser, args, config )
# Directories should be present in the same order
self.assertEqual( 2, len(config.config_dirs) )
self.assertEqual( os.path.realpath(dir_var_nicobot), os.path.realpath(config.config_dirs[0]) )
self.assertEqual( os.path.realpath(dir_etc_nicobot), os.path.realpath(config.config_dirs[1]) )
# In this fixture both '/var/nicobot' and '/etc/nicobot' exist so the first one should be elected
self.assertEqual( os.path.realpath(os.path.join(dir_var_nicobot,'config.yml')), os.path.realpath(config.config_file) )
self.assertEqual( 'jabber', config.backend )
if __name__ == '__main__':
unittest.main()