



"""simple docstring"""







import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


lowerCAmelCase_   						=   '\\n    Text data.\n    Second line of data.'

lowerCAmelCase_   						=   'file'



@pytest.fixture(scope='''session'''	)
def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Union[str, Any]:
		lowercase__   :    Union[str, Any]				=			tmp_path_factory.mktemp('''data'''	) / (FILE_PATH + '''.zstd''')
		lowercase__   :    Any				=			bytes(__lowerCamelCase						,     '''utf-8'''	)
		with zstd.open(__lowerCamelCase						,     '''wb'''	) as f:
				f.write(__lowerCamelCase	)
		return path



@pytest.fixture
def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Tuple:
		with open(os.path.join(tmpfs.local_root_dir						,     __lowerCamelCase	)						,     '''w'''	) as f:
				f.write(__lowerCamelCase	)
		return FILE_PATH



@pytest.mark.parametrize('''compression_format'''						,     ['''gzip''', '''xz''', '''zstd''']	)
def      __UpperCAmelCase      (    __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase	)							->      Optional[int]:
		lowercase__   :    str				=			{'''gzip''': gz_file, '''xz''': xz_file, '''zstd''': zstd_path}
		lowercase__   :    str				=			input_paths[compression_format]
		lowercase__   :    Tuple				=			tmp_path / '''cache'''
		lowercase__   :    Optional[int]				=			DownloadConfig(cache_dir=__lowerCamelCase						,     extract_compressed_file=__lowerCamelCase	)
		lowercase__   :    Union[str, Any]				=			cached_path(__lowerCamelCase						,     download_config=__lowerCamelCase	)
		with open(__lowerCamelCase	) as f:
				lowercase__   :    Dict				=			f.read()
		with open(__lowerCamelCase	) as f:
				lowercase__   :    int				=			f.read()
		assert extracted_file_content == expected_file_content



@pytest.mark.parametrize('''default_extracted'''						,     [True, False]	)
@pytest.mark.parametrize('''default_cache_dir'''						,     [True, False]	)
def      __UpperCAmelCase      (    __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase						,     __lowerCamelCase	)							->      Union[str, Any]:
		lowercase__   :    Any				=			'''custom_cache'''
		lowercase__   :    Tuple				=			'''custom_extracted_dir'''
		lowercase__   :    Tuple				=			tmp_path / '''custom_extracted_path'''
		if default_extracted:
				lowercase__   :    List[Any]				=			('''downloads''' if default_cache_dir else custom_cache_dir, '''extracted''')
		else:
				monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_DIR'''						,     __lowerCamelCase	)
				monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''						,     str(__lowerCamelCase	)	)
				lowercase__   :    str				=			custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

		lowercase__   :    int				=			xz_file
		lowercase__   :    List[Any]				=			(
		    DownloadConfig(extract_compressed_file=__lowerCamelCase	)
		    if default_cache_dir
		    else DownloadConfig(cache_dir=tmp_path / custom_cache_dir						,     extract_compressed_file=__lowerCamelCase	)
		)
		lowercase__   :    Union[str, Any]				=			cached_path(__lowerCamelCase						,     download_config=__lowerCamelCase	)
		assert Path(__lowerCamelCase	).parent.parts[-2:] == expected



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Optional[int]:
		# absolute path
		lowercase__   :    List[Any]				=			str(Path(__lowerCamelCase	).resolve()	)
		assert cached_path(__lowerCamelCase	) == text_file
		# relative path
		lowercase__   :    Union[str, Any]				=			str(Path(__lowerCamelCase	).resolve().relative_to(Path(os.getcwd()	)	)	)
		assert cached_path(__lowerCamelCase	) == text_file



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Optional[int]:
		# absolute path
		lowercase__   :    Dict				=			str(tmp_path.resolve() / '''__missing_file__.txt'''	)
		with pytest.raises(__lowerCamelCase	):
				cached_path(__lowerCamelCase	)
		# relative path
		lowercase__   :    Dict				=			'''./__missing_file__.txt'''
		with pytest.raises(__lowerCamelCase	):
				cached_path(__lowerCamelCase	)



def      __UpperCAmelCase      (    __lowerCamelCase	)							->      int:
		lowercase__   :    List[str]				=			get_from_cache(f"""tmp://{tmpfs_file}"""	)
		with open(__lowerCamelCase	) as f:
				lowercase__   :    int				=			f.read()
		assert output_file_content == FILE_CONTENT



@patch('''datasets.config.HF_DATASETS_OFFLINE'''						,     __lowerCamelCase	)
def      __UpperCAmelCase      (    )							->      Tuple:
		with pytest.raises(__lowerCamelCase	):
				cached_path('''https://huggingface.co'''	)



@patch('''datasets.config.HF_DATASETS_OFFLINE'''						,     __lowerCamelCase	)
def      __UpperCAmelCase      (    __lowerCamelCase	)							->      List[Any]:
		lowercase__   :    str				=			tmp_path_factory.mktemp('''data'''	) / '''file.html'''
		with pytest.raises(__lowerCamelCase	):
				http_get('''https://huggingface.co'''						,     temp_file=__lowerCamelCase	)
		with pytest.raises(__lowerCamelCase	):
				http_head('''https://huggingface.co'''	)



@patch('''datasets.config.HF_DATASETS_OFFLINE'''						,     __lowerCamelCase	)
def      __UpperCAmelCase      (    __lowerCamelCase	)							->      List[str]:
		lowercase__   :    List[str]				=			tmp_path_factory.mktemp('''data'''	) / '''file.html'''
		with pytest.raises(__lowerCamelCase	):
				ftp_get('''ftp://huggingface.co'''						,     temp_file=__lowerCamelCase	)
		with pytest.raises(__lowerCamelCase	):
				ftp_head('''ftp://huggingface.co'''	)





@patch('''datasets.config.HF_DATASETS_OFFLINE'''						,     __lowerCamelCase	)
def      __UpperCAmelCase      (    __lowerCamelCase	)							->      Union[str, Any]:
		lowercase__   :    List[Any]				=			tmp_path_factory.mktemp('''data'''	) / '''file.html'''
		with pytest.raises(__lowerCamelCase	):
				fsspec_get('''s3://huggingface.co'''						,     temp_file=__lowerCamelCase	)
		with pytest.raises(__lowerCamelCase	):
				fsspec_head('''s3://huggingface.co'''	)





"""simple docstring"""







import unittest

from accelerate import debug_launcher
from accelerate.test_utils import require_cpu, test_ops, test_script
@require_cpu
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		str:






				"""simple docstring"""



				debug_launcher(test_script.main						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Tuple:






				"""simple docstring"""



				debug_launcher(test_ops.main						)
