|
1 | 1 | import pathlib
|
2 | 2 | import shutil
|
3 | 3 | import subprocess
|
4 |
| -import unittest |
| 4 | + |
| 5 | +import pytest |
5 | 6 |
|
6 | 7 | from tests import helpers
|
7 | 8 |
|
8 | 9 | TMP = pathlib.Path(__file__).parent.joinpath('tmp')
|
9 | 10 |
|
10 | 11 |
|
11 |
| -class TestApp(unittest.TestCase): |
12 |
| - |
13 |
| - def setUp(self): |
14 |
| - try: |
15 |
| - shutil.rmtree(TMP) |
16 |
| - except FileNotFoundError: |
17 |
| - pass |
18 |
| - TMP.mkdir() |
19 |
| - |
20 |
| - def test_copies_other_file_type(self): |
21 |
| - source_dir = TMP / 'source' |
22 |
| - source_dir.mkdir() |
23 |
| - target_dir = TMP / 'target' |
24 |
| - text_file = source_dir / 'other.txt' |
25 |
| - text_file.write_text('test file') |
26 |
| - |
27 |
| - proc = subprocess.run( |
28 |
| - ['harmonize', str(source_dir), str(target_dir)], |
29 |
| - stderr=subprocess.PIPE, |
30 |
| - stdout=subprocess.PIPE, |
31 |
| - check=True) |
32 |
| - self.assertEqual(proc.stdout, b'') |
33 |
| - self.assertEqual( |
34 |
| - proc.stderr.decode(), |
35 |
| - (f'Scanning "{source_dir}"\n' |
36 |
| - 'Scanned 1 items\n' |
37 |
| - f'Copying {text_file}\n' |
38 |
| - 'Processing complete\n')) |
39 |
| - |
40 |
| - self.assertEqual( |
41 |
| - text_file.read_text(), |
42 |
| - (target_dir / 'other.txt').read_text()) |
43 |
| - |
44 |
| - def test_transcodes_flac_to_mp3(self): |
45 |
| - source_dir = TMP / 'source' |
46 |
| - source_dir.mkdir() |
47 |
| - target_dir = TMP / 'target' |
48 |
| - audio_file = source_dir / 'audio.flac' |
49 |
| - helpers.ffmpeg.generate_silence(1, audio_file) |
50 |
| - |
51 |
| - proc = subprocess.run( |
52 |
| - ['harmonize', str(source_dir), str(target_dir)], |
53 |
| - stderr=subprocess.PIPE, |
54 |
| - stdout=subprocess.PIPE, |
55 |
| - check=True) |
56 |
| - self.assertEqual(proc.stdout, b'') |
57 |
| - self.assertEqual( |
58 |
| - proc.stderr.decode(), |
59 |
| - (f'Scanning "{source_dir}"\n' |
60 |
| - 'Scanned 1 items\n' |
61 |
| - f'Transcoding {audio_file}\n' |
62 |
| - 'Processing complete\n')) |
63 |
| - |
64 |
| - metadata = helpers.ffprobe.get_metadata(target_dir / 'audio.mp3') |
65 |
| - |
66 |
| - self.assertEqual('mp3', metadata['format']['format_name']) |
67 |
| - self.assertEqual(1, len(metadata['streams'])) |
68 |
| - self.assertEqual('mp3', metadata['streams'][0]['codec_name']) |
69 |
| - # mp3 will not be exact duration as input |
70 |
| - self.assertTrue(1 <= float(metadata['format']['duration']) <= 1.1) |
71 |
| - |
72 |
| - def test_transcodes_flac_to_opus(self): |
73 |
| - source_dir = TMP / 'source' |
74 |
| - source_dir.mkdir() |
75 |
| - target_dir = TMP / 'target' |
76 |
| - helpers.ffmpeg.generate_silence(1, source_dir / 'audio.flac') |
77 |
| - |
78 |
| - proc = subprocess.run( |
79 |
| - ['harmonize', '--codec', 'opus', str(source_dir), str(target_dir)], |
80 |
| - stderr=subprocess.PIPE, |
81 |
| - stdout=subprocess.PIPE, |
82 |
| - check=True) |
83 |
| - self.assertEqual(proc.stdout, b'') |
84 |
| - self.assertEqual( |
85 |
| - proc.stderr.decode(), |
86 |
| - (f'Scanning "{source_dir}"\n' |
87 |
| - 'Scanned 1 items\n' |
88 |
| - f'Transcoding {source_dir}/audio.flac\n' |
89 |
| - 'Processing complete\n')) |
90 |
| - |
91 |
| - metadata = helpers.ffprobe.get_metadata(target_dir / 'audio.opus') |
92 |
| - |
93 |
| - self.assertEqual('ogg', metadata['format']['format_name']) |
94 |
| - self.assertEqual(1, len(metadata['streams'])) |
95 |
| - self.assertEqual('opus', metadata['streams'][0]['codec_name']) |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def setup_tmp_dir(): |
| 14 | + try: |
| 15 | + shutil.rmtree(TMP) |
| 16 | + except FileNotFoundError: |
| 17 | + pass |
| 18 | + TMP.mkdir() |
| 19 | + yield |
| 20 | + try: |
| 21 | + shutil.rmtree(TMP) |
| 22 | + except FileNotFoundError: |
| 23 | + pass |
| 24 | + |
| 25 | +def test_copies_other_file_type(): |
| 26 | + source_dir = TMP / 'source' |
| 27 | + source_dir.mkdir() |
| 28 | + target_dir = TMP / 'target' |
| 29 | + text_file = source_dir / 'other.txt' |
| 30 | + text_file.write_text('test file') |
| 31 | + |
| 32 | + proc = subprocess.run( |
| 33 | + ['harmonize', str(source_dir), str(target_dir)], |
| 34 | + stderr=subprocess.PIPE, |
| 35 | + stdout=subprocess.PIPE, |
| 36 | + check=True) |
| 37 | + assert proc.stdout == b'' |
| 38 | + assert proc.stderr.decode() == ( |
| 39 | + f'Scanning "{source_dir}"\n' |
| 40 | + 'Scanned 1 items\n' |
| 41 | + f'Copying {text_file}\n' |
| 42 | + 'Processing complete\n') |
| 43 | + |
| 44 | + assert text_file.read_text() == (target_dir / 'other.txt').read_text() |
| 45 | + |
| 46 | +def test_transcodes_flac_to_mp3(): |
| 47 | + source_dir = TMP / 'source' |
| 48 | + source_dir.mkdir() |
| 49 | + target_dir = TMP / 'target' |
| 50 | + audio_file = source_dir / 'audio.flac' |
| 51 | + helpers.ffmpeg.generate_silence(1, audio_file) |
| 52 | + |
| 53 | + proc = subprocess.run( |
| 54 | + ['harmonize', str(source_dir), str(target_dir)], |
| 55 | + stderr=subprocess.PIPE, |
| 56 | + stdout=subprocess.PIPE, |
| 57 | + check=True) |
| 58 | + assert proc.stdout == b'' |
| 59 | + assert proc.stderr.decode() == ( |
| 60 | + f'Scanning "{source_dir}"\n' |
| 61 | + 'Scanned 1 items\n' |
| 62 | + f'Transcoding {audio_file}\n' |
| 63 | + 'Processing complete\n') |
| 64 | + |
| 65 | + metadata = helpers.ffprobe.get_metadata(target_dir / 'audio.mp3') |
| 66 | + |
| 67 | + assert metadata['format']['format_name'] == 'mp3' |
| 68 | + assert len(metadata['streams']) == 1 |
| 69 | + assert metadata['streams'][0]['codec_name'] == 'mp3' |
| 70 | + # mp3 will not be exact duration as input |
| 71 | + assert 1 <= float(metadata['format']['duration']) <= 1.1 |
| 72 | + |
| 73 | +def test_transcodes_flac_to_opus(): |
| 74 | + source_dir = TMP / 'source' |
| 75 | + source_dir.mkdir() |
| 76 | + target_dir = TMP / 'target' |
| 77 | + helpers.ffmpeg.generate_silence(1, source_dir / 'audio.flac') |
| 78 | + |
| 79 | + proc = subprocess.run( |
| 80 | + ['harmonize', '--codec', 'opus', str(source_dir), str(target_dir)], |
| 81 | + stderr=subprocess.PIPE, |
| 82 | + stdout=subprocess.PIPE, |
| 83 | + check=True) |
| 84 | + assert proc.stdout == b'' |
| 85 | + assert proc.stderr.decode() == ( |
| 86 | + f'Scanning "{source_dir}"\n' |
| 87 | + 'Scanned 1 items\n' |
| 88 | + f'Transcoding {source_dir}/audio.flac\n' |
| 89 | + 'Processing complete\n') |
| 90 | + |
| 91 | + metadata = helpers.ffprobe.get_metadata(target_dir / 'audio.opus') |
| 92 | + |
| 93 | + assert metadata['format']['format_name'] == 'ogg' |
| 94 | + assert len(metadata['streams']) == 1 |
| 95 | + assert metadata['streams'][0]['codec_name'] == 'opus' |
| 96 | + # mp3 will not be exact duration as input |
| 97 | + assert 1 <= float(metadata['format']['duration']) <= 1.1 |
| 98 | + |
| 99 | +def test_multiple_mixed_audio_and_other_files(): |
| 100 | + source_dir = TMP / 'source' |
| 101 | + source_dir.mkdir() |
| 102 | + target_dir = TMP / 'target' |
| 103 | + |
| 104 | + text_file = source_dir / 'other.txt' |
| 105 | + text_file.write_text('test file') |
| 106 | + |
| 107 | + for duration in range(1, 4): |
| 108 | + helpers.ffmpeg.generate_silence( |
| 109 | + duration, source_dir / f'{duration}.flac') |
| 110 | + |
| 111 | + proc = subprocess.run( |
| 112 | + ['harmonize', str(source_dir), str(target_dir)], |
| 113 | + stderr=subprocess.PIPE, |
| 114 | + stdout=subprocess.PIPE, |
| 115 | + check=True) |
| 116 | + assert proc.stdout == b'' |
| 117 | + |
| 118 | + stderr = proc.stderr.decode().splitlines() |
| 119 | + assert stderr[0:2] == [ |
| 120 | + f'Scanning "{source_dir}"', |
| 121 | + 'Scanned 4 items'] |
| 122 | + assert sorted(stderr[2:6]) == [ |
| 123 | + f'Copying {source_dir}/other.txt', |
| 124 | + f'Transcoding {source_dir}/1.flac', |
| 125 | + f'Transcoding {source_dir}/2.flac', |
| 126 | + f'Transcoding {source_dir}/3.flac'] |
| 127 | + assert stderr[6] == 'Processing complete' |
| 128 | + |
| 129 | + for duration in range(1, 4): |
| 130 | + metadata = helpers.ffprobe.get_metadata( |
| 131 | + target_dir / f'{duration}.mp3') |
| 132 | + |
| 133 | + assert metadata['format']['format_name'] == 'mp3' |
| 134 | + assert len(metadata['streams']) == 1 |
| 135 | + assert metadata['streams'][0]['codec_name'] == 'mp3' |
96 | 136 | # mp3 will not be exact duration as input
|
97 |
| - self.assertTrue(1 <= float(metadata['format']['duration']) <= 1.1) |
98 |
| - |
99 |
| - def test_multiple_mixed_audio_and_other_files(self): |
100 |
| - source_dir = TMP / 'source' |
101 |
| - source_dir.mkdir() |
102 |
| - target_dir = TMP / 'target' |
103 |
| - |
104 |
| - text_file = source_dir / 'other.txt' |
105 |
| - text_file.write_text('test file') |
106 |
| - |
107 |
| - for duration in range(1, 4): |
108 |
| - helpers.ffmpeg.generate_silence( |
109 |
| - duration, source_dir / f'{duration}.flac') |
110 |
| - |
111 |
| - proc = subprocess.run( |
112 |
| - ['harmonize', str(source_dir), str(target_dir)], |
113 |
| - stderr=subprocess.PIPE, |
114 |
| - stdout=subprocess.PIPE, |
115 |
| - check=True) |
116 |
| - self.assertEqual(proc.stdout, b'') |
117 |
| - |
118 |
| - stderr = proc.stderr.decode().splitlines() |
119 |
| - self.assertEqual( |
120 |
| - stderr[0:2], |
121 |
| - [f'Scanning "{source_dir}"', |
122 |
| - 'Scanned 4 items']) |
123 |
| - self.assertEqual( |
124 |
| - sorted(stderr[2:6]), |
125 |
| - [f'Copying {source_dir}/other.txt', |
126 |
| - f'Transcoding {source_dir}/1.flac', |
127 |
| - f'Transcoding {source_dir}/2.flac', |
128 |
| - f'Transcoding {source_dir}/3.flac']) |
129 |
| - self.assertEqual( |
130 |
| - stderr[6], |
131 |
| - 'Processing complete') |
132 |
| - |
133 |
| - for duration in range(1, 4): |
134 |
| - metadata = helpers.ffprobe.get_metadata( |
135 |
| - target_dir / f'{duration}.mp3') |
136 |
| - |
137 |
| - self.assertEqual('mp3', metadata['format']['format_name']) |
138 |
| - self.assertEqual(1, len(metadata['streams'])) |
139 |
| - self.assertEqual('mp3', metadata['streams'][0]['codec_name']) |
140 |
| - # mp3 will not be exact duration as input |
141 |
| - self.assertTrue(duration <= float(metadata['format']['duration']) <= duration + 0.1) # noqa: E501 |
142 |
| - |
143 |
| - self.assertEqual( |
144 |
| - text_file.read_text(), |
145 |
| - (target_dir / 'other.txt').read_text()) |
| 137 | + assert duration <= float(metadata['format']['duration']) <= duration + 0.1 |
| 138 | + |
| 139 | + assert text_file.read_text() == (target_dir / 'other.txt').read_text() |
0 commit comments