Spaces:
Running on Zero
Running on Zero
Download validation/test_source.py from fffiloni/TIGER-audio-extraction: direct link, hf CLI and curl.
- Browser
- Download file 3.82 kB
-
https://huggingface.co/spaces/fffiloni/TIGER-audio-extraction/resolve/main/validation/test_source.py
- Command line
-
hf download hf://spaces/fffiloni/TIGER-audio-extraction/validation/test_source.py
-
curl -L -o test_source.py https://huggingface.co/spaces/fffiloni/TIGER-audio-extraction/resolve/main/validation/test_source.py
3.82 kB
| """The HTML event boundary must never accept arbitrary server files.""" | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| from tiger_ui import source | |
| from tiger_ui.source import resolve_source, settings | |
| from tiger_ui.media import MediaError | |
| class SourceTests(unittest.TestCase): | |
| def test_only_actual_upload_folder_files_are_accepted(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| root=Path(temp);uploads=root/'uploads';uploads.mkdir() | |
| valid=uploads/'audio.wav';valid.write_bytes(b'RIFF') | |
| outside=root/'private.txt';outside.write_text('not an upload') | |
| alias=uploads/'alias.wav';alias.symlink_to(outside) | |
| with patch('tiger_ui.source.get_upload_folder',return_value=str(uploads)): | |
| self.assertEqual(resolve_source({'path':str(valid)}),str(valid)) | |
| for path in [outside,alias,uploads/'../private.txt','https://example.org/audio.wav']: | |
| with self.assertRaises(MediaError):resolve_source({'path':str(path)}) | |
| with self.assertRaises(MediaError):resolve_source({'origin':'example','key':'../../private.txt'}) | |
| def test_settings_validate_both_enums(self): | |
| for field in ['task','media']: | |
| invalid={'task':'stems','media':'audio',field:'anything'} | |
| with self.assertRaises(MediaError):settings(invalid) | |
| def test_local_example_does_not_need_network(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| audio=Path(temp)/'audio.wav';audio.write_bytes(b'RIFF audio') | |
| with patch.dict(source.EXAMPLES, {'stems':audio}), patch.object(source,'hf_hub_download') as download: | |
| self.assertEqual(source.example_path('stems'),audio) | |
| download.assert_not_called() | |
| def test_missing_examples_use_pinned_history_and_reuse_cache(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| root=Path(temp) | |
| def download(**kwargs): | |
| self.assertEqual(kwargs['repo_id'],source.EXAMPLE_REPO) | |
| self.assertEqual(kwargs['revision'],source.EXAMPLE_REVISION) | |
| self.assertFalse(kwargs['token']) | |
| path=Path(kwargs['local_dir'])/kwargs['filename'] | |
| path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(b'RIFF audio') | |
| return str(path) | |
| missing={key:root/'missing'/name for key,name in source.EXAMPLE_FILES.items()} | |
| with patch.dict(source.EXAMPLES,missing), patch.object(source,'get_upload_folder',return_value=str(root/'uploads')), patch.object(source,'hf_hub_download',side_effect=download) as fetch: | |
| for key in source.EXAMPLE_FILES: | |
| first=source.example_path(key) | |
| self.assertTrue(first.is_relative_to(root/'uploads')) | |
| self.assertEqual(resolve_source({'origin':'example','key':key}),str(first)) | |
| self.assertEqual(fetch.call_count,3) | |
| with self.assertRaises(MediaError):source.example_path('../../private.txt') | |
| self.assertEqual(fetch.call_count,3) | |
| def test_git_lfs_pointer_is_not_used_as_audio(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| root=Path(temp);pointer=root/'pointer.wav';pointer.write_text('version https://git-lfs.github.com/spec/v1\n') | |
| real=root/'real.wav';real.write_bytes(b'RIFF audio') | |
| with patch.dict(source.EXAMPLES,{'stems':pointer}), patch.object(source,'get_upload_folder',return_value=str(root/'uploads')), patch.object(source,'hf_hub_download',return_value=str(real)) as fetch: | |
| self.assertEqual(source.example_path('stems'),real) | |
| fetch.assert_called_once() | |
| if __name__=='__main__':unittest.main() | |