From 602528315c6d5e4ae1e8f5de106cf56cf5869b16 Mon Sep 17 00:00:00 2001 From: Bilyana Indzheva <36890669+bili2002@users.noreply.github.com> Date: Thu, 15 Aug 2024 07:56:35 +0300 Subject: [PATCH] [ci] prevent `train.binary.bin` and `lgb_train_data.bin` files being left behind after testing (#6606) Co-authored-by: Nikita Titov --- tests/c_api_test/test_.py | 7 ++++--- tests/python_package_test/test_engine.py | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/c_api_test/test_.py b/tests/c_api_test/test_.py index 2931d8b38..743d59dd0 100644 --- a/tests/c_api_test/test_.py +++ b/tests/c_api_test/test_.py @@ -160,7 +160,7 @@ def free_dataset(handle): LIB.LGBM_DatasetFree(handle) -def test_dataset(): +def test_dataset(tmp_path): binary_example_dir = Path(__file__).absolute().parents[2] / "examples" / "binary_classification" train = load_from_file(binary_example_dir / "binary.train", None) test = load_from_mat(binary_example_dir / "binary.test", train) @@ -169,9 +169,10 @@ def test_dataset(): free_dataset(test) test = load_from_csc(binary_example_dir / "binary.test", train) free_dataset(test) - save_to_binary(train, "train.binary.bin") + train_binary = str(tmp_path / "train.binary.bin") + save_to_binary(train, train_binary) free_dataset(train) - train = load_from_file("train.binary.bin", None) + train = load_from_file(train_binary, None) free_dataset(train) diff --git a/tests/python_package_test/test_engine.py b/tests/python_package_test/test_engine.py index a1eab3a15..8e5fddd01 100644 --- a/tests/python_package_test/test_engine.py +++ b/tests/python_package_test/test_engine.py @@ -2021,7 +2021,7 @@ def test_sliced_data(rng): np.testing.assert_allclose(origin_pred, sliced_pred) -def test_init_with_subset(rng): +def test_init_with_subset(tmp_path, rng): data = rng.uniform(size=(50, 2)) y = [1] * 25 + [0] * 25 lgb_train = lgb.Dataset(data, y, free_raw_data=False) @@ -2035,16 +2035,17 @@ def test_init_with_subset(rng): assert lgb_train.get_data().shape[0] == 50 assert subset_data_1.get_data().shape[0] == 30 assert subset_data_2.get_data().shape[0] == 20 - lgb_train.save_binary("lgb_train_data.bin") - lgb_train_from_file = lgb.Dataset("lgb_train_data.bin", free_raw_data=False) + lgb_train_data = str(tmp_path / "lgb_train_data.bin") + lgb_train.save_binary(lgb_train_data) + lgb_train_from_file = lgb.Dataset(lgb_train_data, free_raw_data=False) subset_data_3 = lgb_train_from_file.subset(subset_index_1) subset_data_4 = lgb_train_from_file.subset(subset_index_2) init_gbm_2 = lgb.train(params=params, train_set=subset_data_3, num_boost_round=10, keep_training_booster=True) with np.testing.assert_raises_regex(lgb.basic.LightGBMError, "Unknown format of training data"): lgb.train(params=params, train_set=subset_data_4, num_boost_round=10, init_model=init_gbm_2) - assert lgb_train_from_file.get_data() == "lgb_train_data.bin" - assert subset_data_3.get_data() == "lgb_train_data.bin" - assert subset_data_4.get_data() == "lgb_train_data.bin" + assert lgb_train_from_file.get_data() == lgb_train_data + assert subset_data_3.get_data() == lgb_train_data + assert subset_data_4.get_data() == lgb_train_data def test_training_on_constructed_subset_without_params(rng):