def test_empty_backup_ext(when): with pytest.raises(ValueError) as excinfo: add_line_to_file( "nonexistent.txt", "gnusto=cleesh", backup_ext="", backup=when, ) assert str(excinfo.value) == "Cannot use empty string as backup_ext"
def test_add_line_to_file_encoding_errors_backup(mocker, tmp_path): thefile = tmp_path / "file.txt" thefile.write_text( "edh=\xF0\n" "a-tilde-degrees=\xC3\xB0\n", encoding="latin-1", ) add_line_str_spy = mocker.spy(lineinfile, "add_line_to_string") assert add_line_to_file( thefile, "edh=\xF0", encoding="utf-8", errors="surrogateescape", backup=CHANGED, ) add_line_str_spy.assert_called_once_with( "edh=\uDCF0\n" "a-tilde-degrees=\xF0\n", "edh=\xF0", **STRING_DEFAULTS, ) assert listdir(tmp_path) == ["file.txt", "file.txt~"] assert thefile.read_text( encoding="latin-1") == ("edh=\xF0\n" "a-tilde-degrees=\xC3\xB0\n" "edh=\xC3\xB0\n") assert thefile.with_name(thefile.name + "~").read_text( encoding="latin-1") == ("edh=\xF0\n" "a-tilde-degrees=\xC3\xB0\n")
def test_create_file_no_change(tmp_path): thefile = tmp_path / "file.txt" assert not add_line_to_file( thefile, r"\1=cleesh", regexp=r"^(\w+)=", backrefs=True, create=True, ) assert listdir(tmp_path) == []
def test_file_line_replaces_self(tmp_path): thefile = tmp_path / "file.txt" thefile.write_text(INPUT) assert not add_line_to_file( thefile, "bar=quux\n", regexp=r"^bar=", backup=CHANGED, ) assert listdir(tmp_path) == ["file.txt"] assert thefile.read_text() == INPUT
def test_add_line_to_file_encoding(mocker, tmp_path): thefile = tmp_path / "file.txt" thefile.write_text(INPUT, encoding="utf-16") add_line_str_spy = mocker.spy(lineinfile, "add_line_to_string") assert add_line_to_file(thefile, "gnusto=cleesh", encoding="utf-16") add_line_str_spy.assert_called_once_with( INPUT, "gnusto=cleesh", **STRING_DEFAULTS, ) assert listdir(tmp_path) == ["file.txt"] assert thefile.read_text(encoding="utf-16") == INPUT + "gnusto=cleesh\n"
def test_backup_file_exists(tmp_path, when): thefile = tmp_path / "file.txt" thefile.write_text(INPUT) (tmp_path / "file.txt.bak").write_text("This will be replaced.\n") assert add_line_to_file( thefile, "gnusto=cleesh", backup=when, backup_ext=".bak", ) assert listdir(tmp_path) == ["file.txt", "file.txt.bak"] assert (tmp_path / "file.txt.bak").read_text() == INPUT assert thefile.read_text() == INPUT + "gnusto=cleesh\n"
def add_pyversion(self, v: str) -> None: pyv = PyVersion.parse(v) if pyv in self.details.python_versions: log.info("Project already supports %s; not adding", pyv) return if str(pyv) not in SpecifierSet(self.details.python_requires): raise ValueError(f"Version {pyv} does not match python_requires =" f" {self.details.python_requires!r}") log.info("Adding %s to supported Python versions", pyv) log.info("Updating setup.cfg ...") add_line_to_file( self.directory / "setup.cfg", f" Programming Language :: Python :: {pyv}\n", inserter=AfterLast( r"^ Programming Language :: Python :: \d+\.\d+$"), encoding="utf-8", ) if self.details.has_tests: log.info("Updating tox.ini ...") map_lines( self.directory / "tox.ini", partial( replace_group, re.compile(r"^envlist\s*=[ \t]*(.+)$", flags=re.M), partial(add_py_env, pyv), ), ) if self.details.has_ci: log.info("Updating .github/workflows/test.yml ...") add_line_to_file( self.directory / ".github" / "workflows" / "test.yml", f"{' ' * 10}- '{pyv}'\n", inserter=AfterLast(fr"^{' ' * 10}- ['\x22]?\d+\.\d+['\x22]?$"), encoding="utf-8", ) insort(self.details.python_versions, pyv)
def test_backup_symlink_no_change(tmp_path): thefile = tmp_path / "file.txt" thefile.write_text(INPUT) linkfile = tmp_path / "link.txt" linkfile.symlink_to(thefile) assert not add_line_to_file( linkfile, "foo=apple", backup=ALWAYS, backup_ext=".bak", ) assert listdir(tmp_path) == ["file.txt", "link.txt", "link.txt.bak"] assert linkfile.is_symlink() assert not (tmp_path / "link.txt.bak").is_symlink() assert (tmp_path / "link.txt.bak").read_text() == INPUT assert thefile.read_text() == INPUT
def test_backup_symlink(tmp_path, when): thefile = tmp_path / "file.txt" thefile.write_text(INPUT) linkfile = tmp_path / "link.txt" linkfile.symlink_to(thefile) assert add_line_to_file( linkfile, "gnusto=cleesh", backup=when, backup_ext=".bak", ) assert listdir(tmp_path) == ["file.txt", "link.txt", "link.txt.bak"] assert linkfile.is_symlink() assert not (tmp_path / "link.txt.bak").is_symlink() assert (tmp_path / "link.txt.bak").read_text() == INPUT assert thefile.read_text() == INPUT + "gnusto=cleesh\n"
def test_create_file_not_exists_backup(tmp_path, when): thefile = tmp_path / "file.txt" assert add_line_to_file(thefile, "gnusto=cleesh", create=True, backup=when) assert listdir(tmp_path) == ["file.txt"] assert thefile.read_text() == "gnusto=cleesh\n"
def test_no_create_file_not_exists(tmp_path): thefile = tmp_path / "file.txt" with pytest.raises(FileNotFoundError): add_line_to_file(thefile, "gnusto=cleesh") assert listdir(tmp_path) == []
def test_create_file_exists(tmp_path, create): thefile = tmp_path / "file.txt" thefile.write_text(INPUT) assert add_line_to_file(thefile, "gnusto=cleesh", create=create) assert listdir(tmp_path) == ["file.txt"] assert thefile.read_text() == INPUT + "gnusto=cleesh\n"