|
| 1 | +import random |
| 2 | +import string |
| 3 | +from dataclasses import dataclass |
| 4 | +from functools import cached_property |
| 5 | +from pathlib import Path |
| 6 | +from string import ascii_lowercase |
| 7 | +from subprocess import run |
| 8 | +from tempfile import TemporaryDirectory, gettempdir |
| 9 | +from typing import Callable |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class FakeFile: |
| 16 | + content: bytes |
| 17 | + counts: tuple[int, ...] |
| 18 | + |
| 19 | + @cached_property |
| 20 | + def path(self) -> Path: |
| 21 | + return Path("-") |
| 22 | + |
| 23 | + def format_line(self, max_digits=None, selected=None): |
| 24 | + if selected is None: |
| 25 | + selected = 8 + 4 + 1 |
| 26 | + numbers = [ |
| 27 | + self.counts[i] for i in range(4) if selected & (2 ** (3 - i)) |
| 28 | + ] |
| 29 | + if max_digits is None: |
| 30 | + max_digits = len(str(max(numbers))) |
| 31 | + counts = " ".join( |
| 32 | + filter(None, [f"{number:{max_digits}}" for number in numbers]) |
| 33 | + ) |
| 34 | + if self.path.name == "-": |
| 35 | + return f"{counts}\n".encode("utf-8") |
| 36 | + else: |
| 37 | + return f"{counts} {self.path}\n".encode("utf-8") |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class TempFile(FakeFile): |
| 42 | + @cached_property |
| 43 | + def path(self) -> Path: |
| 44 | + name = "".join(random.choices(ascii_lowercase, k=10)) |
| 45 | + return Path(gettempdir()) / name |
| 46 | + |
| 47 | + def __post_init__(self): |
| 48 | + self.path.write_bytes(self.content) |
| 49 | + |
| 50 | + def delete(self): |
| 51 | + if self.path.is_dir(): |
| 52 | + self.path.rmdir() |
| 53 | + elif self.path.is_file(): |
| 54 | + self.path.unlink(missing_ok=True) |
| 55 | + |
| 56 | + |
| 57 | +@dataclass(frozen=True) |
| 58 | +class Files: |
| 59 | + files: list[FakeFile] |
| 60 | + |
| 61 | + def __iter__(self): |
| 62 | + return iter(self.files) |
| 63 | + |
| 64 | + def __len__(self): |
| 65 | + return len(self.files) |
| 66 | + |
| 67 | + @cached_property |
| 68 | + def paths(self): |
| 69 | + return [str(file.path) for file in self.files] |
| 70 | + |
| 71 | + @cached_property |
| 72 | + def expected(self): |
| 73 | + if len(self.files) > 1: |
| 74 | + return self.file_lines + self.total_line |
| 75 | + else: |
| 76 | + return self.file_lines |
| 77 | + |
| 78 | + @cached_property |
| 79 | + def file_lines(self): |
| 80 | + return b"".join(file.format_line() for file in self.files) |
| 81 | + |
| 82 | + @cached_property |
| 83 | + def total_line(self): |
| 84 | + totals = [sum(file.counts[i] for file in self.files) for i in range(4)] |
| 85 | + md = len(str(max(totals))) |
| 86 | + return f"{totals[0]:{md}} {totals[1]:{md}} {totals[3]:{md}} total\n".encode( |
| 87 | + "utf-8" |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.fixture(scope="session") |
| 92 | +def small_file(): |
| 93 | + temp_file = TempFile(content=b"caffe\n", counts=(1, 1, 6, 6)) |
| 94 | + try: |
| 95 | + yield temp_file |
| 96 | + finally: |
| 97 | + temp_file.delete() |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture(scope="session") |
| 101 | +def big_file(): |
| 102 | + temp_file = TempFile( |
| 103 | + content=( |
| 104 | + b"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n" |
| 105 | + b"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n" |
| 106 | + b"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n" |
| 107 | + b"consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n" |
| 108 | + b"cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n" |
| 109 | + b"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n" |
| 110 | + ), |
| 111 | + counts=(6, 69, 447, 447), |
| 112 | + ) |
| 113 | + try: |
| 114 | + yield temp_file |
| 115 | + finally: |
| 116 | + temp_file.delete() |
| 117 | + |
| 118 | + |
| 119 | +@pytest.fixture(scope="session") |
| 120 | +def file1(): |
| 121 | + temp_file = TempFile(content=b"caffe latte\n", counts=(1, 2, 12, 12)) |
| 122 | + try: |
| 123 | + yield temp_file |
| 124 | + finally: |
| 125 | + temp_file.delete() |
| 126 | + |
| 127 | + |
| 128 | +@pytest.fixture(scope="session") |
| 129 | +def file2(): |
| 130 | + temp_file = TempFile( |
| 131 | + content=b"Lorem ipsum dolor sit amet\n", counts=(1, 5, 27, 27) |
| 132 | + ) |
| 133 | + try: |
| 134 | + yield temp_file |
| 135 | + finally: |
| 136 | + temp_file.delete() |
| 137 | + |
| 138 | + |
| 139 | +@pytest.fixture(scope="session") |
| 140 | +def unicode_file(): |
| 141 | + temp_file = TempFile( |
| 142 | + content="Zażółć gęślą jaźń\n".encode("utf-8"), counts=(1, 3, 18, 27) |
| 143 | + ) |
| 144 | + try: |
| 145 | + yield temp_file |
| 146 | + finally: |
| 147 | + temp_file.delete() |
| 148 | + |
| 149 | + |
| 150 | +@pytest.fixture(scope="session") |
| 151 | +def small_files(): |
| 152 | + temp_files = [ |
| 153 | + TempFile(content=b"Mocha", counts=(0, 1, 5, 5)), |
| 154 | + TempFile(content=b"Espresso\n", counts=(1, 1, 9, 9)), |
| 155 | + TempFile(content=b"Cappuccino\n", counts=(1, 1, 11, 11)), |
| 156 | + TempFile(content=b"Frappuccino", counts=(0, 1, 11, 11)), |
| 157 | + TempFile(content=b"Flat White\n", counts=(1, 2, 11, 11)), |
| 158 | + TempFile(content=b"Turkish Coffee", counts=(0, 2, 14, 14)), |
| 159 | + TempFile(content=b"Irish Coffee Drink\n", counts=(1, 3, 19, 19)), |
| 160 | + TempFile(content=b"Espresso con Panna", counts=(0, 3, 18, 18)), |
| 161 | + ] |
| 162 | + try: |
| 163 | + yield Files(temp_files) |
| 164 | + finally: |
| 165 | + for file in temp_files: |
| 166 | + file.delete() |
| 167 | + |
| 168 | + |
| 169 | +@pytest.fixture(scope="session") |
| 170 | +def medium_files(file1, file2, unicode_file): |
| 171 | + return Files([file1, file2, unicode_file]) |
| 172 | + |
| 173 | + |
| 174 | +@pytest.fixture(scope="session") |
| 175 | +def wc(): |
| 176 | + def function(*args, stdin: bytes | None = None) -> bytes: |
| 177 | + process = run(["wordcount", *args], capture_output=True, input=stdin) |
| 178 | + return process.stdout |
| 179 | + |
| 180 | + return function |
| 181 | + |
| 182 | + |
| 183 | +@pytest.fixture(scope="session") |
| 184 | +def fake_dir(): |
| 185 | + with TemporaryDirectory(delete=False) as directory: |
| 186 | + path = Path(directory) |
| 187 | + try: |
| 188 | + yield path |
| 189 | + finally: |
| 190 | + path.rmdir() |
| 191 | + |
| 192 | + |
| 193 | +@pytest.fixture(scope="function") |
| 194 | +def random_name(): |
| 195 | + return make_random_name() |
| 196 | + |
| 197 | + |
| 198 | +def make_random_name(length=10): |
| 199 | + return "".join(random.choices(string.ascii_lowercase, k=length)) |
| 200 | + |
| 201 | + |
| 202 | +@pytest.fixture(scope="session") |
| 203 | +def runner(wc, small_file, unicode_file, big_file, fake_dir): |
| 204 | + return Runner( |
| 205 | + wc, small_file, unicode_file, big_file, fake_dir, make_random_name() |
| 206 | + ) |
| 207 | + |
| 208 | + |
| 209 | +@dataclass |
| 210 | +class Runner: |
| 211 | + wc: Callable |
| 212 | + file1: FakeFile |
| 213 | + file2: FakeFile |
| 214 | + file3: FakeFile |
| 215 | + fake_dir: Path |
| 216 | + random_name: str |
| 217 | + |
| 218 | + def __call__(self, *flags): |
| 219 | + return self.wc( |
| 220 | + *flags, |
| 221 | + str(self.file1.path), |
| 222 | + "-", |
| 223 | + str(self.file2.path), |
| 224 | + self.fake_dir, |
| 225 | + "-", |
| 226 | + str(self.file3.path), |
| 227 | + self.random_name, |
| 228 | + stdin=b"flat white", |
| 229 | + ) |
0 commit comments