-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fe5ff38
Fixed file corruption bugs in private storage code.
nickovs e5eb061
Merge branch 'dev' into private-storage-2
nickovs 7df69ed
Restoring fixed test case.
nickovs e5cb184
Implemented test suite for utils/json.py
nickovs 86303e3
Added new unit test cases for util/json.py
nickovs 11d03d0
Dixed formatting nags
nickovs 10646fa
Fixed more nags from the Hound
nickovs 83a1913
Added doc strings to some very short functions
nickovs 070ac97
Fixing lint's complains about my choice of parts of speach. Sigh.
nickovs 52dfcc1
Moved atomic save operations down into util/json.py so that all benefit.
nickovs f80a92e
Apparently 'e' is not allows as a variable name for an exception...
nickovs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Test Home Assistant json utility functions.""" | ||
import os | ||
import unittest | ||
import sys | ||
from tempfile import mkdtemp | ||
|
||
from homeassistant.util.json import (SerializationError, | ||
load_json, save_json) | ||
from homeassistant.exceptions import HomeAssistantError | ||
|
||
# Test data that can be saved as JSON | ||
TEST_JSON_A = {"a": 1, "B": "two"} | ||
TEST_JSON_B = {"a": "one", "B": 2} | ||
# Test data that can not be saved as JSON (keys must be strings) | ||
TEST_BAD_OBJECT = {("A",): 1} | ||
# Test data that can not be loaded as JSON | ||
TEST_BAD_SERIALIED = "THIS IS NOT JSON\n" | ||
|
||
|
||
class TestJSON(unittest.TestCase): | ||
"""Test util.json save and load.""" | ||
|
||
def setUp(self): | ||
"""Set up for tests.""" | ||
self.tmp_dir = mkdtemp() | ||
|
||
def tearDown(self): | ||
"""Clean up after tests.""" | ||
for fname in os.listdir(self.tmp_dir): | ||
os.remove(os.path.join(self.tmp_dir, fname)) | ||
os.rmdir(self.tmp_dir) | ||
|
||
def _path_for(self, leaf_name): | ||
return os.path.join(self.tmp_dir, leaf_name+".json") | ||
|
||
def test_save_and_load(self): | ||
"""Test saving and loading back.""" | ||
fname = self._path_for("test1") | ||
save_json(fname, TEST_JSON_A) | ||
data = load_json(fname) | ||
self.assertEqual(data, TEST_JSON_A) | ||
|
||
# Skipped on Windows | ||
@unittest.skipIf(sys.platform.startswith('win'), | ||
"private permissions not supported on Windows") | ||
def test_save_and_load_private(self): | ||
"""Test we can load private files and that they are protected.""" | ||
fname = self._path_for("test2") | ||
save_json(fname, TEST_JSON_A, private=True) | ||
data = load_json(fname) | ||
self.assertEqual(data, TEST_JSON_A) | ||
stats = os.stat(fname) | ||
self.assertEqual(stats.st_mode & 0o77, 0) | ||
|
||
def test_overwrite_and_reload(self): | ||
"""Test that we can overwrite an existing file and read back.""" | ||
fname = self._path_for("test3") | ||
save_json(fname, TEST_JSON_A) | ||
save_json(fname, TEST_JSON_B) | ||
data = load_json(fname) | ||
self.assertEqual(data, TEST_JSON_B) | ||
|
||
def test_save_bad_data(self): | ||
"""Test error from trying to save unserialisable data.""" | ||
fname = self._path_for("test4") | ||
with self.assertRaises(SerializationError): | ||
save_json(fname, TEST_BAD_OBJECT) | ||
|
||
def test_load_bad_data(self): | ||
"""Test error from trying to load unserialisable data.""" | ||
fname = self._path_for("test5") | ||
with open(fname, "w") as fh: | ||
fh.write(TEST_BAD_SERIALIED) | ||
with self.assertRaises(HomeAssistantError): | ||
load_json(fname) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
expected 2 blank lines, found 1