def test_read_file_fills_prefs(self, _mock_file): """read_file should populate the prefs object.""" fake_prefs = Preferences() fake_prefs.read_file("fake_filepath") value = fake_prefs.get_all_prefs() self.assertEqual(value, plistlib.loads(TEST_PLIST_PREFS)) self.assertEqual(fake_prefs.type, "plist")
def test_set_pref_mac(self, mock_set_macos_pref): """Preferences().set_pref should write macOS preference store on macOS.""" self.mock_platform.return_value = "Darwin" fake_prefs = Preferences() fake_prefs.set_pref("TEST_KEY", "fake_value") value = fake_prefs.get_pref("TEST_KEY") self.assertEqual(value, "fake_value") mock_set_macos_pref.assert_called()
def test_set_pref_no_file(self, mock_write_file, mock_set_macos_pref): """set_pref should change the prefs object, but not write when no file loaded""" fake_prefs = Preferences() fake_prefs.set_pref("TEST_KEY", "fake_value") mock_write_file.assert_not_called() mock_set_macos_pref.assert_not_called() value = fake_prefs.get_pref("TEST_KEY") self.assertEqual(value, "fake_value")
def test_init_prefs_files(self, _mock_open): """Preferences should load file-backed config on primary platforms.""" for platform in self.PRIMARY_NON_MACOS_PLATFORMS: with self.subTest(platform=platform): self.mock_platform.return_value = platform prefs = Preferences() self.assertNotEqual(prefs.file_path, None) value = prefs.get_all_prefs() self.assertEqual(value, json.loads(TEST_JSON_PREFS)) self.assertEqual(prefs.type, "json")
def test_new_prefs_object_is_empty(self): """A new Preferences object should be empty with no config.""" test_platforms = ["Darwin"] test_platforms += self.PRIMARY_NON_MACOS_PLATFORMS for platform in test_platforms: with self.subTest(platform=platform): self.mock_platform = platform fake_prefs = Preferences() self.assertEqual(fake_prefs.file_path, None) self.assertEqual(fake_prefs.type, None) self.assertEqual(fake_prefs.get_all_prefs(), {})
def test_set_pref_files(self, mock_write_file, mock_open): """Preferences().set_pref should write file on file-backed config platforms""" for platform in self.PRIMARY_NON_MACOS_PLATFORMS: with self.subTest(platform=platform): self.mock_platform.return_value = platform fake_prefs = Preferences() self.assertNotEqual(fake_prefs.file_path, None) fake_prefs.set_pref("TEST_KEY", "fake_value") mock_write_file.assert_called() value = fake_prefs.get_pref("TEST_KEY") self.assertEqual(value, "fake_value") mock_write_file.reset_mock()
def test_set_pref_mac_files(self, mock_open, mock_write_file, mock_set_macos_pref): """Preferences().set_pref should write file on macOS and read_file() used.""" self.mock_platform.return_value = "darwin" fake_prefs = Preferences() fake_prefs.read_file("fake_config_file") mock_open.assert_called() fake_prefs.set_pref("TEST_KEY", "fake_value") mock_write_file.assert_called() mock_set_macos_pref.assert_not_called() value = fake_prefs.get_pref("TEST_KEY") self.assertEqual(value, "fake_value")
def test_parse_file_is_empty_by_default(self): """Parsing a non-existent file should return an empty dict.""" fake_prefs = Preferences() value = fake_prefs._parse_json_or_plist_file("fake_filepath") self.assertEqual(value, {})
def test_get_macos_pref_returns_value(self): """get_macos_pref should return a value.""" self.mock_copyappvalue.return_value = "FakeValue" fake_prefs = Preferences() value = fake_prefs._get_macos_pref("fake") self.assertEqual(value, "FakeValue")
def test_parse_file_reads_plist(self, _mock_file): """Parsing a PList file should produce a dictionary.""" fake_prefs = Preferences() value = fake_prefs._parse_json_or_plist_file("fake_filepath") self.assertEqual(value, plistlib.loads(TEST_PLIST_PREFS))