From c8b0f33494962c32af79968fb35069272de53c79 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Mon, 12 Aug 2024 14:59:25 -0400 Subject: [PATCH] test: Atomically update file data source file --- .github/workflows/ci.yml | 24 +++++++++++++++++++ .../integrations/files/file_data_source.py | 5 ++++ ldclient/testing/test_file_data_source.py | 4 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f3d6169..cd6f5c2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,30 @@ jobs: - name: Run tests run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - run: make test + - name: Verify typehints run: make lint diff --git a/ldclient/impl/integrations/files/file_data_source.py b/ldclient/impl/integrations/files/file_data_source.py index d02d5b28..9969125f 100644 --- a/ldclient/impl/integrations/files/file_data_source.py +++ b/ldclient/impl/integrations/files/file_data_source.py @@ -112,6 +112,10 @@ def _load_file(self, path, all_data): with open(path, 'r') as f: content = f.read() parsed = self._parse_content(content) + + if type(parsed) is not dict: + parsed = {} + for key, flag in parsed.get('flags', {}).items(): _sanitize_json_item(flag) self._add_item(all_data, FEATURES, flag) @@ -213,4 +217,5 @@ def _check_file_times(self): ret[path] = os.path.getmtime(path) except: ret[path] = None + return ret diff --git a/ldclient/testing/test_file_data_source.py b/ldclient/testing/test_file_data_source.py index 56da7762..90f96ef4 100644 --- a/ldclient/testing/test_file_data_source.py +++ b/ldclient/testing/test_file_data_source.py @@ -117,8 +117,8 @@ def make_temp_file(content): return path def replace_file(path, content): - with open(path, 'w') as f: - f.write(content) + temp_path = make_temp_file(content) + os.rename(temp_path, path) def test_does_not_load_data_prior_to_start(): path = make_temp_file('{"flagValues":{"key":"value"}}')