|
1 | | -import perfact.zodbsync.main |
2 | 1 | import os |
3 | 2 | import os.path |
| 3 | +import subprocess |
| 4 | +import pytest |
| 5 | + |
| 6 | +import perfact.zodbsync.main |
| 7 | +import perfact.zodbsync.tests.environment as env |
| 8 | + |
| 9 | + |
| 10 | +ZEOPORT = 9011 |
4 | 11 |
|
5 | 12 |
|
6 | 13 | class TestSync(): |
7 | 14 | ''' |
8 | 15 | All tests defined in this class share the same environment fixture (i.e., |
9 | 16 | same ZEO, same repo etc.) |
10 | 17 | ''' |
11 | | - def test_record(self, environment): |
12 | | - env = environment |
13 | | - perfact.zodbsync.main.create_runner([ |
14 | | - '--config', env.config.path, 'record', '/', |
15 | | - ]).run() |
16 | | - assert os.path.isfile(env.repo.path + '/__root__/acl_users/__meta__') |
17 | | - |
18 | | - def test_playback(self, environment): |
19 | | - env = environment |
20 | | - self.test_record(environment) |
21 | | - path = env.repo.path + '/__root__/index_html/__source-utf8__.html' |
| 18 | + |
| 19 | + @pytest.fixture(scope='class', autouse=True) |
| 20 | + def environment(self, request): |
| 21 | + ''' |
| 22 | + Fixture that is automatically used by all tests. Initializes |
| 23 | + environment and injects the elements of it into the class. |
| 24 | + ''' |
| 25 | + myenv = { |
| 26 | + 'zeo': env.ZeoInstance(port=ZEOPORT), |
| 27 | + 'repo': env.Repository(), |
| 28 | + 'zopeconfig': env.ZopeConfig(zeoport=ZEOPORT), |
| 29 | + } |
| 30 | + myenv['config'] = env.ZODBSyncConfig(env=myenv) |
| 31 | + |
| 32 | + # inject items into class so methods can use them |
| 33 | + for key, value in myenv.items(): |
| 34 | + setattr(request.cls, key, value) |
| 35 | + |
| 36 | + # at this point, all tests are called |
| 37 | + yield |
| 38 | + |
| 39 | + # clean up items |
| 40 | + for item in myenv.values(): |
| 41 | + item.cleanup() |
| 42 | + |
| 43 | + def runner(self, *cmd): |
| 44 | + ''' |
| 45 | + Create runner for given zodbsync command |
| 46 | + ''' |
| 47 | + return perfact.zodbsync.main.create_runner( |
| 48 | + ['--config', self.config.path] + list(cmd) |
| 49 | + ) |
| 50 | + |
| 51 | + def gitrun(self, *cmd): |
| 52 | + ''' |
| 53 | + Run git command. |
| 54 | + ''' |
| 55 | + subprocess.check_call( |
| 56 | + ['git', '-C', self.repo.path] + list(cmd) |
| 57 | + ) |
| 58 | + |
| 59 | + def gitoutput(self, *cmd): |
| 60 | + ''' |
| 61 | + Run git command, returning output. |
| 62 | + ''' |
| 63 | + return subprocess.check_output( |
| 64 | + ['git', '-C', self.repo.path] + list(cmd), |
| 65 | + universal_newlines=True, |
| 66 | + ) |
| 67 | + |
| 68 | + def record_all(self, commitmsg=None): |
| 69 | + ''' |
| 70 | + Record everything |
| 71 | + ''' |
| 72 | + self.runner('record', '/').run() |
| 73 | + if commitmsg is not None: |
| 74 | + self.gitrun('add', '.') |
| 75 | + self.gitrun('commit', '-m', commitmsg) |
| 76 | + |
| 77 | + def test_record(self): |
| 78 | + ''' |
| 79 | + Record everything and make sure acl_users exists. |
| 80 | + ''' |
| 81 | + self.record_all() |
| 82 | + assert os.path.isfile( |
| 83 | + self.repo.path + '/__root__/acl_users/__meta__' |
| 84 | + ) |
| 85 | + |
| 86 | + def test_playback(self): |
| 87 | + ''' |
| 88 | + Record everything, change /index_html, play it back and check if the |
| 89 | + contents are correct. |
| 90 | + ''' |
| 91 | + self.record_all() |
| 92 | + path = self.repo.path + '/__root__/index_html/__source-utf8__.html' |
22 | 93 | content = '<html></html>' |
23 | 94 | with open(path, 'w') as f: |
24 | 95 | f.write(content) |
25 | | - runner = perfact.zodbsync.main.create_runner([ |
26 | | - '--config', env.config.path, 'playback', '/index_html' |
27 | | - ]) |
| 96 | + runner = self.runner('playback', '/index_html') |
28 | 97 | runner.run() |
29 | 98 | assert runner.sync.app.index_html() == content |
| 99 | + |
| 100 | + def test_pick(self): |
| 101 | + # Record everything, commit it |
| 102 | + self.record_all(commitmsg='First commit') |
| 103 | + |
| 104 | + # Add a folder, commit it |
| 105 | + folder = self.repo.path + '/__root__/TestFolder' |
| 106 | + os.mkdir(folder) |
| 107 | + with open(folder + '/__meta__', 'w') as f: |
| 108 | + f.write('''[ |
| 109 | + ('props', []), |
| 110 | + ('title', ''), |
| 111 | + ('type', 'Folder'), |
| 112 | + ]''') |
| 113 | + self.gitrun('add', '.') |
| 114 | + self.gitrun('commit', '-m', 'Second commit') |
| 115 | + commit = self.gitoutput('show-ref', '--head', '--hash', 'HEAD').strip() |
| 116 | + |
| 117 | + # Reset the commit and pick it again |
| 118 | + self.gitrun('reset', '--hard', 'HEAD~') |
| 119 | + runner = self.runner('pick', commit) |
| 120 | + runner.run() |
| 121 | + |
| 122 | + assert 'TestFolder' in runner.sync.app.objectIds() |
0 commit comments