

import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


A_ :				Dict						 =    get_tests_dir('fixtures')
A_ :				List[str]						 =    get_tests_dir('fixtures/dummy_feature_extractor_config.json')
A_ :				Dict						 =    get_tests_dir('fixtures/dummy-config.json')



class 					_a		(unittest.TestCase       ):


					'''simple docstring'''






					def 						__A    (				self    ):
						A__							:					List[Any]       =	0






					def 						__A    (				self    ):
						A__							:					List[Any]       =	AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""    )
						self.assertIsInstance(A__					, A__    )






					def 						__A    (				self    ):
						A__							:					Optional[int]       =	AutoFeatureExtractor.from_pretrained(A__    )
						self.assertIsInstance(A__					, A__    )






					def 						__A    (				self    ):
						with tempfile.TemporaryDirectory() as tmpdirname:
							A__							:					int       =	WavaVecaConfig()

							# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
							A__							:					Any       =	AutoFeatureExtractor.from_pretrained(A__    ).to_dict()

							config_dict.pop("""feature_extractor_type"""    )
							A__							:					str       =	WavaVecaFeatureExtractor(**A__    )

							# save in new folder
							model_config.save_pretrained(A__    )
							config.save_pretrained(A__    )

							A__							:					Optional[int]       =	AutoFeatureExtractor.from_pretrained(A__    )

							# make sure private variable is not incorrectly saved
							A__							:					List[Any]       =	json.loads(config.to_json_string()    )
							self.assertTrue("""_processor_class""" not in dict_as_saved    )

						self.assertIsInstance(A__					, A__    )






					def 						__A    (				self    ):
						A__							:					Union[str, Any]       =	AutoFeatureExtractor.from_pretrained(A__    )
						self.assertIsInstance(A__					, A__    )






					def 						__A    (				self    ):
						with self.assertRaisesRegex(
						    A__					, """bert-base is not a local folder and is not a valid model identifier"""    ):
							A__							:					int       =	AutoFeatureExtractor.from_pretrained("""bert-base"""    )






					def 						__A    (				self    ):
						with self.assertRaisesRegex(
						    A__					, r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
							A__							:					Union[str, Any]       =	AutoFeatureExtractor.from_pretrained(A__					, revision="""aaaaaa"""    )






					def 						__A    (				self    ):
						with self.assertRaisesRegex(
						    A__					, """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""					, ):
							A__							:					Optional[int]       =	AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""    )






					def 						__A    (				self    ):
						# If remote code is not set, we will time out when asking whether to load the model.
						with self.assertRaises(A__    ):
							A__							:					str       =	AutoFeatureExtractor.from_pretrained(
							    """hf-internal-testing/test_dynamic_feature_extractor"""    )
						# If remote code is disabled, we can't load this config.
						with self.assertRaises(A__    ):
							A__							:					Tuple       =	AutoFeatureExtractor.from_pretrained(
							    """hf-internal-testing/test_dynamic_feature_extractor"""					, trust_remote_code=A__    )

						A__							:					Optional[int]       =	AutoFeatureExtractor.from_pretrained(
						    """hf-internal-testing/test_dynamic_feature_extractor"""					, trust_remote_code=A__    )
						self.assertEqual(feature_extractor.__class__.__name__					, """NewFeatureExtractor"""    )

						# Test feature extractor can be reloaded.
						with tempfile.TemporaryDirectory() as tmp_dir:
							feature_extractor.save_pretrained(A__    )
							A__							:					Any       =	AutoFeatureExtractor.from_pretrained(A__					, trust_remote_code=A__    )
						self.assertEqual(reloaded_feature_extractor.__class__.__name__					, """NewFeatureExtractor"""    )






					def 						__A    (				self    ):
						try:
							AutoConfig.register("""custom"""					, A__    )
							AutoFeatureExtractor.register(A__					, A__    )
							# Trying to register something existing in the Transformers library will raise an error
							with self.assertRaises(A__    ):
								AutoFeatureExtractor.register(A__					, A__    )

							# Now that the config is registered, it can be used as any other config with the auto-API
							A__							:					Dict       =	CustomFeatureExtractor.from_pretrained(A__    )
							with tempfile.TemporaryDirectory() as tmp_dir:
								feature_extractor.save_pretrained(A__    )
								A__							:					Optional[Any]       =	AutoFeatureExtractor.from_pretrained(A__    )
								self.assertIsInstance(A__					, A__    )

						finally:
							if "custom" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
								del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]






					def 						__A    (				self    ):



						class 					_a		(__magic_name__       ):


											'''simple docstring'''






											UpperCAmelCase__:     Dict													= True

						try:
							AutoConfig.register("""custom"""					, A__    )
							AutoFeatureExtractor.register(A__					, A__    )
							# If remote code is not set, the default is to use local
							A__							:					Tuple       =	AutoFeatureExtractor.from_pretrained(
							    """hf-internal-testing/test_dynamic_feature_extractor"""    )
							self.assertEqual(feature_extractor.__class__.__name__					, """NewFeatureExtractor"""    )
							self.assertTrue(feature_extractor.is_local    )

							# If remote code is disabled, we load the local one.
							A__							:					Optional[Any]       =	AutoFeatureExtractor.from_pretrained(
							    """hf-internal-testing/test_dynamic_feature_extractor"""					, trust_remote_code=A__    )
							self.assertEqual(feature_extractor.__class__.__name__					, """NewFeatureExtractor"""    )
							self.assertTrue(feature_extractor.is_local    )

							# If remote is enabled, we load from the Hub
							A__							:					Any       =	AutoFeatureExtractor.from_pretrained(
							    """hf-internal-testing/test_dynamic_feature_extractor"""					, trust_remote_code=A__    )
							self.assertEqual(feature_extractor.__class__.__name__					, """NewFeatureExtractor"""    )
							self.assertTrue(not hasattr(A__					, """is_local"""    )    )

						finally:
							if "custom" in CONFIG_MAPPING._extra_content:
								del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
								del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]






# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
A_ :				Optional[int]						 =    abspath(join(dirname(__file__), 'src'))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='ignore', category=FutureWarning)

def 		UpperCamelCase      (lowercase_:				List[str]      )  ->   Any:
	config.addinivalue_line(
	    """markers"""					,   """is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"""      )
	config.addinivalue_line(
	    """markers"""					,   """is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"""      )
	config.addinivalue_line("""markers"""					,   """is_pipeline_test: mark test to run only when pipelines are tested"""      )
	config.addinivalue_line("""markers"""					,   """is_staging_test: mark test to run only in the staging environment"""      )
	config.addinivalue_line("""markers"""					,   """accelerate_tests: mark test that require accelerate"""      )
	config.addinivalue_line("""markers"""					,   """tool_tests: mark the tool tests that are run on their specific schedule"""      )

def 		UpperCamelCase      (lowercase_:				Optional[int]      )  ->   Optional[Any]:
	from transformers.testing_utils import pytest_addoption_shared

	pytest_addoption_shared(lowercase_      )

def 		UpperCamelCase      (lowercase_:				List[str]      )  ->   Optional[Any]:
	from transformers.testing_utils import pytest_terminal_summary_main

	A__							:					List[Any]       =	terminalreporter.config.getoption("""--make-reports"""      )
	if make_reports:
		pytest_terminal_summary_main(lowercase_					,   id=lowercase_      )

def 		UpperCamelCase      (lowercase_:				Union[str, Any]					,   lowercase_:				int      )  ->   List[str]:
	# If no tests are collected, pytest exists with code 5, which makes the CI fail.
	if exitstatus == 5:
		A__							:					Tuple       =	0


# Doctest custom flag to ignore output.
A_ :				Tuple						 =    doctest.register_optionflag('IGNORE_RESULT')

A_ :				Dict						 =    doctest.OutputChecker



class 					_a		(__magic_name__       ):


					'''simple docstring'''






					def 						__A    (				self					, A__					, A__					, A__    ):
						if IGNORE_RESULT & optionflags:
							return True
						return OutputChecker.check_output(self					, A__					, A__					, A__    )



A_ :				str						 =    CustomOutputChecker
A_ :				Dict						 =    HfDoctestModule


A_ :				Optional[int]						 =    HfDocTestParser



