"""The files plugin's reads, edits, searches and listings in a real kernel. The defects: a read that splits or miscounts lines across chunk boundaries and separators, budgets and limits applied in the wrong order, an edit that picks an ambiguous match or applies part of a list, a write that saves a read listing, a search that hides what it skipped, raises on a clipped result, or reads a ripgrep colour config. The plugin runs in a kernel process with its jobs, which is all of it a daemon would add. """ from pathlib import Path import sys import tempfile import unittest sys.path.insert(0, str(Path(__file__).resolve().parent)) from job_wake_test import Owner # noqa: E402 class FilesPluginTests(unittest.TestCase): def setUp(self): self.cells = 0 self.owner = self.start() def start(self, env=None) -> Owner: """A kernel with the files plugin, in a fresh workspace.""" owner = Owner(["files", "run"], env) self.addCleanup(owner.close) owner.wait_for(lambda f: f.get("type") == "ready") self.workspace = Path(owner.workspace) return owner def execute(self, code): """What the cell printed; it must succeed.""" self.cells += 1 id = f"c{self.cells}" self.owner.send({"type": "execute", "id": id, "code": code}) done = self.owner.wait_for( lambda f: f.get("type") == "done" and f.get("id") == id, timeout=60 ) self.assertEqual(done["status"], "ok", done) return done["output"] def test_a_clipped_search_returns_truncated_rows(self): """A search whose output passes the capture limit answers the rows it kept and says it was truncated, instead of raising.""" output = self.execute( "from pathlib import Path\n" "root = Path('search-fixture'); root.mkdir()\n" "for i in range(20): (root / f'file-{i}').write_text('pinned ' + 'x' * 100000)\n" "rows = await files.find(r'pinned|Pinned', str(root), max_results=30)\n" "print(len(rows), rows.truncated)" ) self.assertRegex(output, r"^\d+ (True|False)$") def test_ambiguous_edit_requires_a_hint_and_preserves_other_matches(self): note = self.workspace / "note.txt" note.write_text("same\nother\nsame\n") output = self.execute( "try:\n files.edit('note.txt', 'same', 'changed')\n" "except Exception as exc:\n print('ambiguous:', exc)\n" "files.edit('note.txt', 'same', 'changed', line_hint=3)\n" "print(files.read('note.txt'))" ) self.assertEqual(note.read_text(), "same\nother\nchanged\n") self.assertIn("ambiguous:", output) self.assertIn("same", output) self.assertIn("changed", output) def test_a_list_of_edits_applies_all_or_none(self): note = self.workspace / "note.txt" note.write_text("alpha\nbeta\ngamma\n") output = self.execute( "print(files.edit('note.txt', [('alpha', 'one'), ('gamma', 'three')]))\n" "try:\n" " files.edit('note.txt', [('one', 'uno'), ('absent', 'x')])\n" "except ValueError as error:\n" " print(str(error).splitlines()[0])" ) self.assertEqual(note.read_text(), "one\nbeta\nthree\n") lines = output.splitlines() self.assertIn("2 replacements", lines[0]) self.assertIn( "edit 2 of 2 failed, so none were made: string not found", lines[1] ) def test_an_edit_miss_says_when_only_whitespace_differs(self): (self.workspace / "note.txt").write_text("if ok:\n run()\n") output = self.execute( "for old in ('if ok:\\n\\trun()', 'absent'):\n" " try:\n files.edit('note.txt', old, 'x')\n" " except ValueError as error:\n print(str(error).splitlines()[0])" ) lines = output.splitlines() self.assertIn("whitespace differs", lines[0]) self.assertNotIn("whitespace differs", lines[1]) def test_find_searches_workspace_and_returns_context(self): (self.workspace / "note.txt").write_text("before\nneedle\nafter\n") output = self.execute( "rows = await files.find('needle', '.', context=1)\n" "print(rows)\nprint(files.read('note.txt', start_line=2, end_line=2))" ) self.assertIn("note.txt", output) self.assertIn("needle", output) self.assertIn("before", output) self.assertIn("after", output) def test_an_empty_find_says_what_was_searched_and_skipped(self): (self.workspace / ".ignore").write_text("ignored.txt\n") (self.workspace / "ignored.txt").write_text("needle\n") (self.workspace / ".dot.txt").write_text("needle\n") (self.workspace / "plain.txt").write_text("nothing\n") output = self.execute( "print(await files.find('needle', '.'))\n" "print(await files.find('needle', '.', hidden=True, ignored=True))" ) self.assertIn("no matches for 'needle'", output) self.assertIn("1 files searched", output) self.assertIn("skipped hidden files, .gitignore/.ignore rules", output) self.assertIn("ignored.txt", output) self.assertIn("rerun with hidden=True and/or ignored=True", output) self.assertEqual(output.count("no matches"), 1, output) def test_find_in_a_missing_root_raises_instead_of_returning_nothing(self): output = self.execute( "try:\n await files.find('needle', 'nowhere')\n" "except FileNotFoundError as exc:\n print('missing:', exc)" ) self.assertIn("missing: nowhere not found", output) def test_cat_round_trips_text_and_write_refuses_a_read_listing(self): (self.workspace / "note.txt").write_bytes(b"a\r\n\nb\n") (self.workspace / "latin.txt").write_bytes(b"caf\xe9\n") output = self.execute( "text = await files.cat('note.txt')\n" "assert text == 'a\\r\\n\\nb\\n', repr(text)\n" "files.write('note.txt', text.replace('b', 'c'))\n" "listing = files.read('note.txt')\n" "for content in (listing, listing.replace('c', 'd') + '\\nextra'):\n" " try:\n files.write('note.txt', content)\n" " except ValueError as error:\n print('refused:', error)\n" "files.write('table.md', '1 | a\\n2 | b\\n')\n" "try:\n files.cat('latin.txt')\n" "except ValueError as error:\n print('cat:', error)" ) self.assertEqual(output.count("refused: content is a files.read listing"), 2) self.assertIn("numbered lines 1-3", output) self.assertIn("cat: latin.txt is not UTF-8", output) self.assertEqual((self.workspace / "note.txt").read_bytes(), b"a\r\n\nc\n") self.assertEqual((self.workspace / "table.md").read_text(), "1 | a\n2 | b\n") def test_a_file_result_is_awaitable_and_offers_content_and_text(self): (self.workspace / "note.txt").write_text("hello\n") output = self.execute( "text = await files.read('note.txt')\n" "print('awaited:', text.strip())\n" "print('content:', (await files.read('note.txt')).content.strip())\n" "print('text:', files.read('note.txt').text.strip())" ) self.assertIn("awaited: 1 | hello", output) self.assertIn("content: 1 | hello", output) self.assertIn("text: 1 | hello", output) def test_paths_searches_a_list_of_directories_with_a_list_of_globs(self): for directory, name in [("a", "one.md"), ("b", "two.txt"), ("c", "three.md")]: (self.workspace / directory).mkdir() (self.workspace / directory / name).write_text("x") output = self.execute( "rows = await files.paths(None, ['a', 'b'], glob=['*.md', '*.txt'])\n" "print(sorted(str(row) for row in rows))" ) self.assertIn("one.md", output) self.assertIn("two.txt", output) self.assertNotIn("three.md", output) def test_paths_globs_a_path_from_the_workspace_root(self): (self.workspace / "src" / "deep").mkdir(parents=True) (self.workspace / "src" / "deep" / "stream_test.go").write_text("x") output = self.execute( "print([str(row) for row in await files.paths('src/*/*_test.go')])\n" "print([str(row) for row in await files.paths('./src/*/*_test.go')])" ) self.assertEqual(output.count("['src/deep/stream_test.go']"), 2, output) def test_paths_globs_with_color_enabled_in_ripgrep_config(self): config = Path(tempfile.mkdtemp(prefix="albedo-rg-")) / "ripgrep.conf" config.write_text("--pretty\n") self.owner = self.start({"RIPGREP_CONFIG_PATH": str(config)}) (self.workspace / "src" / "deep").mkdir(parents=True) (self.workspace / "src" / "deep" / "stream_test.go").write_text("x") output = self.execute( "print([str(row) for row in await files.paths('src/*/*_test.go')])\n" "print([str(row) for row in await files.paths('./src/*/*_test.go')])" ) self.assertEqual(output.count("['src/deep/stream_test.go']"), 2, output) def test_file_reads_decode_complete_lines_across_chunk_boundaries(self): fixtures = [ (b"", []), (b"\n\n", ["", ""]), (b"a\r\r\n", ["a", ""]), (b"a\n", ["a"]), (b"a\nlast", ["a", "last"]), ( "a\nb\rc\r\nd\ve\ff\x1cg\x1dh\x1ei\x85j\u2028k\u2029last".encode(), list("abcdefghijk") + ["last"], ), (b"a\xffb\xe2\x82", ["a\ufffdb\ufffd"]), ] for padding in (65_534, 65_535, 65_536): prefix = b"x" * padding for separator in ( b"\r\n", "\x85".encode(), "\u2028".encode(), "\u2029".encode(), ): fixtures.append((prefix + separator + b"tail", ["x" * padding, "tail"])) fixtures.append((prefix + "é\n尾".encode(), ["x" * padding + "é", "尾"])) fixtures.append( ( prefix + b"\xe2\x82\nend\xe2\x82", ["x" * padding + "\ufffd", "end\ufffd"], ) ) for index, (data, _lines) in enumerate(fixtures): (self.workspace / f"decode-{index}.txt").write_bytes(data) # Keep large expected lines inside the kernel, out of the tool output. small_expected = [lines for _data, lines in fixtures[:7]] output = self.execute( f"expected = {small_expected!r}\n" "for padding in (65534, 65535, 65536):\n" " expected.extend([['x' * padding, 'tail']] * 4)\n" " expected.append(['x' * padding + 'é', '尾'])\n" " expected.append(['x' * padding + '\ufffd', 'end\ufffd'])\n" "for index, lines in enumerate(expected):\n" " result = files.read(f'decode-{index}.txt', max_chars=200000)\n" " numbered = '\\n'.join(f'{number:>6} | {line}' for number, line in enumerate(lines, 1))\n" " if lines:\n" " assert result == numbered, (index, len(result), len(numbered))\n" " else:\n" " assert ' | ' not in result\n" " assert all(detail in result for detail in ('decode-0.txt', '0 lines', 'line 1'))\n" "print('decoded complete lines')" ) self.assertEqual(output.strip(), "decoded complete lines") def test_file_reads_enforce_budgets_and_character_limit_precedence(self): (self.workspace / "budget.txt").write_text("a\nbb\nc\nd\n") (self.workspace / "numbered.txt").write_text("\n" * 999_999 + "x\nz") output = self.execute( "first = ' 1 | a'\n" "second = ' 2 | bb'\n" "both = first + '\\n' + second\n" "assert files.read('budget.txt', end_line=2, max_chars=23) == both\n" "assert files.read('budget.txt', end_line=1, max_chars=11) == first\n" "rejected = files.read('budget.txt', max_chars=10)\n" "assert ' | ' not in rejected\n" "assert all(detail in rejected for detail in ('line 1', '10 characters', 'max_chars=10', 'start_line=1', 'end_line=1', 'max_chars=11'))\n" "for budget, shown, unshown, resume in ((22, [first], 'lines 2-4', 2), (23, [first, second], 'lines 3-4', 3)):\n" " result = files.read('budget.txt', max_chars=budget).splitlines()\n" " assert result[:-1] == shown\n" " assert all(detail in result[-1] for detail in (f'max_chars={budget}', unshown, f'start_line={resume}'))\n" "result = files.read('budget.txt', limit=2, max_chars=23).splitlines()\n" "assert result[:-1] == [first, second]\n" "assert all(detail in result[-1] for detail in ('limit=2', '2 more through line 4', 'start_line=3'))\n" "result = files.read('budget.txt', limit=3, max_chars=23).splitlines()\n" "assert result[:-1] == [first, second]\n" "assert all(detail in result[-1] for detail in ('max_chars=23', 'lines 3-3', 'start_line=3'))\n" "assert 'limit=' not in result[-1]\n" "assert files.read('numbered.txt', start_line=1000000, end_line=1000000, max_chars=12) == '1000000 | x'\n" "rejected = files.read('numbered.txt', start_line=1000000, end_line=1000000, max_chars=11)\n" "assert ' | ' not in rejected\n" "assert all(detail in rejected for detail in ('line 1000000', '11 characters', 'max_chars=11', 'start_line=1000000', 'end_line=1000000', 'max_chars=12'))\n" "print('budgets and numbering checked')" ) self.assertEqual(output.strip(), "budgets and numbering checked") def test_file_reads_count_requested_windows_and_eof_exactly(self): (self.workspace / "window.txt").write_text("a\nb\nc\nd") pattern = "a\r\nb\rc\nd\ve\ff\x1cg\x1dh\x1ei\x85j\u2028k\u2029last\n".encode() (self.workspace / "bulk.txt").write_bytes( b"head\r\n" + pattern * 10_000 + b"tail\xe2\x82" ) output = self.execute( "assert files.read('window.txt', start_line=2, end_line=3) == ' 2 | b\\n 3 | c'\n" "assert files.read('window.txt', start_line=3, end_line=99) == ' 3 | c\\n 4 | d'\n" "for options, row, remaining, last, resume in (({'start_line': 2}, ' 2 | b', 2, 4, 3), ({'start_line': 2, 'end_line': 3}, ' 2 | b', 1, 3, 3), ({'start_line': 3, 'end_line': 99}, ' 3 | c', 1, 4, 4)):\n" " result = files.read('window.txt', limit=1, **options).splitlines()\n" " assert result[:-1] == [row]\n" " assert all(detail in result[-1] for detail in ('limit=1', f'{remaining} more through line {last}', f'start_line={resume}'))\n" "assert files.read('window.txt', start_line=4, limit=1) == ' 4 | d'\n" "empty = files.read('window.txt', start_line=5, end_line=99)\n" "assert all(detail in empty for detail in ('window.txt', '4 lines', 'line 5'))\n" "assert ' | ' not in empty\n" "result = files.read('bulk.txt', start_line=2, limit=1).splitlines()\n" "assert result[:-1] == [' 2 | a']\n" "assert all(detail in result[-1] for detail in ('limit=1', '120000 more through line 120002', 'start_line=3'))\n" "result = files.read('bulk.txt', start_line=2, end_line=100000, limit=1).splitlines()\n" "assert result[:-1] == [' 2 | a']\n" "assert all(detail in result[-1] for detail in ('limit=1', '99998 more through line 100000', 'start_line=3'))\n" "result = files.read('bulk.txt', start_line=120001, limit=1).splitlines()\n" "assert result[:-1] == ['120001 | last']\n" "assert all(detail in result[-1] for detail in ('limit=1', '1 more through line 120002', 'start_line=120002'))\n" "assert files.read('bulk.txt', start_line=120001, end_line=120001) == '120001 | last'\n" "assert files.read('bulk.txt', start_line=120002) == '120002 | tail\\ufffd'\n" "print('window counts checked')" ) self.assertEqual(output.strip(), "window counts checked") def test_file_reads_count_giant_lines_without_shortening_them(self): (self.workspace / "giant.txt").write_text( "head\n" + "x" * 1_048_576 + "\ntail\n" ) (self.workspace / "unicode.txt").write_text("é" * 110_000) output = self.execute( "assert files.read('giant.txt', start_line=3) == ' 3 | tail'\n" "assert files.read('giant.txt', end_line=1) == ' 1 | head'\n" "rejected = files.read('giant.txt', start_line=2, end_line=2)\n" "assert ' | ' not in rejected\n" "assert all(detail in rejected for detail in ('line 2', '1048585 characters', 'max_chars=16000', '200000'))\n" "assert 'max_chars=1048586' not in rejected\n" "result = files.read('giant.txt', max_chars=32).splitlines()\n" "assert result[:-1] == [' 1 | head']\n" "assert all(detail in result[-1] for detail in ('max_chars=32', 'lines 2-3', 'start_line=2'))\n" "result = files.read('giant.txt', limit=1).splitlines()\n" "assert result[:-1] == [' 1 | head']\n" "assert all(detail in result[-1] for detail in ('limit=1', '2 more through line 3', 'start_line=2'))\n" "for budget in (16000, 110009):\n" " rejected = files.read('unicode.txt', max_chars=budget)\n" " assert ' | ' not in rejected\n" " assert all(detail in rejected for detail in ('line 1', '110009 characters', f'max_chars={budget}', 'start_line=1', 'end_line=1', 'max_chars=110010'))\n" "assert files.read('unicode.txt', max_chars=110010) == ' 1 | ' + 'é' * 110000\n" "print('giant lines counted and returned intact')" ) self.assertEqual(output.strip(), "giant lines counted and returned intact") def test_file_reads_validate_ranges_before_opening_and_report_path_errors(self): output = self.execute( "for options in ({'start_line': 0}, {'start_line': 2, 'end_line': 1}, {'limit': 0}, {'max_chars': 0}, {'max_chars': 200001}):\n" " try:\n" " files.read('absent.txt', **options)\n" " except ValueError:\n" " pass\n" " else:\n" " raise AssertionError(options)\n" "try:\n" " files.read('absent.txt')\n" "except FileNotFoundError as error:\n" " assert 'absent.txt' in str(error)\n" "else:\n" " raise AssertionError('missing file accepted')\n" "try:\n" " files.read('.')\n" "except IsADirectoryError as error:\n" " assert \"files.ls('.')\" in str(error)\n" "else:\n" " raise AssertionError('directory accepted')\n" "print('range and path errors preserved')" ) self.assertEqual(output.strip(), "range and path errors preserved") if __name__ == "__main__": unittest.main()