Test some basic functionality.
(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
)
| 90 | |
| 91 | |
| 92 | def test_basic( |
| 93 | tmp_path: Path, |
| 94 | capsys: pytest.CaptureFixture[str], |
| 95 | ) -> None: |
| 96 | """Test some basic functionality.""" |
| 97 | assert cs.main("_does_not_exist_") == 0 |
| 98 | fname = tmp_path / "tmp" |
| 99 | fname.touch() |
| 100 | result = cs.main("-D", "foo", fname, std=True) |
| 101 | assert isinstance(result, tuple) |
| 102 | code, _, stderr = result |
| 103 | assert code == EX_USAGE, "missing dictionary" |
| 104 | assert "cannot find dictionary" in stderr |
| 105 | assert cs.main(fname) == 0, "empty file" |
| 106 | with fname.open("a") as f: |
| 107 | f.write("this is a test file\n") |
| 108 | assert cs.main(fname) == 0, "good" |
| 109 | with fname.open("a") as f: |
| 110 | f.write("abandonned\n") |
| 111 | assert cs.main(fname) == 1, "bad" |
| 112 | with fname.open("a") as f: |
| 113 | f.write("abandonned\n") |
| 114 | assert cs.main(fname) == 2, "worse" |
| 115 | with fname.open("a") as f: |
| 116 | f.write("tim\ngonna\n") |
| 117 | assert cs.main(fname) == 2, "with a name" |
| 118 | assert cs.main("--builtin", "clear,rare,names,informal", fname) == 4 |
| 119 | with fname.open("w") as f: # overwrite the file |
| 120 | f.write("var = 'nwe must check codespell likes escapes nin strings'\n") |
| 121 | assert cs.main(fname) == 1, "checking our string escape test word is bad" |
| 122 | # the first one is missed because the apostrophe means its not currently |
| 123 | # treated as a word on its own |
| 124 | with fname.open("w") as f: # overwrite the file |
| 125 | f.write("var = '\\nwe must check codespell likes escapes \\nin strings'\n") |
| 126 | assert cs.main(fname) == 0, "with string escape" |
| 127 | result = cs.main(fname, "--builtin", "foo", std=True) |
| 128 | assert isinstance(result, tuple) |
| 129 | code, _, stderr = result |
| 130 | assert code == EX_USAGE # bad type |
| 131 | assert "Unknown builtin dictionary" in stderr |
| 132 | result = cs.main(fname, "-D", tmp_path / "foo", std=True) |
| 133 | assert isinstance(result, tuple) |
| 134 | code, _, stderr = result |
| 135 | assert code == EX_USAGE # bad dict |
| 136 | assert "cannot find dictionary" in stderr |
| 137 | fname.unlink() |
| 138 | |
| 139 | with (tmp_path / "bad.txt").open("w", newline="") as f: |
| 140 | f.write( |
| 141 | "abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd\nabandonned\rAbandonned\r\nABANDONNED \n AbAnDoNnEd" # noqa: E501 |
| 142 | ) |
| 143 | assert cs.main(tmp_path) == 8 |
| 144 | result = cs.main("-w", tmp_path, std=True) |
| 145 | assert isinstance(result, tuple) |
| 146 | code, _, stderr = result |
| 147 | assert code == 0 |
| 148 | assert "FIXED:" in stderr |
| 149 | with (tmp_path / "bad.txt").open(newline="") as f: |