[ci] [python-package] Python tests leave files behind #6361
Description
Description
The Python unit tests in this project leave some files behind when they are done running.
They should be modified to use Python-managed temporary files that are automatically removed, so that:
- successive test runs don't accidentally rely on outputs from previous runs
- files aren't left behind on developers' local systems
Reproducible example
Build the Python package and run the Python tests.
cmake -B build -S .
cmake --build build --target _lightgbm
sh build-python.sh install --precompile
pytest tests/python_package_test
(for more details on this, see #6350).
Look at the files created.
git status --ignored
As of latest master
(b27d81e), you'll see all of these created by tests:
categorical.model
lgb.model
lgb.pkl
lgb_train_data.bin
model.txt
Tree4.gv.pdf
Tree4.gv
Approach
Find the tests that created those files, and ensure that they stop creating them.
For example, it looks like lgb.model
probably comes from here:
LightGBM/tests/python_package_test/test_engine.py
Line 1373 in b27d81e
And that that could be avoided using pytests
's tmp_path
fixture, like this:
For more on how that works, see "How to use temporary directories and files in tests" (pytest docs).
Additional Comments
You do not need to put up a pull request fixing all of these! Contributions that fix any of these would be welcomed.
This list will be updated as these are fixed:
- categorical.model ([ci] prevent
categorical.model
andmodel.txt
files being left behind after testing #6590) - data_dask.csv ([ci] prevent Python tests from leaving behind files #6626)
- lgb.model ([ci] prevent
lgb.model
andlgb.pkl
files being left behind after testing #6518) - lgb.pkl ([ci] prevent
lgb.model
andlgb.pkl
files being left behind after testing #6518) - lgb_train_data.bin ([ci] prevent
train.binary.bin
andlgb_train_data.bin
files being left behind after testing #6606) - model.txt ([ci] prevent
categorical.model
andmodel.txt
files being left behind after testing #6590) - preb.txt ([ci] prevent C API tests from leaving files behind (fixes #6361) #6637)
- train.binary.bin ([ci] prevent
train.binary.bin
andlgb_train_data.bin
files being left behind after testing #6606) - Tree4.gv.pdf ([ci] prevent Python tests from leaving behind files #6626)
- Tree4.gv ([ci] prevent Python tests from leaving behind files #6626)
If you are interested in working on this, comment here to indicate that and to ask for help if you need it.