From 389ea896b9c79e4183fbec46e94d0823bf2588c4 Mon Sep 17 00:00:00 2001 From: huang06 Date: Mon, 3 Oct 2022 00:33:23 +0000 Subject: [PATCH] SUBMARINE-1333. Replace legacy tmpdir with tmp_path ### What is this PR for? Pytest provides both `tmp_path` and `tmpdir` fixtures to indicate the temporary directory. Nowadays, it's preferred to use the `tmp_path` fixture which returns a `pathlib.Path` instead of the legacy `tmpdir` which returns `py.path.local`. ### What type of PR is it? Improvement ### Todos ### What is the Jira issue? ### How should this be tested? This PR modified the following conftest files: ```text submarine-sdk/pysubmarine/tests/ml/pytorch/model/conftest.py submarine-sdk/pysubmarine/tests/ml/tensorflow/model/conftest.py submarine-sdk/pysubmarine/tests/ml/tensorflow_v2/model/conftest.py ``` Then, run pytest on ` tests/ml/` to validate the changes: ```bash cd submarine-sdk/pysubmarine python3 -m venv .venv source .venv/bin/activate pip install -U pip setuptools wheel pip install -r github-actions/test-requirements.txt pip install -e '.[tf2,pytorch]' pytest tests/ml/ ``` ### Screenshots (if appropriate) ### Questions: * Do the license files need updating? No * Are there breaking changes for older versions? No * Does this need new documentation? No Author: huang06 Signed-off-by: cdmikechen Closes #1000 from huang06/tests/tmp_path and squashes the following commits: ba7fe85b [huang06] ci: change working dir in order to load pytest.ini 8afc76eb [huang06] tests: replace legacy tmpdir with tmp_path --- .github/workflows/python.yml | 1 + .../tests/ml/pytorch/model/conftest.py | 22 +++++++++---------- .../tests/ml/tensorflow/model/conftest.py | 20 ++++++++--------- .../tests/ml/tensorflow_v2/model/conftest.py | 20 ++++++++--------- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index e118955b1f..af696cc22a 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -78,6 +78,7 @@ jobs: - name: List installed packages run: pip list - name: Run unit test + working-directory: ./submarine-sdk/pysubmarine run: pytest -m "not e2e" integration: diff --git a/submarine-sdk/pysubmarine/tests/ml/pytorch/model/conftest.py b/submarine-sdk/pysubmarine/tests/ml/pytorch/model/conftest.py index a425a03c67..4403a06193 100644 --- a/submarine-sdk/pysubmarine/tests/ml/pytorch/model/conftest.py +++ b/submarine-sdk/pysubmarine/tests/ml/pytorch/model/conftest.py @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - import pytest # noqa @@ -32,22 +30,22 @@ @pytest.fixture -def get_model_param(tmpdir): - data_file = os.path.join(str(tmpdir), "libsvm.txt") - save_model_dir = os.path.join(str(tmpdir), "experiment") - os.mkdir(save_model_dir) +def get_model_param(tmp_path): + data_file = tmp_path / "libsvm.txt" + save_model_dir = tmp_path / "experiment" + save_model_dir.mkdir() - with open(data_file, "wt") as writer: + with data_file.open("wt") as writer: writer.write(LIBSVM_DATA) params = { "input": { - "train_data": data_file, - "valid_data": data_file, - "test_data": data_file, + "train_data": str(data_file), + "valid_data": str(data_file), + "test_data": str(data_file), "type": "libsvm", }, - "output": {"save_model_dir": save_model_dir, "metric": "roc_auc"}, + "output": {"save_model_dir": str(save_model_dir), "metric": "roc_auc"}, "training": { "batch_size": 4, "num_epochs": 1, @@ -77,4 +75,4 @@ def get_model_param(tmpdir): } yield params - os.remove(data_file) + data_file.unlink() diff --git a/submarine-sdk/pysubmarine/tests/ml/tensorflow/model/conftest.py b/submarine-sdk/pysubmarine/tests/ml/tensorflow/model/conftest.py index 132bdeaee4..027e832567 100644 --- a/submarine-sdk/pysubmarine/tests/ml/tensorflow/model/conftest.py +++ b/submarine-sdk/pysubmarine/tests/ml/tensorflow/model/conftest.py @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - import pytest LIBSVM_DATA = """1 1:0 2:0.051495 3:0.5 4:0.1 5:0.113437 6:0.874 7:0.01 8:0.08 9:0.028 10:0 @@ -29,22 +27,22 @@ @pytest.fixture -def get_model_param(tmpdir): - data_file = os.path.join(str(tmpdir), "libsvm.txt") - save_model_dir = os.path.join(str(tmpdir), "experiment") - with open(data_file, "wt") as writer: +def get_model_param(tmp_path): + data_file = tmp_path / "libsvm.txt" + save_model_dir = tmp_path / "experiment" + with data_file.open("wt") as writer: writer.write(LIBSVM_DATA) params = { "input": { - "train_data": data_file, - "valid_data": data_file, - "test_data": data_file, + "train_data": str(data_file), + "valid_data": str(data_file), + "test_data": str(data_file), "type": "libsvm", }, - "output": {"save_model_dir": save_model_dir, "metric": "auc"}, + "output": {"save_model_dir": str(save_model_dir), "metric": "auc"}, "training": {"batch_size": 256, "num_epochs": 1, "field_size": 10, "feature_size": 1000}, } yield params - os.remove(data_file) + data_file.unlink() diff --git a/submarine-sdk/pysubmarine/tests/ml/tensorflow_v2/model/conftest.py b/submarine-sdk/pysubmarine/tests/ml/tensorflow_v2/model/conftest.py index 132bdeaee4..027e832567 100644 --- a/submarine-sdk/pysubmarine/tests/ml/tensorflow_v2/model/conftest.py +++ b/submarine-sdk/pysubmarine/tests/ml/tensorflow_v2/model/conftest.py @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - import pytest LIBSVM_DATA = """1 1:0 2:0.051495 3:0.5 4:0.1 5:0.113437 6:0.874 7:0.01 8:0.08 9:0.028 10:0 @@ -29,22 +27,22 @@ @pytest.fixture -def get_model_param(tmpdir): - data_file = os.path.join(str(tmpdir), "libsvm.txt") - save_model_dir = os.path.join(str(tmpdir), "experiment") - with open(data_file, "wt") as writer: +def get_model_param(tmp_path): + data_file = tmp_path / "libsvm.txt" + save_model_dir = tmp_path / "experiment" + with data_file.open("wt") as writer: writer.write(LIBSVM_DATA) params = { "input": { - "train_data": data_file, - "valid_data": data_file, - "test_data": data_file, + "train_data": str(data_file), + "valid_data": str(data_file), + "test_data": str(data_file), "type": "libsvm", }, - "output": {"save_model_dir": save_model_dir, "metric": "auc"}, + "output": {"save_model_dir": str(save_model_dir), "metric": "auc"}, "training": {"batch_size": 256, "num_epochs": 1, "field_size": 10, "feature_size": 1000}, } yield params - os.remove(data_file) + data_file.unlink()