|
1 | 1 | import tempfile |
2 | 2 | from pathlib import Path |
3 | 3 | import shutil |
4 | | -from sync import sync |
| 4 | +from sync import sync, determine_actions |
5 | 5 |
|
6 | | -def test_when_a_file_exists_in_the_source_but_not_the_destination(): |
7 | | - try: |
8 | | - source = tempfile.mkdtemp() |
9 | | - dest = tempfile.mkdtemp() |
10 | 6 |
|
11 | | - content = "I am a very useful file" |
12 | | - (Path(source) / 'my-file').write_text(content) |
| 7 | +class TestE2E: |
13 | 8 |
|
14 | | - sync(source, dest) |
| 9 | + @staticmethod |
| 10 | + def test_when_a_file_exists_in_the_source_but_not_the_destination(): |
| 11 | + try: |
| 12 | + source = tempfile.mkdtemp() |
| 13 | + dest = tempfile.mkdtemp() |
15 | 14 |
|
16 | | - expected_path = Path(dest) / 'my-file' |
17 | | - assert expected_path.exists() |
18 | | - assert expected_path.read_text() == content |
| 15 | + content = "I am a very useful file" |
| 16 | + (Path(source) / 'my-file').write_text(content) |
19 | 17 |
|
20 | | - finally: |
21 | | - shutil.rmtree(source) |
22 | | - shutil.rmtree(dest) |
| 18 | + sync(source, dest) |
23 | 19 |
|
| 20 | + expected_path = Path(dest) / 'my-file' |
| 21 | + assert expected_path.exists() |
| 22 | + assert expected_path.read_text() == content |
24 | 23 |
|
25 | | -def test_when_a_file_has_been_renamed_in_the_source(): |
26 | | - try: |
27 | | - source = tempfile.mkdtemp() |
28 | | - dest = tempfile.mkdtemp() |
| 24 | + finally: |
| 25 | + shutil.rmtree(source) |
| 26 | + shutil.rmtree(dest) |
29 | 27 |
|
30 | | - content = "I am a file that was renamed" |
31 | | - source_path = Path(source) / 'source-filename' |
32 | | - old_dest_path = Path(dest) / 'dest-filename' |
33 | | - expected_dest_path = Path(dest) / 'source-filename' |
34 | | - source_path.write_text(content) |
35 | | - old_dest_path.write_text(content) |
| 28 | + @staticmethod |
| 29 | + def test_when_a_file_has_been_renamed_in_the_source(): |
| 30 | + try: |
| 31 | + source = tempfile.mkdtemp() |
| 32 | + dest = tempfile.mkdtemp() |
36 | 33 |
|
37 | | - sync(source, dest) |
| 34 | + content = "I am a file that was renamed" |
| 35 | + source_path = Path(source) / 'source-filename' |
| 36 | + old_dest_path = Path(dest) / 'dest-filename' |
| 37 | + expected_dest_path = Path(dest) / 'source-filename' |
| 38 | + source_path.write_text(content) |
| 39 | + old_dest_path.write_text(content) |
38 | 40 |
|
39 | | - assert old_dest_path.exists() is False |
40 | | - assert expected_dest_path.read_text() == content |
| 41 | + sync(source, dest) |
41 | 42 |
|
| 43 | + assert old_dest_path.exists() is False |
| 44 | + assert expected_dest_path.read_text() == content |
42 | 45 |
|
43 | | - finally: |
44 | | - shutil.rmtree(source) |
45 | | - shutil.rmtree(dest) |
| 46 | + finally: |
| 47 | + shutil.rmtree(source) |
| 48 | + shutil.rmtree(dest) |
| 49 | + |
| 50 | + |
| 51 | +class TestAction: |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def test_when_a_file_exists_in_the_source_but_not_the_destination(): |
| 55 | + src_hashes = {'hash1': 'fn1'} |
| 56 | + dst_hashes = {} |
| 57 | + actions = list(determine_actions(src_hashes, dst_hashes, Path('/src'), Path('/dst'))) |
| 58 | + assert actions == [('copy', Path('/src/fn1'), Path('/dst/fn1'))] |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def test_when_a_file_has_been_renamed_in_the_source(): |
| 62 | + src_hashes = {'hash1': 'fn1'} |
| 63 | + dst_hashes = {'hash1': 'fn2'} |
| 64 | + actions = list(determine_actions(src_hashes, dst_hashes, Path('/src'), Path('/dst'))) |
| 65 | + assert actions == [('move', Path('/dst/fn2'), Path('/dst/fn1'))] |
46 | 66 |
|
0 commit comments