CraftSpider / dpytest

A package that assists in writing tests for discord.py
MIT License
102 stars 26 forks source link

[BUG] Dpytest crashes when run via GitHub Actions due to missing flags #96

Closed Diapolo10 closed 1 year ago

Diapolo10 commented 1 year ago

I can run all of my tests locally perfectly fine, all tests passing, so I was rather surprised when I suddenly got a barrage of emails when I pushed my code through routine GitHub Actions checks to be merged.

I can only assume that there's some kind of an incompatibility going on here. Whether this is something dpytest is supposed to support or ignore is not something I would know, but seeing as how useful/important automated testing is I would say it's at least worth considering.

Here's the error from one of my tests, the rest are virtually identical:

  .tox\py310\lib\site-packages\discord\member.py:358: KeyError
  _______________ ERROR at setup of test_hardcore_ironman_success _______________

  event_loop = <ProactorEventLoop running=False closed=False debug=False>
  request = <SubRequest 'bot' for <Function test_hardcore_ironman_success>>
  kwargs = {}
  setup = <function _wrap_async_fixture.<locals>._async_fixture_wrapper.<locals>.setup at 0x0000024C949F3490>

      @functools.wraps(fixture)
      def _async_fixture_wrapper(
          event_loop: asyncio.AbstractEventLoop, request: SubRequest, **kwargs: Any
      ):
          func = _perhaps_rebind_fixture_func(
              fixture, request.instance, fixturedef.unittest
          )

          async def setup():
              res = await func(**_add_kwargs(func, kwargs, event_loop, request))
              return res

  >       return event_loop.run_until_complete(setup())

  .tox\py310\lib\site-packages\pytest_asyncio\plugin.py:321: 
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  C:\hostedtoolcache\windows\Python\3.10.10\x64\lib\asyncio\base_events.py:649: in run_until_complete
      return future.result()
  .tox\py310\lib\site-packages\pytest_asyncio\plugin.py:318: in setup
      res = await func(**_add_kwargs(func, kwargs, event_loop, request))
  tests\conftest.py:28: in bot
      dpytest.configure(_bot)
  .tox\py310\lib\site-packages\discord\ext\test\runner.py:386: in configure
      member = back.make_member(user, guild, nick=user.name + f"_{str(num)}_nick")
  .tox\py310\lib\site-packages\discord\ext\test\backend.py:795: in make_member
      state.parse_guild_member_add(data)
  .tox\py310\lib\site-packages\discord\state.py:1017: in parse_guild_member_add
      member = Member(guild=guild, data=data, state=self)
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

  self = <Member id=1080289725396418583 name='TestUser0' discriminator='0001' bot=False nick='TestUser0_0_nick' guild=<Guild id=1080289725396418581 name='Test Guild 0' shard_id=0 chunked=False member_count=1>>

      def __init__(self, *, data: MemberWithUserPayload, guild: Guild, state: ConnectionState):
          self._state: ConnectionState = state
          self._user: User = state.store_user(data['user'])
          self.guild: Guild = guild
          self.joined_at: Optional[datetime.datetime] = utils.parse_time(data.get('joined_at'))
          self.premium_since: Optional[datetime.datetime] = utils.parse_time(data.get('premium_since'))
          self._roles: utils.SnowflakeList = utils.SnowflakeList(map(int, data['roles']))
          self._client_status: _ClientStatus = _ClientStatus()
          self.activities: Tuple[ActivityTypes, ...] = ()
          self.nick: Optional[str] = data.get('nick', None)
          self.pending: bool = data.get('pending', False)
          self._avatar: Optional[str] = data.get('avatar')
          self._permissions: Optional[int]
  >       self._flags: int = data['flags']
  E       KeyError: 'flags'

The tests failed on both Windows and Linux, so I don't think this is related to the operating system. The failed actions can be seen here: https://github.com/Diapolo10/clan-quest-osrs-discord-bot/actions/runs/4298768145

null2264 commented 1 year ago

It seems to be caused by discord.py v2.2 supporting member flags (https://github.com/Rapptz/discord.py/commit/c46f309c13948cd72adfd3023ee38652f80eedf3) and dpytest is not yet updated to support that version.

I fixed it for the time being by doing monkey-patching in conftest.py:

from discord.ext.test import factories

old_member_dict = factories.make_member_dict
def new_make_member_dict(*args, **kwargs):
    res = old_member_dict(*args, **kwargs)
    res["flags"] = 0
    return res
factories.make_member_dict = new_make_member_dict
Sergeileduc commented 1 year ago

yup, it's because of discord.py 2.2.0 (that's why it's better to specify the version, in you requirements.txt, you wouldn't have that issue if you used discord.py==2.1.1)

Anyway, I allready made a branch to fix that. Thanks to @null2264 by the way, you really pinpoint me in the right spot, so It was piece of cake.

Closing.

(solution is : either you explicitely test with discord.py==2.1.1 or you wait till I released the version with the fix for 2.2.0)

Diapolo10 commented 1 year ago

Okay, I'll make the version requirements more strict in the meantime. Thanks!