def test_bad_environment_variable(self): """ Test environment variables being set to files that don't exist. """ with patch.dict(os.environ, {"SGTK_PREFERENCES_LOCATION": "/a/b/c"}): with self.assertRaisesRegex(EnvironmentVariableFileLookupError, "/a/b/c"): UserSettings() with patch.dict(os.environ, {"SGTK_DESKTOP_CONFIG_LOCATION": "/d/e/f"}): with self.assertRaisesRegex(EnvironmentVariableFileLookupError, "/d/e/f"): UserSettings()
def test_boolean_setting(self): """ Tests that we can read a setting into a boolean. """ self.write_toolkit_ini_file(Custom={"valid": "ON", "invalid": "L"}) self.assertEqual(UserSettings().get_boolean_setting("Custom", "valid"), True) with self.assertRaisesRegex( TankError, "Invalid value 'L' in '.*' for setting 'invalid' in section 'Custom': expecting one of .*.", ): UserSettings().get_boolean_setting("Custom", "invalid")
def write_toolkit_ini_file(self, login_section={}, **kwargs): """ Creates an ini file in a unique location with the user settings. :param login_section: Dictionary of settings that will be stored in the [Login] section. :param **kwargs: Dictionary where the key is a section name and the value is a dictionary of the settings for that section. :returns: Path to the Toolkit ini file. """ # Create a unique folder for this test. folder = os.path.join(self.tank_temp, str(uuid.uuid4())) os.makedirs(folder) # Manually write the file as this is the format we're expecting the UserSettings # to parse. ini_file_location = os.path.join(folder, "toolkit.ini") with open(ini_file_location, "w") as f: f.writelines(["[Login]\n"]) for key, value in login_section.iteritems(): f.writelines(["%s=%s\n" % (key, value)]) for section in kwargs: f.writelines(["[%s]\n" % section]) for key, value in kwargs[section].iteritems(): f.writelines(["%s=%s\n" % (key, value)]) # The setUp phase cleared the singleton. So set the preferences environment variable and # instantiate the singleton, which will read the env var and open that location. with mock.patch.dict(os.environ, {"SGTK_PREFERENCES_LOCATION": ini_file_location}): UserSettings() return ini_file_location
def test_app_store_to_none(self, mock): """ Tests a file with a present but empty app store proxy setting. """ settings = UserSettings() self.assertTrue(settings.is_app_store_proxy_set()) self.assertEqual(settings.app_store_proxy, None)
def test_settings_enumeration(self): """ Tests that we can enumerate settings from a section. """ self.write_toolkit_ini_file({ "this": "is", "my": "boomstick" }, Custom={}) self.assertEqual( UserSettings().get_section_settings("missing section"), None) self.assertEqual(UserSettings().get_section_settings("Custom"), []) self.assertEqual(UserSettings().get_section_settings("Login"), ["this", "my"])
def test_empty_file(self): """ Tests a complete yaml file. """ settings = UserSettings() self.assertIsNone(settings.default_site) self.assertIsNone(settings.default_login) self.assertIsNone(settings.shotgun_proxy) self.assertIsNone(settings.app_store_proxy)
def test_environment_variable_expansions(self, mock): """ Tests that setting an environment variable will be resolved. """ with patch.dict(os.environ, {"SGTK_TEST_SHOTGUN_SITE": "shotgun_site"}): settings = UserSettings() self.assertEqual(settings.default_site, "https://shotgun_site.shotgunstudio.com")
def test_system_proxy(self, mock): """ Tests the fallback on the operating system http proxy. """ http_proxy = "foo:[email protected]:80" # IP address of shotgunstudio.com with patch.dict(os.environ, {"http_proxy": "http://" + http_proxy}): settings = UserSettings() self.assertEqual(settings.shotgun_proxy, http_proxy)
def test_custom_settings(self): """ Tests that we can read settings in any section of the file. """ self.write_toolkit_ini_file(Custom={"custom_key": "custom_value"}) self.assertEqual(UserSettings().get_setting("Custom", "custom_key"), "custom_value")
def test_filled_file(self, mock): """ Tests a complete yaml file. """ settings = UserSettings() self.assertEqual(settings.default_site, "site") self.assertEqual(settings.default_login, "login") self.assertEqual(settings.shotgun_proxy, "http_proxy") self.assertTrue(settings.is_app_store_proxy_set()) self.assertEqual(settings.app_store_proxy, "app_store_http_proxy")
def test_environment_variable_expansions(self): """ Tests that setting an environment variable will be resolved. """ self.write_toolkit_ini_file({ # Config parser represent empty settings as empty strings "default_site": "https://${SGTK_TEST_SHOTGUN_SITE}.shotgunstudio.com" }) with patch.dict(os.environ, {"SGTK_TEST_SHOTGUN_SITE": "shotgun_site"}): settings = UserSettings() self.assertEqual(settings.default_site, "https://shotgun_site.shotgunstudio.com")
def test_integer_setting(self): """ Tests that we can read a setting into an integer """ self.write_toolkit_ini_file(Custom={ "valid": "1", "also_valid": "-1", "invalid": "L" }) self.assertEqual(UserSettings().get_integer_setting("Custom", "valid"), 1) self.assertEqual( UserSettings().get_integer_setting("Custom", "also_valid"), -1) with self.assertRaisesRegex( TankError, "Invalid value 'L' in '.*' for setting 'invalid' in section 'Custom': expecting integer.", ): UserSettings().get_integer_setting("Custom", "invalid")
def test_empty_settings(self): """ Tests a yaml file with the settings present but empty. """ self.write_toolkit_ini_file({ "default_site": "", "default_login": "", "http_proxy": "", "app_store_http_proxy": "" }) settings = UserSettings() self.assertEqual(settings.default_site, "") self.assertEqual(settings.default_login, "") self.assertEqual(settings.shotgun_proxy, "") self.assertEqual(settings.app_store_proxy, "")
def test_filled_file(self): """ Tests a complete yaml file. """ self.write_toolkit_ini_file({ "default_site": "site", "default_login": "******", "http_proxy": "http_proxy", "app_store_http_proxy": "app_store_http_proxy" }) settings = UserSettings() self.assertEqual(settings.default_site, "site") self.assertEqual(settings.default_login, "login") self.assertEqual(settings.shotgun_proxy, "http_proxy") self.assertEqual(settings.app_store_proxy, "app_store_http_proxy")