From 89dcbbc92d0d935fddfe6a8061f7721da88dbcad Mon Sep 17 00:00:00 2001 From: hiijoshi Date: Tue, 24 Mar 2026 22:22:15 +0530 Subject: [PATCH 1/2] Add test for rsync_tmbackup importer, fix backup.marker path bug --- src/borg_import/rsync_tmbackup.py | 2 +- src/borg_import/testsuite/test_borg.py | 5 +-- src/borg_import/testsuite/test_tmbackup.py | 51 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/borg_import/testsuite/test_tmbackup.py diff --git a/src/borg_import/rsync_tmbackup.py b/src/borg_import/rsync_tmbackup.py index a177578..edb9431 100644 --- a/src/borg_import/rsync_tmbackup.py +++ b/src/borg_import/rsync_tmbackup.py @@ -10,7 +10,7 @@ def get_tmbackup_snapshots(root, prefix): """Return metadata for all snapshots discovered in the rsync root directory.""" regex = re.compile(r"(?P.+)") - if not Path("backup.marker").exists(): + if not (root / "backup.marker").exists(): raise FileNotFoundError("The backup.marker file must exist for rsync-time-backup import") for path in discover(str(root), 1): diff --git a/src/borg_import/testsuite/test_borg.py b/src/borg_import/testsuite/test_borg.py index 0c51101..d471fdb 100644 --- a/src/borg_import/testsuite/test_borg.py +++ b/src/borg_import/testsuite/test_borg.py @@ -1,4 +1,5 @@ import subprocess +from pathlib import Path from borg_import.main import main @@ -25,7 +26,6 @@ def test_borg_import(tmpdir, monkeypatch): # Create archives in the source repository subprocess.check_call(["borg", "create", f"{source_repo}::archive1", "."], cwd=str(archive1_data)) - subprocess.check_call(["borg", "create", f"{source_repo}::archive2", "."], cwd=str(archive2_data)) # Initialize the target repository @@ -49,11 +49,10 @@ def test_borg_import(tmpdir, monkeypatch): extract_dir2 = tmpdir.mkdir("extract2") subprocess.check_call(["borg", "extract", f"{target_repo}::archive1"], cwd=str(extract_dir1)) - subprocess.check_call(["borg", "extract", f"{target_repo}::archive2"], cwd=str(extract_dir2)) # Verify the contents of the extracted archives assert extract_dir1.join("file1.txt").read() == "This is file 1 in archive 1" assert extract_dir1.join("file2.txt").read() == "This is file 2 in archive 1" assert extract_dir2.join("file1.txt").read() == "This is file 1 in archive 2" - assert extract_dir2.join("file2.txt").read() == "This is file 2 in archive 2" + assert extract_dir2.join("file2.txt").read() == "This is file 2 in archive 2" \ No newline at end of file diff --git a/src/borg_import/testsuite/test_tmbackup.py b/src/borg_import/testsuite/test_tmbackup.py new file mode 100644 index 0000000..b520fc5 --- /dev/null +++ b/src/borg_import/testsuite/test_tmbackup.py @@ -0,0 +1,51 @@ +import subprocess +from pathlib import Path + +def test_rsync_tmbackup_import(tmpdir, monkeypatch): + """Test importing rsync-time-backup style snapshots into a borg repo.""" + src = Path(str(tmpdir.mkdir("tmbackup"))) + + # backup.marker is required by rsync-time-backup + (src / "backup.marker").touch() + + # two timestamped snapshot directories + snap1 = src / "2023-01-01-120000" + snap1.mkdir() + (snap1 / "file1.txt").write_text("This is file 1 in snap1") + + snap2 = src / "2023-01-02-120000" + snap2.mkdir() + (snap2 / "file1.txt").write_text("This is file 1 in snap2") + + target_repo = Path(str(tmpdir.mkdir("target_repo"))) + + # Initialize the target repository + subprocess.check_call(["borg", "init", "--encryption=none", str(target_repo)]) + + # Run the importer + monkeypatch.setattr( + "sys.argv", + ["borg-import", "rsync_tmbackup", "--prefix", "backup-", str(src), str(target_repo)], + ) + + from borg_import.main import main + main() + + # Verify archives were created + output = subprocess.check_output( + ["borg", "list", "--short", str(target_repo)] + ).decode() + archives = output.splitlines() + + assert len(archives) == 2 + assert any("2023-01-01" in a for a in archives) + assert any("2023-01-02" in a for a in archives) + + # Extract and verify file contents + extract_dir = Path(str(tmpdir.mkdir("extracted"))) + first_archive = f"{target_repo}::{archives[0]}" + subprocess.check_call(["borg", "extract", first_archive], cwd=str(extract_dir)) + + restored = extract_dir / "file1.txt" + assert restored.exists() + assert restored.read_text() == "This is file 1 in snap1" \ No newline at end of file From a6b838d8279a7d3a4c31d849a92223801329f25e Mon Sep 17 00:00:00 2001 From: hiijoshi Date: Tue, 24 Mar 2026 22:29:18 +0530 Subject: [PATCH 2/2] Fix flake8 issues: unused import, blank lines, newlines at end of file --- src/borg_import/testsuite/test_borg.py | 3 +-- src/borg_import/testsuite/test_tmbackup.py | 11 +++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/borg_import/testsuite/test_borg.py b/src/borg_import/testsuite/test_borg.py index d471fdb..a5ac4c3 100644 --- a/src/borg_import/testsuite/test_borg.py +++ b/src/borg_import/testsuite/test_borg.py @@ -1,5 +1,4 @@ import subprocess -from pathlib import Path from borg_import.main import main @@ -55,4 +54,4 @@ def test_borg_import(tmpdir, monkeypatch): assert extract_dir1.join("file1.txt").read() == "This is file 1 in archive 1" assert extract_dir1.join("file2.txt").read() == "This is file 2 in archive 1" assert extract_dir2.join("file1.txt").read() == "This is file 1 in archive 2" - assert extract_dir2.join("file2.txt").read() == "This is file 2 in archive 2" \ No newline at end of file + assert extract_dir2.join("file2.txt").read() == "This is file 2 in archive 2" diff --git a/src/borg_import/testsuite/test_tmbackup.py b/src/borg_import/testsuite/test_tmbackup.py index b520fc5..0b8bdaa 100644 --- a/src/borg_import/testsuite/test_tmbackup.py +++ b/src/borg_import/testsuite/test_tmbackup.py @@ -1,6 +1,7 @@ import subprocess from pathlib import Path + def test_rsync_tmbackup_import(tmpdir, monkeypatch): """Test importing rsync-time-backup style snapshots into a borg repo.""" src = Path(str(tmpdir.mkdir("tmbackup"))) @@ -24,17 +25,15 @@ def test_rsync_tmbackup_import(tmpdir, monkeypatch): # Run the importer monkeypatch.setattr( - "sys.argv", - ["borg-import", "rsync_tmbackup", "--prefix", "backup-", str(src), str(target_repo)], + "sys.argv", ["borg-import", "rsync_tmbackup", "--prefix", "backup-", str(src), str(target_repo)] ) from borg_import.main import main + main() # Verify archives were created - output = subprocess.check_output( - ["borg", "list", "--short", str(target_repo)] - ).decode() + output = subprocess.check_output(["borg", "list", "--short", str(target_repo)]).decode() archives = output.splitlines() assert len(archives) == 2 @@ -48,4 +47,4 @@ def test_rsync_tmbackup_import(tmpdir, monkeypatch): restored = extract_dir / "file1.txt" assert restored.exists() - assert restored.read_text() == "This is file 1 in snap1" \ No newline at end of file + assert restored.read_text() == "This is file 1 in snap1"