diff --git a/tests/remotes/azure.py b/tests/remotes/azure.py index de26cb829..14306a472 100644 --- a/tests/remotes/azure.py +++ b/tests/remotes/azure.py @@ -53,12 +53,6 @@ class Azure(Base, CloudURLInfo): def write_bytes(self, contents): self.blob_client.upload_blob(contents, overwrite=True) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): stream = self.blob_client.download_blob() return stream.readall() diff --git a/tests/remotes/base.py b/tests/remotes/base.py index 6e27c1d18..704503f3f 100644 --- a/tests/remotes/base.py +++ b/tests/remotes/base.py @@ -1,3 +1,4 @@ +import locale import pathlib from funcy import cached_property @@ -19,7 +20,10 @@ class Base(URLInfo): raise NotImplementedError def write_text(self, contents, encoding=None, errors=None): - raise NotImplementedError + if not encoding: + encoding = locale.getpreferredencoding(False) + assert errors is None + self.write_bytes(contents.encode(encoding)) def write_bytes(self, contents): raise NotImplementedError diff --git a/tests/remotes/gs.py b/tests/remotes/gs.py index 6870c5121..0099ccc84 100644 --- a/tests/remotes/gs.py +++ b/tests/remotes/gs.py @@ -84,12 +84,6 @@ class GCP(Base, CloudURLInfo): assert isinstance(contents, bytes) self._blob.upload_from_string(contents) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): return self._blob.download_as_string() diff --git a/tests/remotes/hdfs.py b/tests/remotes/hdfs.py index c49da7703..690ff2944 100644 --- a/tests/remotes/hdfs.py +++ b/tests/remotes/hdfs.py @@ -58,12 +58,6 @@ class HDFS(Base, URLInfo): # pylint: disable=abstract-method with _hdfs.open_output_stream(self.path) as fobj: fobj.write(contents) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): with self._hdfs() as _hdfs: with _hdfs.open_input_stream(self.path) as fobj: diff --git a/tests/remotes/http.py b/tests/remotes/http.py index 49775b7b8..ee9978fb1 100644 --- a/tests/remotes/http.py +++ b/tests/remotes/http.py @@ -1,5 +1,4 @@ # pylint:disable=abstract-method -import locale import os import uuid @@ -26,12 +25,6 @@ class HTTP(Base, HTTPURLInfo): response = requests.post(self.url, data=contents) assert response.status_code == 200 - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - @pytest.fixture(scope="session") def http_server(test_config, tmp_path_factory): diff --git a/tests/remotes/s3.py b/tests/remotes/s3.py index e90da9611..22f8c9a3d 100644 --- a/tests/remotes/s3.py +++ b/tests/remotes/s3.py @@ -80,12 +80,6 @@ class S3(Base, CloudURLInfo): Bucket=self.bucket, Key=self.path, Body=contents, ) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): data = self._s3.get_object(Bucket=self.bucket, Key=self.path) return data["Body"].read() diff --git a/tests/remotes/ssh.py b/tests/remotes/ssh.py index ebdeeea21..2cf386289 100644 --- a/tests/remotes/ssh.py +++ b/tests/remotes/ssh.py @@ -92,12 +92,6 @@ class SSHMocked(Base, URLInfo): # NOTE: accepts both str and bytes fobj.write(contents) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): with self._ssh() as _ssh: # NOTE: sftp always reads in binary format diff --git a/tests/remotes/webdav.py b/tests/remotes/webdav.py index 9c4938a69..e91d9a68a 100644 --- a/tests/remotes/webdav.py +++ b/tests/remotes/webdav.py @@ -1,12 +1,9 @@ # pylint:disable=abstract-method import os -import uuid -from pathlib import Path -from tempfile import TemporaryDirectory from wsgiref.simple_server import make_server import pytest -from funcy import first +from funcy import cached_property, first from wsgidav.wsgidav_app import WsgiDAVApp from dvc.path_info import WebDAVURLInfo @@ -15,27 +12,34 @@ from tests.utils.httpd import run_server_on_thread from .base import Base AUTH = {"user1": {"password": "password1"}} -_WEBDAV_ROOT = TemporaryDirectory() -_WEBDAV_DIR = Path(_WEBDAV_ROOT.name) class Webdav(Base, WebDAVURLInfo): @staticmethod - def get_url(port, root_id): # pylint: disable=arguments-differ - return f"webdav://localhost:{port}/{root_id}/" + def get_url(port): # pylint: disable=arguments-differ + return f"webdav://localhost:{port}" + + @cached_property + def client(self): + from webdav3.client import Client + + user, secrets = first(AUTH.items()) + return Client( + { + "webdav_hostname": self.replace(path="").url, + "webdav_login": user, + "webdav_password": secrets["password"], + } + ) def mkdir(self, mode=0o777, parents=False, exist_ok=False): - self.dir_path.mkdir(parents=parents, exist_ok=True) + assert mode == 0o777 + parent_dirs = list(reversed(self.parents))[1:] if parents else [] + for d in parent_dirs + [self]: + self.client.mkdir(d.path) # pylint: disable=no-member def write_bytes(self, contents): - self.dir_path.write_bytes(contents) - - def write_text(self, contents, encoding=None, errors=None): - self.dir_path.write_text(contents, encoding=encoding, errors=errors) - - @property - def dir_path(self): - return _WEBDAV_DIR / self.path[1:] + self.client.upload_to(contents, self.path) @pytest.fixture @@ -43,10 +47,8 @@ def webdav_server(test_config, tmp_path_factory): test_config.requires("webdav") host, port = "localhost", 0 - root_id = str(uuid.uuid4()) - root_dir = _WEBDAV_DIR / root_id - root_dir.mkdir() - dirmap = {f"/{root_id}": os.fspath(_WEBDAV_DIR / root_id)} + directory = os.fspath(tmp_path_factory.mktemp("http")) + dirmap = {"/": directory} app = WsgiDAVApp( { @@ -57,14 +59,13 @@ def webdav_server(test_config, tmp_path_factory): } ) server = make_server(host, port, app) - server.root_id = root_id with run_server_on_thread(server) as httpd: yield httpd @pytest.fixture def webdav(webdav_server): - url = Webdav.get_url(webdav_server.server_port, webdav_server.root_id) + url = Webdav.get_url(webdav_server.server_port) ret = Webdav(url) user, secrets = first(AUTH.items()) ret.config = {"url": url, "user": user, **secrets} diff --git a/tests/remotes/webhdfs.py b/tests/remotes/webhdfs.py index 08f673fa5..634fcce79 100644 --- a/tests/remotes/webhdfs.py +++ b/tests/remotes/webhdfs.py @@ -45,12 +45,6 @@ class WebHDFS(Base, URLInfo): # pylint: disable=abstract-method with _hdfs.write(self.path, overwrite=True) as writer: writer.write(contents) - def write_text(self, contents, encoding=None, errors=None): - if not encoding: - encoding = locale.getpreferredencoding(False) - assert errors is None - self.write_bytes(contents.encode(encoding)) - def read_bytes(self): with self._webhdfs() as _hdfs: with _hdfs.read(self.path) as reader: