enthought / apptools

Other
37 stars 24 forks source link

Preferences written on helper creation #342

Closed mdickinson closed 3 months ago

mdickinson commented 4 months ago

Preferences are currently written the first time a PreferencesHelper for those preferences is created. This leads to some surprising behaviour, especially in scoped preferences. Here's an example test case that currently fails:

    def test_preferences_not_written_on_helper_creation(self):

        class AppPreferencesHelper(PreferencesHelper):
            #: The node that contains the preferences.
            preferences_path = "app"

            #: The user's favourite colour
            color = Str()

        default_preferences = Preferences(name="default")
        default_preferences.set("app.color", "red")

        application_preferences = Preferences(name="application")
        preferences = ScopedPreferences(
            scopes=[application_preferences, default_preferences]
        )
        self.assertIsNone(application_preferences.get("app.color"))

        # Then creation of the helper should not cause the application
        # preferences to change.
        AppPreferencesHelper(preferences=preferences)
        self.assertIsNone(application_preferences.get("app.color"))

This test case fails at the second self.assertIsNotNone line: the preferences from the default scope have been written into the application scope.