diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 701f8a5..dfc22a1 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -34,7 +34,7 @@ jobs: run: pipenv run pytest -v --cov=./switcher_client --cov-report xml --cov-config=.coveragerc - name: SonarCloud Scan - uses: sonarsource/sonarqube-scan-action@v7.1.0 + uses: sonarsource/sonarqube-scan-action@v8.0.0 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} if: env.SONAR_TOKEN != '' diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 1c20a3e..85bf3d5 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -47,7 +47,7 @@ jobs: run: pipenv run pytest -v --cov=./switcher_client --cov-report xml - name: SonarCloud Scan - uses: sonarsource/sonarqube-scan-action@v7.1.0 + uses: sonarsource/sonarqube-scan-action@v8.0.0 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} if: env.SONAR_TOKEN != '' diff --git a/README.md b/README.md index bee36a0..8bc0051 100644 --- a/README.md +++ b/README.md @@ -288,13 +288,23 @@ switcher.remote().is_on('FEATURE01') ### Loading Snapshots -Load configuration snapshots from the API for local/offline usage: +Load snapshots from the API or local files: ```python -# Download and save snapshot from API +# Load snapshot from local file Client.load_snapshot() ``` +```python +# Load snapshot from API with fetch_remote option +Client.load_snapshot(LoadSnapshotOptions(fetch_remote=True)) +``` + +```python +# Load snapshot with watch option to update the client in real-time when file changes +Client.load_snapshot(LoadSnapshotOptions(watch_snapshot=True)) +``` + ### Version Management Check your current snapshot version: diff --git a/switcher_client/client.py b/switcher_client/client.py index 3cec386..0e07862 100644 --- a/switcher_client/client.py +++ b/switcher_client/client.py @@ -141,6 +141,9 @@ def load_snapshot(options: Optional[LoadSnapshotOptions] = None) -> int: if Client._is_check_snapshot_available(snapshot_options.fetch_remote): Client.check_snapshot() + if snapshot_options.watch_snapshot: + Client.watch_snapshot() + return Client.snapshot_version() @staticmethod diff --git a/tests/test_client_watch_snapshot.py b/tests/test_client_watch_snapshot.py index 8023586..434fab8 100644 --- a/tests/test_client_watch_snapshot.py +++ b/tests/test_client_watch_snapshot.py @@ -6,6 +6,7 @@ from switcher_client.client import Client, ContextOptions from switcher_client.lib.snapshot_watcher import SnapshotWatcher, WatchSnapshotCallback +from switcher_client.lib.globals.global_snapshot import LoadSnapshotOptions context_options_local = ContextOptions(local=True, snapshot_location='tests/snapshots/temp') async_error = None @@ -56,6 +57,31 @@ def test_watch_snapshot(self): else: print("Warning: Snapshot watcher did not detect the change within the time limit") + def test_watch_snapshot_from_load_snapshot(self): + """ Should watch the snapshot file when load_snapshot is called with watch_snapshot option """ + + fixture_env = 'default_load_1' + fixture_env_file_modified = 'tests/snapshots/default_load_2.json' + fixture_location = 'tests/snapshots/temp' + + # given + modify_fixture_snapshot(fixture_location, fixture_env, f'tests/snapshots/{fixture_env}.json') + given_context(options=context_options_local, environment=fixture_env) + + # test + Client.load_snapshot(LoadSnapshotOptions(watch_snapshot=True)) + switcher = Client.get_switcher('FF2FOR2030') + + assert switcher.is_on() + modify_fixture_snapshot(fixture_location, fixture_env, fixture_env_file_modified) + + # then + verified = verify_util(30, lambda: switcher.is_on() == False) + if verified: + assert switcher.is_on() == False + else: + print("Warning: Snapshot watcher did not detect the change within the time limit") + def test_watch_snapshot_err_no_snapshot_location(self): """ Should reject with error when snapshot location is not defined in the context options """