Fixing unit tests for Channel command

This commit is contained in:
Griatch 2021-05-05 22:14:28 +02:00
parent 2da679cdd1
commit bbfb77022e
4 changed files with 64 additions and 69 deletions

View file

@ -68,7 +68,6 @@ class AccountCmdSet(CmdSet):
self.add(comms.CmdChannelCreate()) self.add(comms.CmdChannelCreate())
self.add(comms.CmdClock()) self.add(comms.CmdClock())
self.add(comms.CmdCBoot()) self.add(comms.CmdCBoot())
self.add(comms.CmdCemit())
self.add(comms.CmdCWho()) self.add(comms.CmdCWho())
self.add(comms.CmdCdesc()) self.add(comms.CmdCdesc())
self.add(comms.CmdPage()) self.add(comms.CmdPage())

View file

@ -181,7 +181,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
"log_file", default=channel.log_to_file.format(channel_key=channel.key)) "log_file", default=channel.log_to_file.format(channel_key=channel.key))
def send_msg(lines): def send_msg(lines):
return caller.msg( return self.msg(
"".join(line.split("[-]", 1)[1] if "[-]" in line else line for line in lines) "".join(line.split("[-]", 1)[1] if "[-]" in line else line for line in lines)
) )
# asynchronously tail the log file # asynchronously tail the log file
@ -699,6 +699,8 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
switches = self.switches switches = self.switches
channel_names = [name for name in self.lhslist if name] channel_names = [name for name in self.lhslist if name]
#from evennia import set_trace;set_trace()
if not channel_names: if not channel_names:
if 'all' in switches: if 'all' in switches:
# show all available channels # show all available channels
@ -719,7 +721,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
return return
if not self.switches and not self.args: if not self.switches and not self.args:
caller.msg("Usage[/switches]: channel [= message]") self.msg("Usage[/switches]: channel [= message]")
return return
if 'create' in switches: if 'create' in switches:
@ -801,8 +803,10 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
return return
if 'sub' in switches: if 'sub' in switches:
# subscribe to a channel aliases = set(alias.strip().lower() for # subscribe to a channel
# alias in self.rhs.split(";")) aliases = []
if self.rhs:
aliases = set(alias.strip().lower() for alias in self.rhs.split(";"))
success, err = self.sub_to_channel(channel) success, err = self.sub_to_channel(channel)
if success: if success:
for alias in aliases: for alias in aliases:
@ -864,7 +868,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
def _perform_delete(caller, *args, **kwargs): def _perform_delete(caller, *args, **kwargs):
self.destroy_channel(channel, message=reason) self.destroy_channel(channel, message=reason)
caller.msg(f"Channel {channel.key} was successfully deleted.") self.msg(f"Channel {channel.key} was successfully deleted.")
ask_yes_no( ask_yes_no(
caller, caller,
@ -904,9 +908,9 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
success, err = self.set_lock(channel, self.rhs) success, err = self.set_lock(channel, self.rhs)
if success: if success:
caller.msg("Added/updated lock on channel.") self.msg("Added/updated lock on channel.")
else: else:
caller.msg(f"Could not add/update lock: {err}") self.msg(f"Could not add/update lock: {err}")
return return
if 'unlock' in switches: if 'unlock' in switches:
@ -923,16 +927,16 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
success, err = self.unset_lock(channel, self.rhs) success, err = self.unset_lock(channel, self.rhs)
if success: if success:
caller.msg("Removed lock from channel.") self.msg("Removed lock from channel.")
else: else:
caller.msg(f"Could not remove lock: {err}") self.msg(f"Could not remove lock: {err}")
return return
if 'boot' in switches: if 'boot' in switches:
# boot a user from channel(s) # boot a user from channel(s)
if not self.rhs: if not self.rhs:
caller.msg("Usage: channel/boot channel[,channel,...] = username [:reason]") self.msg("Usage: channel/boot channel[,channel,...] = username [:reason]")
return return
target_str, *reason = self.rhs.rsplit(":", 1) target_str, *reason = self.rhs.rsplit(":", 1)
@ -947,16 +951,16 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
# the target must be a member of all given channels # the target must be a member of all given channels
target = caller.search(target_str, candidates=chan.subscriptions.all()) target = caller.search(target_str, candidates=chan.subscriptions.all())
if not target: if not target:
caller.msg(f"Cannot boot '{target_str}' - not in channel {chan.key}.") self.msg(f"Cannot boot '{target_str}' - not in channel {chan.key}.")
return return
def _boot_user(caller, *args, **kwargs): def _boot_user(caller, *args, **kwargs):
for chan in channels: for chan in channels:
success, err = self.boot_user(chan, target, quiet=False, reason=reason) success, err = self.boot_user(chan, target, quiet=False, reason=reason)
if success: if success:
caller.msg(f"Booted {target.key} from channel {chan.key}.") self.msg(f"Booted {target.key} from channel {chan.key}.")
else: else:
caller.msg(f"Cannot boot {target.key} from channel {chan.key}: {err}") self.msg(f"Cannot boot {target.key} from channel {chan.key}: {err}")
channames = ", ".join(chan.key for chan in channels) channames = ", ".join(chan.key for chan in channels)
reasonwarn = (". Also note that your reason will be echoed to the channel" reasonwarn = (". Also note that your reason will be echoed to the channel"
@ -994,13 +998,13 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
for chan in channels: for chan in channels:
# the target must be a member of all given channels # the target must be a member of all given channels
if not chan.access(caller, "control"): if not chan.access(caller, "control"):
caller.msg(f"You don't have access to ban users on channel {chan.key}") self.msg(f"You don't have access to ban users on channel {chan.key}")
return return
target = caller.search(target_str, candidates=chan.subscriptions.all()) target = caller.search(target_str, candidates=chan.subscriptions.all())
if not target: if not target:
caller.msg(f"Cannot ban '{target_str}' - not in channel {chan.key}.") self.msg(f"Cannot ban '{target_str}' - not in channel {chan.key}.")
return return
def _ban_user(caller, *args, **kwargs): def _ban_user(caller, *args, **kwargs):
@ -1036,7 +1040,7 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
for chan in channels: for chan in channels:
# the target must be a member of all given channels # the target must be a member of all given channels
if not chan.access(caller, "control"): if not chan.access(caller, "control"):
caller.msg(f"You don't have access to unban users on channel {chan.key}") self.msg(f"You don't have access to unban users on channel {chan.key}")
return return
banlists.extend(chan.banlist) banlists.extend(chan.banlist)
@ -1058,10 +1062,15 @@ class CmdChannel(COMMAND_DEFAULT_CLASS):
who_list = [f"Subscribed to {channel.key}:"] who_list = [f"Subscribed to {channel.key}:"]
who_list.extend(self.channel_list_who(channel)) who_list.extend(self.channel_list_who(channel))
caller.msg("\n".join(who_list)) self.msg("\n".join(who_list))
return return
# a channel-command parent for use with Characters/Objects.
class CmdObjectChannel(CmdChannel):
account_caller = False
class CmdAddCom(CmdChannel): class CmdAddCom(CmdChannel):
""" """
Add a channel alias and/or subscribe to a channel Add a channel alias and/or subscribe to a channel
@ -1117,15 +1126,14 @@ class CmdAddCom(CmdChannel):
return return
if channel.unmute(caller): if channel.unmute(caller):
string += "You unmute channel %s." % channel.key self.msg(f"You unmute channel {channel.key}.")
else: else:
string += "You are already connected to channel %s." % channel.key self.msg(f"You are already connected to channel {channel.key}.")
if alias: if alias:
# create a nick and add it to the caller. # create a nick and add it to the caller.
self.add_alias(channel, alias) self.add_alias(channel, alias)
self.msg(f" You can now refer to the channel {channel} with the alias '{alias}'.") self.msg(f" You can now refer to the channel {channel} with the alias '{alias}'.")
self.msg(string % (channel.key, alias))
else: else:
string += " No alias added." string += " No alias added."
self.msg(string) self.msg(string)
@ -1219,8 +1227,11 @@ class CmdAllCom(CmdChannel):
caller = self.caller caller = self.caller
args = self.args args = self.args
if not args: if not args:
self.execute_cmd("channels") subscribed, available = self.list_channels()
self.msg("(Usage: allcom on | off | who | destroy)") table = self.display_all_channels(subscribed, available)
self.msg(
"\n|wAvailable channels:\n{table}")
return
return return
if args == "on": if args == "on":

View file

@ -35,7 +35,6 @@ from evennia.commands.default import (
unloggedin, unloggedin,
syscommands, syscommands,
) )
from evennia.commands.cmdparser import build_matches
from evennia.commands.default.muxcommand import MuxCommand from evennia.commands.default.muxcommand import MuxCommand
from evennia.commands.command import Command, InterruptCommand from evennia.commands.command import Command, InterruptCommand
from evennia.commands import cmdparser from evennia.commands import cmdparser
@ -1770,21 +1769,13 @@ class TestComms(CommandTest):
self.call( self.call(
comms.CmdAddCom(), comms.CmdAddCom(),
"tc = testchan", "tc = testchan",
"You are already connected to channel testchan. You can now", "You are already connected to channel testchan.| You can now",
receiver=self.account, receiver=self.account,
) )
self.call( self.call(
comms.CmdDelCom(), comms.CmdDelCom(),
"tc", "tc",
"Your alias 'tc' for channel testchan was cleared.", "Any alias 'tc' for channel testchan was cleared.",
receiver=self.account,
)
def test_channels(self):
self.call(
comms.CmdChannels(),
"",
"Available channels (use comlist,addcom and delcom to manage",
receiver=self.account, receiver=self.account,
) )
@ -1792,7 +1783,7 @@ class TestComms(CommandTest):
self.call( self.call(
comms.CmdAllCom(), comms.CmdAllCom(),
"", "",
"Available channels (use comlist,addcom and delcom to manage", "Available channels:",
receiver=self.account, receiver=self.account,
) )
@ -1812,14 +1803,6 @@ class TestComms(CommandTest):
receiver=self.account, receiver=self.account,
) )
def test_cemit(self):
self.call(
comms.CmdCemit(),
"testchan = Test Message",
"[testchan] Test Message|Sent to channel testchan: Test Message",
receiver=self.account,
)
def test_cwho(self): def test_cwho(self):
self.call( self.call(
comms.CmdCWho(), comms.CmdCWho(),
@ -1869,6 +1852,8 @@ class TestCommsChannel(CommandTest):
key="testchannel", key="testchannel",
desc="A test channel") desc="A test channel")
self.channel.connect(self.char1) self.channel.connect(self.char1)
self.cmdchannel = comms.CmdChannel
self.cmdchannel.account_caller = False
def tearDown(self): def tearDown(self):
if self.channel.pk: if self.channel.pk:
@ -1877,7 +1862,7 @@ class TestCommsChannel(CommandTest):
# test channel command # test channel command
def test_channel__noarg(self): def test_channel__noarg(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"", "",
"Channel subscriptions" "Channel subscriptions"
) )
@ -1885,7 +1870,7 @@ class TestCommsChannel(CommandTest):
def test_channel__msg(self): def test_channel__msg(self):
self.channel.msg = Mock() self.channel.msg = Mock()
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"testchannel = Test message", "testchannel = Test message",
"" ""
) )
@ -1893,14 +1878,14 @@ class TestCommsChannel(CommandTest):
def test_channel__list(self): def test_channel__list(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/list", "/list",
"Channel subscriptions" "Channel subscriptions"
) )
def test_channel__all(self): def test_channel__all(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/all", "/all",
"Available channels" "Available channels"
) )
@ -1908,7 +1893,7 @@ class TestCommsChannel(CommandTest):
def test_channel__history(self): def test_channel__history(self):
with patch("evennia.commands.default.comms.tail_log_file") as mock_tail: with patch("evennia.commands.default.comms.tail_log_file") as mock_tail:
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/history testchannel", "/history testchannel",
"" ""
) )
@ -1918,18 +1903,16 @@ class TestCommsChannel(CommandTest):
self.channel.disconnect(self.char1) self.channel.disconnect(self.char1)
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/sub testchannel", "/sub testchannel",
"You are now subscribed" "You are now subscribed"
) )
self.assertTrue(self.char1 in self.channel.subscriptions.all()) self.assertTrue(self.char1 in self.channel.subscriptions.all())
alias_msg = comms.CmdChannel.channel_msg_nick_alias.format(alias='testchannel') self.assertEqual(self.char1.nicks.nickreplace("testchannel Hello"), "channel testchannel = Hello")
self.assertEqual(self.char1.nicks.get(alias_msg, category="channel"),
"channel testchannel = $1")
def test_channel__unsub(self): def test_channel__unsub(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/unsub testchannel", "/unsub testchannel",
"You un-subscribed" "You un-subscribed"
) )
@ -1939,24 +1922,24 @@ class TestCommsChannel(CommandTest):
"""Add and then remove a channel alias""" """Add and then remove a channel alias"""
# add alias # add alias
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/alias testchannel = foo", "/alias testchannel = foo",
"Added/updated your alias 'foo' for channel testchannel." "Added/updated your alias 'foo' for channel testchannel."
) )
self.assertEqual( self.assertEqual(
self.char1.nicks.get('foo $1', category="channel"), "channel testchannel = $1") self.char1.nicks.nickreplace('foo Hello'), "channel testchannel = Hello")
# use alias # use alias
self.channel.msg = Mock() self.channel.msg = Mock()
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"foo = test message", "foo = test message",
"") "")
self.channel.msg.assert_called_with("test message", senders=self.char1) self.channel.msg.assert_called_with("test message", senders=self.char1)
# remove alias # remove alias
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/unalias testchannel = foo", "/unalias testchannel = foo",
"Removed your channel alias 'foo'" "Removed your channel alias 'foo'"
) )
@ -1964,7 +1947,7 @@ class TestCommsChannel(CommandTest):
def test_channel__mute(self): def test_channel__mute(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/mute testchannel", "/mute testchannel",
"Muted channel testchannel" "Muted channel testchannel"
) )
@ -1974,7 +1957,7 @@ class TestCommsChannel(CommandTest):
self.channel.mute(self.char1) self.channel.mute(self.char1)
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/unmute testchannel = Char1", "/unmute testchannel = Char1",
"Un-muted channel testchannel" "Un-muted channel testchannel"
) )
@ -1982,7 +1965,7 @@ class TestCommsChannel(CommandTest):
def test_channel__create(self): def test_channel__create(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/create testchannel2", "/create testchannel2",
"Created (and joined) new channel" "Created (and joined) new channel"
) )
@ -1990,7 +1973,7 @@ class TestCommsChannel(CommandTest):
def test_channel__destroy(self): def test_channel__destroy(self):
self.channel.msg = Mock() self.channel.msg = Mock()
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/destroy testchannel = delete reason", "/destroy testchannel = delete reason",
"Are you sure you want to delete channel ", "Are you sure you want to delete channel ",
inputs=['Yes'] inputs=['Yes']
@ -2000,14 +1983,14 @@ class TestCommsChannel(CommandTest):
def test_channel__desc(self): def test_channel__desc(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/desc testchannel = Another description", "/desc testchannel = Another description",
"Updated channel description." "Updated channel description."
) )
def test_channel__lock(self): def test_channel__lock(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/lock testchannel = foo:false()", "/lock testchannel = foo:false()",
"Added/updated lock on channel" "Added/updated lock on channel"
) )
@ -2016,7 +1999,7 @@ class TestCommsChannel(CommandTest):
def test_channel__unlock(self): def test_channel__unlock(self):
self.channel.locks.add("foo:true()") self.channel.locks.add("foo:true()")
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/unlock testchannel = foo", "/unlock testchannel = foo",
"Removed lock from channel" "Removed lock from channel"
) )
@ -2029,7 +2012,7 @@ class TestCommsChannel(CommandTest):
self.char2.msg = Mock() self.char2.msg = Mock()
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/boot testchannel = Char2 : Booting from channel!", "/boot testchannel = Char2 : Booting from channel!",
"Are you sure ", "Are you sure ",
inputs=["Yes"] inputs=["Yes"]
@ -2049,7 +2032,7 @@ class TestCommsChannel(CommandTest):
self.char2.msg = Mock() self.char2.msg = Mock()
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/ban testchannel = Char2 : Banning from channel!", "/ban testchannel = Char2 : Banning from channel!",
"Are you sure ", "Are you sure ",
inputs=["Yes"] inputs=["Yes"]
@ -2063,7 +2046,7 @@ class TestCommsChannel(CommandTest):
# unban # unban
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/unban testchannel = Char2", "/unban testchannel = Char2",
"Un-banned Char2 from channel testchannel" "Un-banned Char2 from channel testchannel"
) )
@ -2071,7 +2054,7 @@ class TestCommsChannel(CommandTest):
def test_channel__who(self): def test_channel__who(self):
self.call( self.call(
comms.CmdChannel(), self.cmdchannel(),
"/who testchannel", "/who testchannel",
"Subscribed to testchannel:\nChar" "Subscribed to testchannel:\nChar"
) )

View file

@ -1853,10 +1853,12 @@ def format_grid(elements, width=78, sep=" ", verbatim_elements=None):
decorations in the grid, such as horizontal bars. decorations in the grid, such as horizontal bars.
Returns: Returns:
gridstr: The grid as a list of ready-formatted rows. We return it list: The grid as a list of ready-formatted rows. We return it
like this to make it easier to insert decorations between rows, such like this to make it easier to insert decorations between rows, such
as horizontal bars. as horizontal bars.
""" """
if not elements:
return []
if not verbatim_elements: if not verbatim_elements:
verbatim_elements = [] verbatim_elements = []