PyHardLinkBackup 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. PyHardLinkBackup/__init__.py +7 -0
  2. PyHardLinkBackup/__main__.py +10 -0
  3. PyHardLinkBackup/backup.py +203 -0
  4. PyHardLinkBackup/cli_app/__init__.py +41 -0
  5. PyHardLinkBackup/cli_app/phlb.py +50 -0
  6. PyHardLinkBackup/cli_dev/__init__.py +70 -0
  7. PyHardLinkBackup/cli_dev/benchmark.py +119 -0
  8. PyHardLinkBackup/cli_dev/code_style.py +12 -0
  9. PyHardLinkBackup/cli_dev/packaging.py +65 -0
  10. PyHardLinkBackup/cli_dev/shell_completion.py +23 -0
  11. PyHardLinkBackup/cli_dev/testing.py +52 -0
  12. PyHardLinkBackup/cli_dev/update_readme_history.py +33 -0
  13. PyHardLinkBackup/constants.py +16 -0
  14. PyHardLinkBackup/tests/__init__.py +36 -0
  15. PyHardLinkBackup/tests/test_backup.py +399 -0
  16. PyHardLinkBackup/tests/test_doc_write.py +25 -0
  17. PyHardLinkBackup/tests/test_doctests.py +10 -0
  18. PyHardLinkBackup/tests/test_project_setup.py +46 -0
  19. PyHardLinkBackup/tests/test_readme.py +75 -0
  20. PyHardLinkBackup/tests/test_readme_history.py +8 -0
  21. PyHardLinkBackup/utilities/__init__.py +0 -0
  22. PyHardLinkBackup/utilities/file_hash_database.py +58 -0
  23. PyHardLinkBackup/utilities/file_size_database.py +46 -0
  24. PyHardLinkBackup/utilities/filesystem.py +133 -0
  25. PyHardLinkBackup/utilities/humanize.py +22 -0
  26. PyHardLinkBackup/utilities/rich_utils.py +98 -0
  27. PyHardLinkBackup/utilities/tests/__init__.py +0 -0
  28. PyHardLinkBackup/utilities/tests/test_file_hash_database.py +134 -0
  29. PyHardLinkBackup/utilities/tests/test_file_size_database.py +131 -0
  30. PyHardLinkBackup/utilities/tests/test_filesystem.py +94 -0
  31. pyhardlinkbackup-1.0.0.dist-info/METADATA +547 -0
  32. pyhardlinkbackup-1.0.0.dist-info/RECORD +34 -0
  33. pyhardlinkbackup-1.0.0.dist-info/WHEEL +4 -0
  34. pyhardlinkbackup-1.0.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,16 @@
1
+ from pathlib import Path
2
+
3
+ import PyHardLinkBackup
4
+
5
+
6
+ CLI_EPILOG = 'Project Homepage: https://github.com/jedie/PyHardLinkBackup'
7
+
8
+ BASE_PATH = Path(PyHardLinkBackup.__file__).parent
9
+
10
+
11
+ ##########################################################################
12
+ # "Settings" for PyHardLinkBackup:
13
+
14
+ CHUNK_SIZE = 64 * 1024 * 1024 # 64 MB
15
+ SMALL_FILE_THRESHOLD = 1000 # bytes
16
+ HASH_ALGO = 'sha256'
@@ -0,0 +1,36 @@
1
+ import os
2
+ import unittest.util
3
+ from pathlib import Path
4
+
5
+ from bx_py_utils.test_utils.deny_requests import deny_any_real_request
6
+ from cli_base.cli_tools.verbosity import MAX_LOG_LEVEL, setup_logging
7
+ from rich import print # noqa
8
+ from typeguard import install_import_hook
9
+
10
+
11
+ # Check type annotations via typeguard in all tests:
12
+ install_import_hook(packages=('PyHardLinkBackup',))
13
+
14
+
15
+ def pre_configure_tests() -> None:
16
+ print(f'Configure unittests via "load_tests Protocol" from {Path(__file__).relative_to(Path.cwd())}')
17
+
18
+ # Hacky way to display more "assert"-Context in failing tests:
19
+ _MIN_MAX_DIFF = unittest.util._MAX_LENGTH - unittest.util._MIN_DIFF_LEN
20
+ unittest.util._MAX_LENGTH = int(os.environ.get('UNITTEST_MAX_LENGTH', 2000))
21
+ unittest.util._MIN_DIFF_LEN = unittest.util._MAX_LENGTH - _MIN_MAX_DIFF
22
+
23
+ # Deny any request via docket/urllib3 because tests they should mock all requests:
24
+ deny_any_real_request()
25
+
26
+ # Display DEBUG logs in tests:
27
+ setup_logging(verbosity=MAX_LOG_LEVEL)
28
+
29
+
30
+ def load_tests(loader, tests, pattern):
31
+ """
32
+ Use unittest "load_tests Protocol" as a hook to setup test environment before running tests.
33
+ https://docs.python.org/3/library/unittest.html#load-tests-protocol
34
+ """
35
+ pre_configure_tests()
36
+ return loader.discover(start_dir=Path(__file__).parent, pattern=pattern)
@@ -0,0 +1,399 @@
1
+ import datetime
2
+ import os
3
+ import tempfile
4
+ import textwrap
5
+ import zlib
6
+ from pathlib import Path
7
+ from unittest import TestCase
8
+
9
+ from bx_py_utils.test_utils.assertion import assert_text_equal
10
+ from bx_py_utils.test_utils.datetime import parse_dt
11
+ from freezegun import freeze_time
12
+ from tabulate import tabulate
13
+
14
+ from PyHardLinkBackup.backup import BackupResult, backup_tree
15
+ from PyHardLinkBackup.constants import CHUNK_SIZE
16
+ from PyHardLinkBackup.utilities.file_size_database import FileSizeDatabase
17
+ from PyHardLinkBackup.utilities.filesystem import iter_scandir_files
18
+ from PyHardLinkBackup.utilities.tests.test_file_hash_database import assert_hash_db_info
19
+
20
+
21
+ def set_file_times(path: Path, dt: datetime.datetime):
22
+ # move dt to UTC if it has timezone info:
23
+ if dt.tzinfo is not None:
24
+ dt = dt.astimezone(datetime.timezone.utc).replace(tzinfo=None)
25
+ fixed_time = dt.timestamp()
26
+ for entry in iter_scandir_files(path, excludes=set()):
27
+ os.utime(entry.path, (fixed_time, fixed_time))
28
+
29
+
30
+ def fs_tree_overview(root: Path) -> str:
31
+ lines = []
32
+ for entry in iter_scandir_files(root, excludes=set()):
33
+ file_path = Path(entry.path)
34
+ try:
35
+ file_stat = entry.stat()
36
+ except FileNotFoundError:
37
+ crc32 = '-'
38
+ nlink = '-'
39
+ size = '-'
40
+ birthtime = '-'
41
+ else:
42
+ crc32 = zlib.crc32(file_path.read_bytes())
43
+ crc32 = f'{crc32:08x}'
44
+ nlink = file_stat.st_nlink
45
+ size = file_stat.st_size
46
+ birthtime = getattr(file_stat, 'st_birthtime', file_stat.st_mtime)
47
+ birthtime = datetime.datetime.fromtimestamp(birthtime).strftime('%H:%M:%S')
48
+
49
+ if entry.is_symlink():
50
+ file_type = 'symlink'
51
+ elif nlink > 1:
52
+ file_type = 'hardlink'
53
+ else:
54
+ file_type = 'file'
55
+
56
+ lines.append(
57
+ [
58
+ str(file_path.relative_to(root)),
59
+ birthtime,
60
+ file_type,
61
+ nlink,
62
+ size,
63
+ crc32,
64
+ ]
65
+ )
66
+
67
+ result = tabulate(sorted(lines), headers=['path', 'birthtime', 'type', 'nlink', 'size', 'CRC32'], tablefmt='plain')
68
+ return result
69
+
70
+
71
+ def assert_fs_tree_overview(root: Path, expected_overview: str):
72
+ expected_overview = textwrap.dedent(expected_overview).strip()
73
+ actual_overview = fs_tree_overview(root)
74
+ assert_text_equal(
75
+ actual_overview,
76
+ expected_overview,
77
+ msg=f'Filesystem tree overview does not match expected overview.\n\n{actual_overview}\n\n',
78
+ )
79
+
80
+
81
+ class BackupTreeTestCase(TestCase):
82
+ def test_happy_path(self):
83
+ with tempfile.TemporaryDirectory() as temp_dir:
84
+ temp_path = Path(temp_dir)
85
+
86
+ src_root = temp_path / 'source'
87
+ backup_root = temp_path / 'backup'
88
+
89
+ src_root.mkdir()
90
+ backup_root.mkdir()
91
+
92
+ file1_path = src_root / 'file2.txt'
93
+ file1_path.write_text('This is file 1')
94
+
95
+ (src_root / 'symlink2file1').symlink_to(file1_path)
96
+ os.link(file1_path, src_root / 'hardlink2file1')
97
+
98
+ sub_dir = src_root / 'subdir'
99
+ sub_dir.mkdir()
100
+ (sub_dir / 'file.txt').write_text('This is file in subdir')
101
+
102
+ # Only files bigger than MIN_SIZE will be considered for hardlinking:
103
+ size_db_min_file = src_root / 'min_sized_file1.bin'
104
+ size_db_min_file.write_bytes(b'X' * FileSizeDatabase.MIN_SIZE)
105
+
106
+ # Same content and big enough to be considered for hardlinking:
107
+ size_db_min_file = src_root / 'min_sized_file2.bin'
108
+ size_db_min_file.write_bytes(b'X' * FileSizeDatabase.MIN_SIZE)
109
+
110
+ # Larger then CHUNK_SIZE file will be handled differently:
111
+ large_file = src_root / 'large_file.bin'
112
+ large_file.write_bytes(b'Y' * (CHUNK_SIZE + 1))
113
+
114
+ excluded_dir = src_root / '.cache'
115
+ excluded_dir.mkdir()
116
+ (excluded_dir / 'tempfile.tmp').write_text('Temporary file that should be excluded')
117
+
118
+ # FIXME: freezegun doesn't handle this, see: https://github.com/spulec/freezegun/issues/392
119
+ # Set modification times to a fixed time for easier testing:
120
+ set_file_times(src_root, dt=parse_dt('2026-01-01T12:00:00+0000'))
121
+
122
+ #######################################################################################
123
+ # Create first backup:
124
+
125
+ with freeze_time('2026-01-01T12:34:56Z', auto_tick_seconds=0):
126
+ result = backup_tree(
127
+ src_root=src_root,
128
+ backup_root=backup_root,
129
+ excludes={'.cache'},
130
+ )
131
+ backup_dir = result.backup_dir
132
+ self.assertEqual(
133
+ str(Path(backup_dir).relative_to(temp_path)),
134
+ 'backup/source/20260101_123456',
135
+ )
136
+ self.assertEqual(
137
+ result,
138
+ BackupResult(
139
+ backup_dir=backup_dir,
140
+ backup_count=7,
141
+ backup_size=67110929,
142
+ symlink_files=1,
143
+ hardlinked_files=1,
144
+ hardlinked_size=1000,
145
+ copied_files=5,
146
+ copied_size=67109915,
147
+ copied_small_files=3,
148
+ copied_small_size=50,
149
+ ),
150
+ )
151
+
152
+ # The sources:
153
+ assert_fs_tree_overview(
154
+ root=src_root,
155
+ expected_overview="""
156
+ path birthtime type nlink size CRC32
157
+ .cache/tempfile.tmp 12:00:00 file 1 38 41d7a2c9
158
+ file2.txt 12:00:00 hardlink 2 14 8a11514a
159
+ hardlink2file1 12:00:00 hardlink 2 14 8a11514a
160
+ large_file.bin 12:00:00 file 1 67108865 9671eaac
161
+ min_sized_file1.bin 12:00:00 file 1 1000 f0d93de4
162
+ min_sized_file2.bin 12:00:00 file 1 1000 f0d93de4
163
+ subdir/file.txt 12:00:00 file 1 22 c0167e63
164
+ symlink2file1 12:00:00 symlink 2 14 8a11514a
165
+ """,
166
+ )
167
+ # The backup:
168
+ # * /.cache/ -> excluded
169
+ # * min_sized_file1.bin and min_sized_file2.bin -> hardlinked
170
+ assert_fs_tree_overview(
171
+ root=backup_dir,
172
+ expected_overview="""
173
+ path birthtime type nlink size CRC32
174
+ file2.txt 12:00:00 file 1 14 8a11514a
175
+ hardlink2file1 12:00:00 file 1 14 8a11514a
176
+ large_file.bin 12:00:00 file 1 67108865 9671eaac
177
+ min_sized_file1.bin 12:00:00 hardlink 2 1000 f0d93de4
178
+ min_sized_file2.bin 12:00:00 hardlink 2 1000 f0d93de4
179
+ subdir/file.txt 12:00:00 file 1 22 c0167e63
180
+ symlink2file1 12:00:00 symlink 2 14 8a11514a
181
+ """,
182
+ )
183
+
184
+ # Let's check our FileHashDatabase:
185
+ assert_hash_db_info(
186
+ backup_root=backup_root,
187
+ expected="""
188
+ bb/c4/bbc4de2ca238d1… -> source/20260101_123456/min_sized_file1.bin
189
+ e6/37/e6374ac11d9049… -> source/20260101_123456/large_file.bin
190
+ """,
191
+ )
192
+
193
+ #######################################################################################
194
+ # Just backup again:
195
+
196
+ with freeze_time('2026-01-02T12:34:56Z', auto_tick_seconds=0):
197
+ result = backup_tree(
198
+ src_root=src_root,
199
+ backup_root=backup_root,
200
+ excludes={'.cache'},
201
+ )
202
+ backup_dir = result.backup_dir
203
+ self.assertEqual(
204
+ str(Path(backup_dir).relative_to(temp_path)),
205
+ 'backup/source/20260102_123456',
206
+ )
207
+ self.assertEqual(
208
+ result,
209
+ BackupResult(
210
+ backup_dir=backup_dir,
211
+ backup_count=7,
212
+ backup_size=67110929,
213
+ symlink_files=1,
214
+ hardlinked_files=3, # <<< More hardlinks this time!
215
+ hardlinked_size=67110865,
216
+ copied_files=3,
217
+ copied_size=50,
218
+ copied_small_files=3,
219
+ copied_small_size=50,
220
+ ),
221
+ )
222
+ # The second backup:
223
+ # * /.cache/ -> excluded
224
+ # * min_sized_file1.bin and min_sized_file2.bin -> hardlinked
225
+ assert_fs_tree_overview(
226
+ root=backup_dir,
227
+ expected_overview="""
228
+ path birthtime type nlink size CRC32
229
+ file2.txt 12:00:00 file 1 14 8a11514a
230
+ hardlink2file1 12:00:00 file 1 14 8a11514a
231
+ large_file.bin 12:00:00 hardlink 2 67108865 9671eaac
232
+ min_sized_file1.bin 12:00:00 hardlink 4 1000 f0d93de4
233
+ min_sized_file2.bin 12:00:00 hardlink 4 1000 f0d93de4
234
+ subdir/file.txt 12:00:00 file 1 22 c0167e63
235
+ symlink2file1 12:00:00 symlink 2 14 8a11514a
236
+ """,
237
+ )
238
+
239
+ # The FileHashDatabase remains the same:
240
+ assert_hash_db_info(
241
+ backup_root=backup_root,
242
+ expected="""
243
+ bb/c4/bbc4de2ca238d1… -> source/20260101_123456/min_sized_file1.bin
244
+ e6/37/e6374ac11d9049… -> source/20260101_123456/large_file.bin
245
+ """,
246
+ )
247
+
248
+ #######################################################################################
249
+ # Don't create broken hardlinks!
250
+
251
+ """DocWrite: README.md ## FileHashDatabase - Missing hardlink target file
252
+ If a hardlink source from a old backup is missing, we cannot create a hardlink to it.
253
+ But it still works to hardlink same files within the current backup.
254
+ """
255
+
256
+ # Let's remove one of the files used for hardlinking from the first backup:
257
+ (backup_root / 'source/20260101_123456/min_sized_file1.bin').unlink()
258
+
259
+ # Backup again:
260
+ with freeze_time('2026-01-03T12:34:56Z', auto_tick_seconds=0):
261
+ result = backup_tree(
262
+ src_root=src_root,
263
+ backup_root=backup_root,
264
+ excludes={'.cache'},
265
+ )
266
+ backup_dir = result.backup_dir
267
+ self.assertEqual(
268
+ result,
269
+ BackupResult(
270
+ backup_dir=backup_dir,
271
+ backup_count=7,
272
+ backup_size=67110929,
273
+ symlink_files=1,
274
+ hardlinked_files=2, # <<< Less hardlinks this time, because of missing link source!
275
+ hardlinked_size=67109865,
276
+ copied_files=4,
277
+ copied_size=1050,
278
+ copied_small_files=3,
279
+ copied_small_size=50,
280
+ ),
281
+ )
282
+
283
+ # Note: min_sized_file1.bin and min_sized_file2.bin are hardlinked,
284
+ # but not with the first backup anymore! So it's only nlink=2 now!
285
+ assert_fs_tree_overview(
286
+ root=backup_dir,
287
+ expected_overview="""
288
+ path birthtime type nlink size CRC32
289
+ file2.txt 12:00:00 file 1 14 8a11514a
290
+ hardlink2file1 12:00:00 file 1 14 8a11514a
291
+ large_file.bin 12:00:00 hardlink 3 67108865 9671eaac
292
+ min_sized_file1.bin 12:00:00 hardlink 2 1000 f0d93de4
293
+ min_sized_file2.bin 12:00:00 hardlink 2 1000 f0d93de4
294
+ subdir/file.txt 12:00:00 file 1 22 c0167e63
295
+ symlink2file1 12:00:00 symlink 2 14 8a11514a
296
+ """,
297
+ )
298
+
299
+ # Note: min_sized_file1.bin is now from the 2026-01-03 backup!
300
+ self.assertEqual(backup_dir.name, '20260103_123456') # Latest backup dir name
301
+ assert_hash_db_info(
302
+ backup_root=backup_root,
303
+ expected="""
304
+ bb/c4/bbc4de2ca238d1… -> source/20260103_123456/min_sized_file1.bin
305
+ e6/37/e6374ac11d9049… -> source/20260101_123456/large_file.bin
306
+ """,
307
+ )
308
+
309
+ def test_symlink(self):
310
+ with tempfile.TemporaryDirectory() as temp_dir:
311
+ temp_path = Path(temp_dir)
312
+
313
+ src_root = temp_path / 'src'
314
+ backup_root = temp_path / 'bak'
315
+
316
+ src_root.mkdir()
317
+ backup_root.mkdir()
318
+
319
+ source_file_path = src_root / 'source_file.txt'
320
+ source_file_path.write_text('File in the "source" directory.')
321
+
322
+ symlink2source_file_path = src_root / 'symlink2source'
323
+ symlink2source_file_path.symlink_to(source_file_path)
324
+ self.assertEqual(symlink2source_file_path.read_text(), 'File in the "source" directory.')
325
+
326
+ outside_file_path = temp_path / 'outside_file.txt'
327
+ outside_file_path.write_text('File outside the "source" directory!')
328
+
329
+ symlink2outside_file_path = src_root / 'symlink2outside'
330
+ symlink2outside_file_path.symlink_to(outside_file_path)
331
+ self.assertEqual(symlink2outside_file_path.read_text(), 'File outside the "source" directory!')
332
+
333
+ # FIXME: freezegun doesn't handle this, see: https://github.com/spulec/freezegun/issues/392
334
+ # Set modification times to a fixed time for easier testing:
335
+ set_file_times(src_root, dt=parse_dt('2026-01-01T12:00:00+0000'))
336
+
337
+ broken_symlink_path = src_root / 'broken_symlink'
338
+ broken_symlink_path.symlink_to(temp_path / 'not/existing/file.txt')
339
+ broken_symlink_path.is_symlink()
340
+
341
+ #######################################################################################
342
+ # Create first backup:
343
+
344
+ with freeze_time('2026-01-01T12:34:56Z', auto_tick_seconds=0):
345
+ result = backup_tree(src_root=src_root, backup_root=backup_root, excludes=set())
346
+ backup_dir1 = result.backup_dir
347
+ self.assertEqual(
348
+ str(Path(backup_dir1).relative_to(temp_path)),
349
+ 'bak/src/20260101_123456',
350
+ )
351
+
352
+ assert_fs_tree_overview(
353
+ root=temp_path, # The complete overview os source + backup and outside file
354
+ expected_overview="""
355
+ path birthtime type nlink size CRC32
356
+ bak/src/20260101_123456/broken_symlink - symlink - - -
357
+ bak/src/20260101_123456/source_file.txt 12:00:00 file 1 31 9309a10c
358
+ bak/src/20260101_123456/symlink2outside 12:00:00 symlink 1 36 24b5bf4c
359
+ bak/src/20260101_123456/symlink2source 12:00:00 symlink 1 31 9309a10c
360
+ outside_file.txt 12:00:00 file 1 36 24b5bf4c
361
+ src/broken_symlink - symlink - - -
362
+ src/source_file.txt 12:00:00 file 1 31 9309a10c
363
+ src/symlink2outside 12:00:00 symlink 1 36 24b5bf4c
364
+ src/symlink2source 12:00:00 symlink 1 31 9309a10c
365
+ """,
366
+ )
367
+
368
+ self.assertEqual(
369
+ result,
370
+ BackupResult(
371
+ backup_dir=backup_dir1,
372
+ backup_count=4,
373
+ backup_size=98,
374
+ symlink_files=3,
375
+ hardlinked_files=0,
376
+ hardlinked_size=0,
377
+ copied_files=1,
378
+ copied_size=31,
379
+ copied_small_files=1,
380
+ copied_small_size=31,
381
+ ),
382
+ )
383
+
384
+ """DocWrite: README.md ## backup implementation - Symlinks
385
+ Symlinks are copied as symlinks in the backup."""
386
+ self.assertEqual(
387
+ (backup_dir1 / 'symlink2outside').read_text(),
388
+ 'File outside the "source" directory!',
389
+ )
390
+ self.assertEqual(
391
+ (backup_dir1 / 'symlink2source').read_text(),
392
+ 'File in the "source" directory.',
393
+ )
394
+ self.assertEqual((backup_dir1 / 'symlink2outside').readlink(), outside_file_path)
395
+ self.assertEqual((backup_dir1 / 'symlink2source').readlink(), source_file_path)
396
+
397
+ """DocWrite: README.md ## backup implementation - Symlinks
398
+ Symlinks are not stored in our FileHashDatabase, because they are not considered for hardlinking."""
399
+ assert_hash_db_info(backup_root=backup_root, expected='')
@@ -0,0 +1,25 @@
1
+ from unittest import TestCase
2
+
3
+ from bx_py_utils.doc_write.api import GeneratedInfo, generate
4
+ from bx_py_utils.path import assert_is_file
5
+
6
+ from PyHardLinkBackup.cli_dev import PACKAGE_ROOT
7
+
8
+
9
+ class DocuWriteApiTestCase(TestCase):
10
+ def test_up2date_docs(self):
11
+ """DocWrite: about-docs.md # generate Doc-Write
12
+
13
+ These documentation files are generated automatically with the "Doc-Write" tool.
14
+ They updated automatically by unittests.
15
+
16
+ More information about Doc-Write can be found here:
17
+
18
+ https://github.com/boxine/bx_py_utils/tree/master/bx_py_utils/doc_write
19
+ """
20
+ assert_is_file(PACKAGE_ROOT / 'pyproject.toml')
21
+
22
+ info: GeneratedInfo = generate(base_path=PACKAGE_ROOT)
23
+ self.assertGreaterEqual(len(info.paths), 1)
24
+ self.assertEqual(info.update_count, 0, 'No files should be updated, commit the changes')
25
+ self.assertEqual(info.remove_count, 0, 'No files should be removed, commit the changes')
@@ -0,0 +1,10 @@
1
+ from bx_py_utils.test_utils.unittest_utils import BaseDocTests
2
+
3
+ import PyHardLinkBackup
4
+
5
+
6
+ class DocTests(BaseDocTests):
7
+ def test_doctests(self):
8
+ self.run_doctests(
9
+ modules=(PyHardLinkBackup,),
10
+ )
@@ -0,0 +1,46 @@
1
+ import subprocess
2
+ from unittest import TestCase
3
+
4
+ from bx_py_utils.path import assert_is_file
5
+ from cli_base.cli_tools.code_style import assert_code_style
6
+ from cli_base.cli_tools.subprocess_utils import ToolsExecutor
7
+ from manageprojects.test_utils.project_setup import check_editor_config, get_py_max_line_length
8
+ from packaging.version import Version
9
+
10
+ from PyHardLinkBackup import __version__
11
+ from PyHardLinkBackup.cli_dev import PACKAGE_ROOT
12
+
13
+
14
+ class ProjectSetupTestCase(TestCase):
15
+ def test_version(self):
16
+ self.assertIsNotNone(__version__)
17
+
18
+ version = Version(__version__) # Will raise InvalidVersion() if wrong formatted
19
+ self.assertEqual(str(version), __version__)
20
+
21
+ cli_bin = PACKAGE_ROOT / 'cli.py'
22
+ assert_is_file(cli_bin)
23
+
24
+ output = subprocess.check_output([cli_bin, 'version'], text=True)
25
+ self.assertIn(f'PyHardLinkBackup v{__version__}', output)
26
+
27
+ dev_cli_bin = PACKAGE_ROOT / 'dev-cli.py'
28
+ assert_is_file(dev_cli_bin)
29
+
30
+ output = subprocess.check_output([dev_cli_bin, 'version'], text=True)
31
+ self.assertIn(f'PyHardLinkBackup v{__version__}', output)
32
+
33
+ def test_code_style(self):
34
+ return_code = assert_code_style(package_root=PACKAGE_ROOT)
35
+ self.assertEqual(return_code, 0, 'Code style error, see output above!')
36
+
37
+ def test_check_editor_config(self):
38
+ check_editor_config(package_root=PACKAGE_ROOT)
39
+
40
+ max_line_length = get_py_max_line_length(package_root=PACKAGE_ROOT)
41
+ self.assertEqual(max_line_length, 119)
42
+
43
+ def test_pre_commit_hooks(self):
44
+ executor = ToolsExecutor(cwd=PACKAGE_ROOT)
45
+ for command in ('migrate-config', 'validate-config', 'validate-manifest'):
46
+ executor.verbose_check_call('pre-commit', command, exit_on_error=True)
@@ -0,0 +1,75 @@
1
+ from bx_py_utils.auto_doc import assert_readme_block
2
+ from bx_py_utils.path import assert_is_file
3
+ from cli_base.cli_tools.test_utils.rich_test_utils import NoColorEnvRich, invoke
4
+ from manageprojects.tests.base import BaseTestCase
5
+
6
+ from PyHardLinkBackup import constants
7
+ from PyHardLinkBackup.cli_dev import PACKAGE_ROOT
8
+
9
+
10
+ def assert_cli_help_in_readme(text_block: str, marker: str):
11
+ README_PATH = PACKAGE_ROOT / 'README.md'
12
+ assert_is_file(README_PATH)
13
+
14
+ text_block = text_block.replace(constants.CLI_EPILOG, '')
15
+ text_block = f'```\n{text_block.strip()}\n```'
16
+ assert_readme_block(
17
+ readme_path=README_PATH,
18
+ text_block=text_block,
19
+ start_marker_line=f'[comment]: <> (✂✂✂ auto generated {marker} start ✂✂✂)',
20
+ end_marker_line=f'[comment]: <> (✂✂✂ auto generated {marker} end ✂✂✂)',
21
+ )
22
+
23
+
24
+ class ReadmeTestCase(BaseTestCase):
25
+
26
+ def test_main_help(self):
27
+ with NoColorEnvRich():
28
+ stdout = invoke(cli_bin=PACKAGE_ROOT / 'cli.py', args=['--help'], strip_line_prefix='usage: ')
29
+
30
+ self.assert_in_content(
31
+ got=stdout,
32
+ parts=(
33
+ 'usage: ./cli.py [-h]',
34
+ ' version ',
35
+ 'Print version and exit',
36
+ constants.CLI_EPILOG,
37
+ ),
38
+ )
39
+
40
+ # Installed via pipx is called 'phlb', not 'cli.py':
41
+ stdout = stdout.replace('./cli.py', 'phlb')
42
+
43
+ assert_cli_help_in_readme(text_block=stdout, marker='main help')
44
+
45
+ def test_backup_help(self):
46
+ with NoColorEnvRich():
47
+ stdout = invoke(cli_bin=PACKAGE_ROOT / 'cli.py', args=['backup', '--help'], strip_line_prefix='usage: ')
48
+ self.assert_in_content(
49
+ got=stdout,
50
+ parts=(
51
+ 'usage: ./cli.py backup [-h] ',
52
+ 'Backup the source directory to the destination',
53
+ ),
54
+ )
55
+
56
+ # Installed via pipx is called 'phlb', not 'cli.py':
57
+ stdout = stdout.replace('./cli.py', 'phlb')
58
+
59
+ assert_cli_help_in_readme(text_block=stdout, marker='backup help')
60
+
61
+ def test_dev_help(self):
62
+ with NoColorEnvRich():
63
+ stdout = invoke(cli_bin=PACKAGE_ROOT / 'dev-cli.py', args=['--help'], strip_line_prefix='usage: ')
64
+ self.assert_in_content(
65
+ got=stdout,
66
+ parts=(
67
+ 'usage: ./dev-cli.py [-h]',
68
+ ' lint ',
69
+ ' coverage ',
70
+ ' update-readme-history ',
71
+ ' publish ',
72
+ constants.CLI_EPILOG,
73
+ ),
74
+ )
75
+ assert_cli_help_in_readme(text_block=stdout, marker='dev help')
@@ -0,0 +1,8 @@
1
+ from unittest import TestCase
2
+
3
+ from cli_base.cli_tools.git_history import update_readme_history
4
+
5
+
6
+ class ReadmeHistoryTestCase(TestCase):
7
+ def test_readme_history(self):
8
+ update_readme_history(raise_update_error=True)
File without changes