Closed gerth2 closed 11 months ago
RobotPy uses its own implementation of getDeployDirectory
(see https://github.com/robotpy/mostrobotpy/blob/main/subprojects/robotpy-wpilib/wpilib/src/rpy/Filesystem.inc), and the location of the directory is based on where python determines the __main__
module is located, assuming that __main__
has a __file__
attribute.
I tried this on Linux using ./robot.py test
and it prints out the expected directory for me. How are you launching your tests?
I ran more experiments, it looks like it changes based on how I launch.
I created a single testcase and tried running it a few ways, observing the file it put to disk and the contents of that file.
# pylint: disable-all
from wpilib import getDeployDirectory
import logging
def test_deployDirectory():
with open("deploydirlog.txt", "w") as f:
deployDir = getDeployDirectory()
f.write(deployDir)
If I do the same experiment you ran, it is correct.
The results I had were from launching my test suite under vsCode's python debug environment:
{
"name": "Test",
"type": "python",
"request": "launch",
"program": "robot.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"test"
],
"cwd": "${workspaceFolder}"
}
In this case, the deploydirlog.txt
file shows up in the test folder, so something is changing directory there.
I get similarly wonky results when using an extension for running pytest, and clicking "run" next to the single testcase:
c:\Users\chris.vscode\extensions\ms-python.python-2023.20.0\pythonFiles\vscode_pytest\deploy
However, this also does not run the test in the tests
folder, rather one directory up, next to robot.py
Looks like vsCode's debug environment or extentions is both messing with where __main__
is at, and possibly working directory?
maximal reproducible example here from my experiments: https://github.com/RobotCasserole1736/firstRoboPy/tree/chris_test_deployDir
The only supported way to run pytest tests on RobotPy code is via ./robot.py test
, because of the amount of cleanup code necessary -- specifically, see https://github.com/robotpy/pyfrc/blob/b8472df58739031b60c384f3304c6cb56e582f19/pyfrc/test_support/pytest_plugin.py#L114; so for the case where you run pytest directly we can't really fix.
When running python robot.py test
via vscode there must be something incorrect about the way that vscode is loading the launched file. According to SO it seems that the main module's file path is made absolute as of Python 3.9, but when running under vscode's launcher this isn't the case.
We could work around this at https://github.com/robotpy/pyfrc/blob/main/pyfrc/mains/cli_test.py#L75 by adding the following:
# In some cases __main__.__file__ is not an absolute path, and some
# internals depend on that being correct. Set it up before we change
# directories
sys.modules["__main__"].__file__ = abspath(sys.modules["__main__"].__file__)
But I'm not particularly happy about that solution. Open to other ideas?
This should be fixed in the kickoff release due to changes in how the robot code is launched.
Problem description
wpilib.getDeployDirectory()
is returningtests\deploy
for the deploy directory, rather than justdeploy
. It looks like the underlying wpilib implementation doesn't account for the fact that the working directory for tests is not the same folder asrobot.py
?Currently, we're using pathplanner to store
.path
files in the deploy directory. However, when running the test suite, the code that works on the robot fails due to the extratest
in the path.I think that, at least, during tests, the python implementation might need to be unique to make sure it points back at the correct folder, regardless of where the code is running from.
Operating System
Windows
Installed Python Packages
Reproducible example code