Black formatting
This commit is contained in:
@@ -24,7 +24,9 @@ def write_file(path: Path, content: str):
|
|||||||
path.write_text(content, encoding="utf-8")
|
path.write_text(content, encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
def backup_original(original_path: Path, original_content: str, backup_dir: Path) -> Path:
|
def backup_original(
|
||||||
|
original_path: Path, original_content: str, backup_dir: Path
|
||||||
|
) -> Path:
|
||||||
backup_dir.mkdir(parents=True, exist_ok=True)
|
backup_dir.mkdir(parents=True, exist_ok=True)
|
||||||
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
|
timestamp = time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime())
|
||||||
shortstamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime())
|
shortstamp = time.strftime("%Y%m%dT%H%M%S", time.gmtime())
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ import mirro.main as mirro
|
|||||||
# get_version
|
# get_version
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_get_version_found(monkeypatch):
|
def test_get_version_found(monkeypatch):
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(mirro.importlib.metadata, "version", lambda _: "1.2.3")
|
||||||
mirro.importlib.metadata, "version", lambda _: "1.2.3"
|
|
||||||
)
|
|
||||||
assert mirro.get_version() == "1.2.3"
|
assert mirro.get_version() == "1.2.3"
|
||||||
|
|
||||||
|
|
||||||
def test_get_version_not_found(monkeypatch):
|
def test_get_version_not_found(monkeypatch):
|
||||||
def raiser(_):
|
def raiser(_):
|
||||||
raise mirro.importlib.metadata.PackageNotFoundError
|
raise mirro.importlib.metadata.PackageNotFoundError
|
||||||
|
|
||||||
monkeypatch.setattr(mirro.importlib.metadata, "version", raiser)
|
monkeypatch.setattr(mirro.importlib.metadata, "version", raiser)
|
||||||
assert mirro.get_version() == "unknown"
|
assert mirro.get_version() == "unknown"
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ def test_get_version_not_found(monkeypatch):
|
|||||||
# read_file / write_file
|
# read_file / write_file
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_read_file_exists(tmp_path):
|
def test_read_file_exists(tmp_path):
|
||||||
p = tmp_path / "x.txt"
|
p = tmp_path / "x.txt"
|
||||||
p.write_text("hello\n", encoding="utf-8")
|
p.write_text("hello\n", encoding="utf-8")
|
||||||
@@ -51,20 +52,23 @@ def test_write_file(tmp_path):
|
|||||||
# backup_original
|
# backup_original
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_backup_original(tmp_path, monkeypatch):
|
def test_backup_original(tmp_path, monkeypatch):
|
||||||
original_path = tmp_path / "test.txt"
|
original_path = tmp_path / "test.txt"
|
||||||
original_content = "ABC"
|
original_content = "ABC"
|
||||||
backup_dir = tmp_path / "backups"
|
backup_dir = tmp_path / "backups"
|
||||||
|
|
||||||
# Freeze timestamps
|
# Freeze timestamps
|
||||||
monkeypatch.setattr(time, "gmtime", lambda: time.struct_time((2023,1,2,3,4,5,0,0,0)))
|
monkeypatch.setattr(
|
||||||
|
time, "gmtime", lambda: time.struct_time((2023, 1, 2, 3, 4, 5, 0, 0, 0))
|
||||||
|
)
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
time,
|
time,
|
||||||
"strftime",
|
"strftime",
|
||||||
lambda fmt, _: {
|
lambda fmt, _: {
|
||||||
"%Y-%m-%d %H:%M:%S UTC": "2023-01-02 03:04:05 UTC",
|
"%Y-%m-%d %H:%M:%S UTC": "2023-01-02 03:04:05 UTC",
|
||||||
"%Y%m%dT%H%M%S": "20230102T030405",
|
"%Y%m%dT%H%M%S": "20230102T030405",
|
||||||
}[fmt]
|
}[fmt],
|
||||||
)
|
)
|
||||||
|
|
||||||
backup_path = mirro.backup_original(original_path, original_content, backup_dir)
|
backup_path = mirro.backup_original(original_path, original_content, backup_dir)
|
||||||
@@ -80,6 +84,7 @@ def test_backup_original(tmp_path, monkeypatch):
|
|||||||
# Helper to run main()
|
# Helper to run main()
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def simulate_main(
|
def simulate_main(
|
||||||
monkeypatch,
|
monkeypatch,
|
||||||
capsys,
|
capsys,
|
||||||
@@ -129,6 +134,7 @@ def simulate_main(
|
|||||||
# main: missing file argument
|
# main: missing file argument
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_missing_argument(capsys):
|
def test_main_missing_argument(capsys):
|
||||||
with patch("sys.argv", ["mirro"]):
|
with patch("sys.argv", ["mirro"]):
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
@@ -141,6 +147,7 @@ def test_main_missing_argument(capsys):
|
|||||||
# main: unchanged file (line 137)
|
# main: unchanged file (line 137)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_existing_unchanged(tmp_path, monkeypatch, capsys):
|
def test_main_existing_unchanged(tmp_path, monkeypatch, capsys):
|
||||||
target = tmp_path / "file.txt"
|
target = tmp_path / "file.txt"
|
||||||
target.write_text("hello\n", encoding="utf-8")
|
target.write_text("hello\n", encoding="utf-8")
|
||||||
@@ -164,6 +171,7 @@ def test_main_existing_unchanged(tmp_path, monkeypatch, capsys):
|
|||||||
# main: changed file
|
# main: changed file
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_existing_changed(tmp_path, monkeypatch, capsys):
|
def test_main_existing_changed(tmp_path, monkeypatch, capsys):
|
||||||
target = tmp_path / "file2.txt"
|
target = tmp_path / "file2.txt"
|
||||||
|
|
||||||
@@ -184,6 +192,7 @@ def test_main_existing_changed(tmp_path, monkeypatch, capsys):
|
|||||||
# main: new file unchanged
|
# main: new file unchanged
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_new_file_unchanged(tmp_path, monkeypatch, capsys):
|
def test_main_new_file_unchanged(tmp_path, monkeypatch, capsys):
|
||||||
new = tmp_path / "new.txt"
|
new = tmp_path / "new.txt"
|
||||||
|
|
||||||
@@ -204,6 +213,7 @@ def test_main_new_file_unchanged(tmp_path, monkeypatch, capsys):
|
|||||||
# main: new file changed
|
# main: new file changed
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_new_file_changed(tmp_path, monkeypatch, capsys):
|
def test_main_new_file_changed(tmp_path, monkeypatch, capsys):
|
||||||
new = tmp_path / "new2.txt"
|
new = tmp_path / "new2.txt"
|
||||||
|
|
||||||
@@ -224,6 +234,7 @@ def test_main_new_file_changed(tmp_path, monkeypatch, capsys):
|
|||||||
# main: permission denied for existing file (line 78)
|
# main: permission denied for existing file (line 78)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_permission_denied_existing(tmp_path, monkeypatch, capsys):
|
def test_main_permission_denied_existing(tmp_path, monkeypatch, capsys):
|
||||||
target = tmp_path / "blocked.txt"
|
target = tmp_path / "blocked.txt"
|
||||||
target.write_text("hello", encoding="utf-8")
|
target.write_text("hello", encoding="utf-8")
|
||||||
@@ -243,6 +254,7 @@ def test_main_permission_denied_existing(tmp_path, monkeypatch, capsys):
|
|||||||
# main: permission denied creating file (line 84)
|
# main: permission denied creating file (line 84)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_permission_denied_create(tmp_path, monkeypatch, capsys):
|
def test_main_permission_denied_create(tmp_path, monkeypatch, capsys):
|
||||||
newfile = tmp_path / "subdir" / "nofile.txt"
|
newfile = tmp_path / "subdir" / "nofile.txt"
|
||||||
parent = newfile.parent
|
parent = newfile.parent
|
||||||
@@ -269,6 +281,7 @@ def test_main_permission_denied_create(tmp_path, monkeypatch, capsys):
|
|||||||
# main: non-nano editor (ordering branch)
|
# main: non-nano editor (ordering branch)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
def test_main_editor_non_nano(tmp_path, monkeypatch, capsys):
|
def test_main_editor_non_nano(tmp_path, monkeypatch, capsys):
|
||||||
target = tmp_path / "vim.txt"
|
target = tmp_path / "vim.txt"
|
||||||
target.write_text("old\n", encoding="utf-8")
|
target.write_text("old\n", encoding="utf-8")
|
||||||
|
|||||||
Reference in New Issue
Block a user