|
1 | 1 | import os |
2 | | -import unittest |
| 2 | +from pathlib import Path |
3 | 3 |
|
| 4 | +from torchaudio.datasets import utils as dataset_utils |
4 | 5 | from torchaudio.datasets.commonvoice import COMMONVOICE |
5 | 6 | from torchaudio.datasets.librispeech import LIBRISPEECH |
6 | 7 | from torchaudio.datasets.speechcommands import SPEECHCOMMANDS |
|
22 | 23 | ) |
23 | 24 |
|
24 | 25 |
|
| 26 | +class TestWalkFiles(TempDirMixin, TorchaudioTestCase): |
| 27 | + root = None |
| 28 | + expected = None |
| 29 | + |
| 30 | + def _add_file(self, *parts): |
| 31 | + path = self.get_temp_path(*parts) |
| 32 | + self.expected.append(path) |
| 33 | + Path(path).touch() |
| 34 | + |
| 35 | + def setUp(self): |
| 36 | + self.root = self.get_temp_path() |
| 37 | + self.expected = [] |
| 38 | + |
| 39 | + # level 1 |
| 40 | + for filename in ['a.txt', 'b.txt', 'c.txt']: |
| 41 | + self._add_file(filename) |
| 42 | + |
| 43 | + # level 2 |
| 44 | + for dir1 in ['d1', 'd2', 'd3']: |
| 45 | + for filename in ['d.txt', 'e.txt', 'f.txt']: |
| 46 | + self._add_file(dir1, filename) |
| 47 | + # level 3 |
| 48 | + for dir2 in ['d1', 'd2', 'd3']: |
| 49 | + for filename in ['g.txt', 'h.txt', 'i.txt']: |
| 50 | + self._add_file(dir1, dir2, filename) |
| 51 | + |
| 52 | + print('\n'.join(self.expected)) |
| 53 | + |
| 54 | + def test_walk_files(self): |
| 55 | + """walk_files should traverse files in alphabetical order""" |
| 56 | + for i, path in enumerate(dataset_utils.walk_files(self.root, '.txt', prefix=True)): |
| 57 | + found = os.path.join(self.root, path) |
| 58 | + assert found == self.expected[i] |
| 59 | + |
| 60 | + |
25 | 61 | class TestDatasets(TorchaudioTestCase): |
26 | 62 | backend = 'default' |
27 | 63 | path = get_asset_path() |
|
0 commit comments