-
-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Added working support for private storage #16903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
fe5ff38
e5eb061
7df69ed
e5cb184
86303e3
11d03d0
10646fa
83a1913
070ac97
52dfcc1
f80a92e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,7 +187,9 @@ def _write_data(self, path: str, data: Dict): | |
| os.makedirs(os.path.dirname(path)) | ||
|
|
||
| _LOGGER.debug('Writing data for %s', self.key) | ||
| json.save_json(path, data, self._private) | ||
| tmp_path = path + "__TEMP__" | ||
| json.save_json(tmp_path, data, self._private) | ||
| os.replace(tmp_path, path) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we should move this functionality to I also think that we should consider cleaning up the temp path in case of an exception replacing the other file
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair points. I'll make it so. |
||
|
|
||
| async def _async_migrate_func(self, old_version, old_data): | ||
| """Migrate to the new version.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| MOCK_VERSION = 1 | ||
| MOCK_KEY = 'storage-test' | ||
| MOCK_DATA = {'hello': 'world'} | ||
| MOCK_DATA2 = {'goodbye': 'cruel world'} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
|
|
@@ -30,6 +31,14 @@ async def test_loading(hass, store): | |
| assert data == MOCK_DATA | ||
|
|
||
|
|
||
| async def test_overwriting(hass, store): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually doesn't do anything because all writes for the storage helper are mocked in tests
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha. Does that mean that the current storage implementation does not get tested down the filesystem level with the existing unit tests? If that's the case, is it fine to leave this out altogether?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, we try to avoid I/O as much as possible in unit tests as it's slow. We should still test this functionality but it should test |
||
| """Test we can save, overwrite and reload data.""" | ||
| await store.async_save(MOCK_DATA) | ||
| await store.async_save(MOCK_DATA2) | ||
| data = await store.async_load() | ||
| assert data == MOCK_DATA2 | ||
|
|
||
|
|
||
| async def test_loading_non_existing(hass, store): | ||
| """Test we can save and load data.""" | ||
| with patch('homeassistant.util.json.open', side_effect=FileNotFoundError): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark file as not visible with
., and we should use a context manager to cleanup the temporary file after this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to clean up since we're using os.replace