forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fetch.py
More file actions
93 lines (74 loc) · 2.59 KB
/
Copy pathtest_fetch.py
File metadata and controls
93 lines (74 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pytest
from speechbrain.utils import fetching
def test_link_with_strategy_symlink_and_copy(tmp_path):
# Create a source file
src = tmp_path / "source.txt"
dst = tmp_path / "dest.txt"
src.write_text("testdata")
# Test COPY
result = fetching.link_with_strategy(src, dst, fetching.LocalStrategy.COPY)
assert dst.exists()
assert dst.read_text() == "testdata"
assert result == dst
# Overwrite with SYMLINK
dst.unlink()
result = fetching.link_with_strategy(
src, dst, fetching.LocalStrategy.SYMLINK
)
assert dst.is_symlink()
assert dst.resolve() == src
# Test NO_LINK
result = fetching.link_with_strategy(
src, dst, fetching.LocalStrategy.NO_LINK
)
assert result == src
def test_link_with_strategy_self_symlink(tmp_path):
# Create a file that is a symlink to itself (simulate error)
path = tmp_path / "loop.txt"
path.write_text("content")
path.unlink()
path.symlink_to(path)
with pytest.raises(ValueError):
fetching.link_with_strategy(path, path, fetching.LocalStrategy.SYMLINK)
def test_guess_source_local_and_uri(tmp_path):
# Local directory
srcdir = tmp_path
fetch_from, path = fetching.guess_source(str(srcdir))
assert fetch_from == fetching.FetchFrom.LOCAL
# URI
fetch_from, path = fetching.guess_source("http://example.com")
assert fetch_from == fetching.FetchFrom.URI
# Huggingface fallback
fetch_from, path = fetching.guess_source("facebook/wav2vec2-base-960h")
assert fetch_from == fetching.FetchFrom.HUGGING_FACE
def test_fetch_local_file(tmp_path):
# Setup local file and dest
srcdir = tmp_path / "src"
srcdir.mkdir()
f = srcdir / "foo.txt"
f.write_text("abc123")
destdir = tmp_path / "dest"
outpath = fetching.fetch(
"foo.txt",
str(srcdir),
savedir=str(destdir),
local_strategy=fetching.LocalStrategy.COPY,
)
assert outpath.exists()
assert outpath.read_text() == "abc123"
assert outpath.parent == destdir
def test_fetch_raises_on_bad_uri(tmp_path):
# Should raise ValueError when fetching from URI without savedir
with pytest.raises(ValueError):
fetching.fetch("foo.txt", "http://example.com", savedir=None)
def test_fetch_raises_on_network_disallowed(tmp_path):
destdir = tmp_path / "dest"
destdir.mkdir()
config = fetching.FetchConfig(allow_network=False)
with pytest.raises(ValueError):
fetching.fetch(
"foo.txt",
"http://example.com",
savedir=str(destdir),
fetch_config=config,
)