Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't copy files when testing #905

Merged
merged 22 commits into from
Feb 9, 2024

Conversation

AttackingOrDefending
Copy link
Member

@AttackingOrDefending AttackingOrDefending commented Feb 3, 2024

Type of pull request:

  • Bug fix
  • Feature
  • Other

Description:

Doesn't copy test_bot\lichess.py to lichess.py and the back. In the past, when the tests timed out the wrong lichess.py file would be in lib/ so that the user would have to copy it back manually. strategies.py also doesn't change in testing now.

Related Issues:

None

Checklist:

  • I have read and followed the contribution guidelines.
  • I have added necessary documentation (if applicable).
  • The changes pass all existing tests.

Screenshots/logs (if applicable):

@AttackingOrDefending
Copy link
Member Author

I've also tried:

def get_lichess():
    if __name__ == "main":
        from lib import lichess as lichess1
        return lichess1
    else:  # __name__ == "lichess-bot"
        from test_bot import lichess as lichess2
        return lichess2

but it raises even more errors.

@AttackingOrDefending AttackingOrDefending marked this pull request as draft February 3, 2024 14:07
@MarkZH
Copy link
Collaborator

MarkZH commented Feb 4, 2024

Since there's only ever one instance of the Lichess class, perhaps a better approach is to create the li instance with a runtime-specified type, rather than importing the correct lichess:

    # lichess-bot.py

    # ...
    import test_bot
    # ...
    LICHESS_TYPE = Union[lichess.Lichess, test_bot.lichess.Lichess]

    # ...

    lichess_communicator: type[LICHESS_TYPE]
    if __name__ == "__main__":
        lichess_communicator = lichess.Lichess
    else:
        lichess_communicator = test_bot.lichess.Lichess

    li = lichess_communicator(CONFIG.token, CONFIG.url, __version__, logging_level, max_retries)

This will require fixing the arguments and typing of the test_bot version of the Lichess class to match the original and finding a way to pass the LICHESS_TYPE definition around to other modules. But, I think this will be easier.

@MarkZH
Copy link
Collaborator

MarkZH commented Feb 4, 2024

I do like this change. This will especially help when I'm changing lichess.py and no longer have to worry about losing changes during testing.

@AttackingOrDefending AttackingOrDefending marked this pull request as ready for review February 4, 2024 17:50
@AttackingOrDefending AttackingOrDefending changed the title Pytest Don't copy files when testing Feb 4, 2024
lib/engine_wrapper.py Outdated Show resolved Hide resolved
lichess-bot.py Outdated Show resolved Hide resolved
test_bot/test_bot.py Outdated Show resolved Hide resolved
lichess-bot.py Outdated Show resolved Hide resolved
@MarkZH
Copy link
Collaborator

MarkZH commented Feb 7, 2024

Here's a hacky idea that seems to work. Change the name of the homemade class to something that wouldn't be a legal class name as a signal that this is for testing. In this case, by using hyphens in the class name. That way, users won't be able to accidentally reach the test branch.

diff --git a/lib/engine_wrapper.py b/lib/engine_wrapper.py
index f64a6ec..4fe668b 100644
--- a/lib/engine_wrapper.py
+++ b/lib/engine_wrapper.py
@@ -12,7 +12,6 @@ import datetime
 import time
 import random
 import math
-import sys
 import test_bot.lichess
 from collections import Counter
 from collections.abc import Callable
@@ -588,6 +587,9 @@ class FillerEngine:
         return method
 
 
+test_suffix = "-for-lichess-bot-testing-only"
+
+
 def getHomemadeEngine(name: str) -> type[MinimalEngine]:
     """
     Get the homemade engine with name `name`. e.g. If `name` is `RandomMove` then we will return `strategies.RandomMove`.
@@ -598,8 +600,8 @@ def getHomemadeEngine(name: str) -> type[MinimalEngine]:
     from lib import strategies
     from test_bot import strategies as test_strategies
     engine: type[MinimalEngine]
-    if "pytest" in sys.modules:
-        engine = getattr(test_strategies, name)
+    if name.endswith(test_suffix):
+        engine = getattr(test_strategies, name.removesuffix(test_suffix))
     else:
         engine = getattr(strategies, name)
     return engine
diff --git a/test_bot/test_bot.py b/test_bot/test_bot.py
index 1439c54..09828a8 100644
--- a/test_bot/test_bot.py
+++ b/test_bot/test_bot.py
@@ -16,6 +16,7 @@ import tarfile
 import test_bot.lichess
 from lib import config
 from lib.timer import Timer, to_seconds, seconds
+from lib.engine_wrapper import test_suffix
 from typing import Any
 if "pytest" not in sys.modules:
     sys.exit(f"The script {os.path.basename(__file__)} should only be run by pytest.")
@@ -292,7 +293,7 @@ def test_homemade() -> None:
     with open("./config.yml.default") as file:
         CONFIG = yaml.safe_load(file)
     CONFIG["token"] = ""
-    CONFIG["engine"]["name"] = "Stockfish"
+    CONFIG["engine"]["name"] = "Stockfish" + test_suffix
     CONFIG["engine"]["protocol"] = "homemade"
     CONFIG["pgn_directory"] = "TEMP/homemade_game_record"
     win = run_bot(CONFIG, logging_level)

@MarkZH MarkZH merged commit 35f62d2 into lichess-bot-devs:master Feb 9, 2024
15 checks passed
@AttackingOrDefending AttackingOrDefending deleted the pytest branch February 9, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants