def test_custom_root(self): """ Ensures that setting SHOTGUN_HOME overrides the local file storage default paths. """ # Here we will assume that the unit test framework already has set SHOTGUN_HOME. This # makes the code simpler, but also ensures that the sandboxing of the tests is not broken # by someone modifying how TankTestBase is initialized. for version in [ LocalFileStorageManager.CORE_V17, LocalFileStorageManager.CORE_V18 ]: self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE, version), self.tank_temp) self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.PREFERENCES, version), os.path.join(self.tank_temp, "preferences")) self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.PERSISTENT, version), os.path.join(self.tank_temp, "data")) self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.LOGGING, version), os.path.join(self.tank_temp, "logs"))
def test_legacy_global(self): """ tests the global root, 0.17 style """ cache_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE, LocalFileStorageManager.CORE_V17) persistent_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.CORE_V17) log_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.LOGGING, LocalFileStorageManager.CORE_V17) if sys.platform == "darwin": self.assertEqual(cache_path, os.path.expanduser("~/Library/Caches/Shotgun")) self.assertEqual( persistent_path, os.path.expanduser("~/Library/Application Support/Shotgun")) self.assertEqual(log_path, os.path.expanduser("~/Library/Logs/Shotgun")) elif sys.platform == "win32": app_data = os.environ.get("APPDATA", "APPDATA_NOT_SET") self.assertEqual(cache_path, os.path.join(app_data, "Shotgun")) self.assertEqual(persistent_path, os.path.join(app_data, "Shotgun")) self.assertEqual(log_path, os.path.join(app_data, "Shotgun")) else: # linux self.assertEqual(cache_path, os.path.expanduser("~/.shotgun")) self.assertEqual(persistent_path, os.path.expanduser("~/.shotgun")) self.assertEqual(log_path, os.path.expanduser("~/.shotgun"))
def test_legacy_global(self): """ tests the global root, 0.17 style """ cache_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE, LocalFileStorageManager.CORE_V17 ) persistent_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.CORE_V17 ) log_path = LocalFileStorageManager.get_global_root( LocalFileStorageManager.LOGGING, LocalFileStorageManager.CORE_V17 ) if sys.platform == "darwin": self.assertEqual(cache_path, os.path.expanduser("~/Library/Caches/Shotgun")) self.assertEqual(persistent_path, os.path.expanduser("~/Library/Application Support/Shotgun")) self.assertEqual(log_path, os.path.expanduser("~/Library/Logs/Shotgun")) elif sys.platform == "win32": app_data = os.environ.get("APPDATA", "APPDATA_NOT_SET") self.assertEqual(cache_path, os.path.join(app_data, "Shotgun")) self.assertEqual(persistent_path, os.path.join(app_data, "Shotgun")) self.assertEqual(log_path, os.path.join(app_data, "Shotgun")) else: # linux self.assertEqual(cache_path, os.path.expanduser("~/.shotgun")) self.assertEqual(persistent_path, os.path.expanduser("~/.shotgun")) self.assertEqual(log_path, os.path.expanduser("~/.shotgun"))
def test_global(self): """ tests the global root """ pref_path = LocalFileStorageManager.get_global_root(LocalFileStorageManager.PREFERENCES) cache_path = LocalFileStorageManager.get_global_root(LocalFileStorageManager.CACHE) persistent_path = LocalFileStorageManager.get_global_root(LocalFileStorageManager.PERSISTENT) log_path = LocalFileStorageManager.get_global_root(LocalFileStorageManager.LOGGING) if sys.platform == "darwin": self.assertEqual(cache_path, os.path.expanduser("~/Library/Caches/Shotgun")) self.assertEqual(pref_path, os.path.expanduser("~/Library/Preferences/Shotgun")) self.assertEqual(persistent_path, os.path.expanduser("~/Library/Application Support/Shotgun")) self.assertEqual(log_path, os.path.expanduser("~/Library/Logs/Shotgun")) elif sys.platform == "win32": app_data = os.environ.get("APPDATA", "APPDATA_NOT_SET") self.assertEqual(cache_path, os.path.join(app_data, "Shotgun")) self.assertEqual(pref_path, os.path.join(app_data, "Shotgun", "Preferences")) self.assertEqual(persistent_path, os.path.join(app_data, "Shotgun", "Data")) self.assertEqual(log_path, os.path.join(app_data, "Shotgun", "Logs")) else: # linux self.assertEqual(cache_path, os.path.expanduser("~/.shotgun")) self.assertEqual(pref_path, os.path.expanduser("~/.shotgun/preferences")) self.assertEqual(persistent_path, os.path.expanduser("~/.shotgun/data")) self.assertEqual(log_path, os.path.expanduser("~/.shotgun/logs"))
def test_custom_root(self): """ Ensures that setting SHOTGUN_HOME overrides the local file storage default paths. """ # Here we will assume that the unit test framework already has set SHOTGUN_HOME. This # makes the code simpler, but also ensures that the sandboxing of the tests is not broken # by someone modifying how TankTestBase is initialized. for version in [LocalFileStorageManager.CORE_V17, LocalFileStorageManager.CORE_V18]: self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE, version ), self.tank_temp ) self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.PREFERENCES, version ), os.path.join(self.tank_temp, "preferences") ) self.assertEqual( LocalFileStorageManager.get_global_root( LocalFileStorageManager.PERSISTENT, version ), os.path.join(self.tank_temp, "data") ) self.assertEqual( LocalFileStorageManager.get_global_root(LocalFileStorageManager.LOGGING, version), os.path.join(self.tank_temp, "logs") )
def test_url_cleanup(self): # Make sure that if a file has the url saved incorrectly... with patch("sgtk.util.shotgun.connection.sanitize_url", wraps=lambda x: x): session_cache.set_current_host("https://host.cleaned.up.on.read/") # ... then sure we indeed disabled cleanup and that the malformed value was written to disk... self.assertEquals("https://host.cleaned.up.on.read/", session_cache.get_current_host()) # ... and finaly that the value is filtered when being read back from disk. self.assertEquals("https://host.cleaned.up.on.read", session_cache.get_current_host()) # Make sure we're cleaning up the hostname when saving it. session_cache.set_current_host("https://host.cleaned.up.on.write/") with open( os.path.join( LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE ), "authentication.yml" ), "r" ) as fh: # Let's read the file directly to see if the data was cleaned up. data = yaml.load(fh) self.assertEqual( data["current_host"], "https://host.cleaned.up.on.write" )
def test_url_cleanup(self): # Make sure that if a file has the url saved incorrectly... with patch("sgtk.util.shotgun.connection.sanitize_url", wraps=lambda x: x): session_cache.set_current_host("https://host.cleaned.up.on.read/") # ... then sure we indeed disabled cleanup and that the malformed value was written to disk... self.assertEquals("https://host.cleaned.up.on.read/", session_cache.get_current_host()) # ... and finaly that the value is filtered when being read back from disk. self.assertEquals("https://host.cleaned.up.on.read", session_cache.get_current_host()) # Make sure we're cleaning up the hostname when saving it. session_cache.set_current_host("https://host.cleaned.up.on.write/") with open( os.path.join( LocalFileStorageManager.get_global_root( LocalFileStorageManager.CACHE), "authentication.yml"), "r") as fh: # Let's read the file directly to see if the data was cleaned up. data = yaml.load(fh) self.assertEqual(data["current_host"], "https://host.cleaned.up.on.write")
def test_get_site_cache_root(self): """ Tests site cache root """ for mode in [LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING]: site_path = LocalFileStorageManager.get_site_root("http://sg-internal", mode) global_path = LocalFileStorageManager.get_global_root(mode) self.assertEqual(os.path.dirname(site_path), global_path) self.assertEqual(os.path.basename(site_path), "sg-internal") site_path = LocalFileStorageManager.get_site_root("http://foo.int", mode) self.assertEqual(os.path.basename(site_path), "foo.int") site_path = LocalFileStorageManager.get_site_root("https://my-site.shotgunstudio.com", mode) self.assertEqual(os.path.basename(site_path), "my-site") legacy_site_path = LocalFileStorageManager.get_site_root("http://sg-internal", mode, LocalFileStorageManager.CORE_V17) legacy_global_path = LocalFileStorageManager.get_global_root(mode, LocalFileStorageManager.CORE_V17) self.assertEqual(os.path.dirname(legacy_site_path), legacy_global_path) self.assertEqual(os.path.basename(legacy_site_path), "sg-internal") legacy_site_path = LocalFileStorageManager.get_site_root( "http://foo.int", mode, LocalFileStorageManager.CORE_V17 ) self.assertEqual(os.path.basename(legacy_site_path), "foo.int") legacy_site_path = LocalFileStorageManager.get_site_root( "https://my-site.shotgunstudio.com", mode, LocalFileStorageManager.CORE_V17 ) self.assertEqual(os.path.basename(legacy_site_path), "my-site.shotgunstudio.com") # @ todo - add tests for project/ pipleine config level methods
def test_get_cache_root(self): """ Tests get_cache_root """ p = LocalFileStorageManager.get_global_root(LocalFileStorageManager.CACHE) if sys.platform == "win32": self.assertEqual(p, os.path.join(os.environ["APPDATA"], "Shotgun")) if sys.platform == "darwin": self.assertEqual(p, os.path.expanduser("~/Library/Caches/Shotgun")) if sys.platform == "linux2": self.assertEqual(p, os.path.expanduser("~/.shotgun")) p = LocalFileStorageManager.get_global_root(LocalFileStorageManager.CACHE, LocalFileStorageManager.CORE_V17) if sys.platform == "win32": self.assertEqual(p, os.path.join(os.environ["APPDATA"], "Shotgun")) if sys.platform == "darwin": self.assertEqual(p, os.path.expanduser("~/Library/Caches/Shotgun")) if sys.platform == "linux2": self.assertEqual(p, os.path.expanduser("~/.shotgun"))
def test_get_data_root(self): """ Tests get_cache_root """ p = LocalFileStorageManager.get_global_root(LocalFileStorageManager.PERSISTENT) if sys.platform == "win32": self.assertEqual(p, os.path.join(os.environ["APPDATA"], "Shotgun", "Data")) if sys.platform == "darwin": self.assertEqual(p, os.path.expanduser("~/Library/Application Support/Shotgun")) if sys.platform == "linux2": self.assertEqual(p, os.path.expanduser("~/.shotgun/data")) p = LocalFileStorageManager.get_global_root(LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.CORE_V17) if sys.platform == "win32": self.assertEqual(p, os.path.join(os.environ["APPDATA"], "Shotgun")) if sys.platform == "darwin": self.assertEqual(p, os.path.expanduser("~/Library/Application Support/Shotgun")) if sys.platform == "linux2": self.assertEqual(p, os.path.expanduser("~/.shotgun"))
def test_env_var_and_tilde(self): """ Makes sure environment variables and ~ are expanded """ try: # Create an environment variable that uses a ~. os.environ["SHOTGUN_HOME_UNIT_TEST"] = os.path.join("~", "test") # Set the SHOTGUN_HOME to use that environment variable. os.environ["SHOTGUN_HOME"] = "$SHOTGUN_HOME_UNIT_TEST" # Make sure that both the environment variable and the ~ have been expanded. self.assertEqual( LocalFileStorageManager.get_global_root(LocalFileStorageManager.PREFERENCES), os.path.join(os.path.expanduser(os.environ["SHOTGUN_HOME_UNIT_TEST"]), "preferences") ) finally: os.environ["SHOTGUN_HOME_UNIT_TEST"]
def test_site(self): """ tests the site config root """ with self.assertRaises(TankError): LocalFileStorageManager.get_site_root( None, LocalFileStorageManager.CACHE) new_path_types = [ LocalFileStorageManager.PREFERENCES, LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING ] for path_type in new_path_types: cache_path = LocalFileStorageManager.get_site_root( "https://test.shotgunstudio.com", path_type) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "test")) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "shotgun")) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "shotgun.int")) old_path_types = [ LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING ] for path_type in old_path_types: cache_path = LocalFileStorageManager.get_site_root( "https://test.shotgunstudio.com", path_type, LocalFileStorageManager.CORE_V17) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17), "test.shotgunstudio.com")) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type, LocalFileStorageManager.CORE_V17) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17), "shotgun")) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type, LocalFileStorageManager.CORE_V17) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17), "shotgun.int"))
def test_site(self): """ tests the site config root """ with self.assertRaises(TankError): LocalFileStorageManager.get_site_root(None, LocalFileStorageManager.CACHE) new_path_types = [ LocalFileStorageManager.PREFERENCES, LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING, ] hosted_sg_sites = ["test.shotgunstudio.com", "test.shotgrid.autodesk.com"] for path_type in new_path_types: for site_name in hosted_sg_sites: cache_path = LocalFileStorageManager.get_site_root( "https://{}".format(site_name), path_type ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "test" ), ) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "shotgun" ), ) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type), "shotgun.int" ), ) old_path_types = [ LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING, ] for path_type in old_path_types: for site_name in hosted_sg_sites: cache_path = LocalFileStorageManager.get_site_root( "https://{}".format(site_name), path_type, LocalFileStorageManager.CORE_V17, ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17 ), site_name, ), ) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type, LocalFileStorageManager.CORE_V17 ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17 ), "shotgun", ), ) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type, LocalFileStorageManager.CORE_V17 ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17 ), "shotgun.int", ), )
def test_site(self): """ tests the site config root """ with self.assertRaises(TankError): LocalFileStorageManager.get_site_root(None, LocalFileStorageManager.CACHE) new_path_types = [ LocalFileStorageManager.PREFERENCES, LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING ] for path_type in new_path_types: cache_path = LocalFileStorageManager.get_site_root( "https://test.shotgunstudio.com", path_type ) self.assertEqual( cache_path, os.path.join(LocalFileStorageManager.get_global_root(path_type), "test") ) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type ) self.assertEqual( cache_path, os.path.join(LocalFileStorageManager.get_global_root(path_type), "shotgun") ) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type ) self.assertEqual( cache_path, os.path.join(LocalFileStorageManager.get_global_root(path_type), "shotgun.int") ) old_path_types = [ LocalFileStorageManager.CACHE, LocalFileStorageManager.PERSISTENT, LocalFileStorageManager.LOGGING ] for path_type in old_path_types: cache_path = LocalFileStorageManager.get_site_root( "https://test.shotgunstudio.com", path_type, LocalFileStorageManager.CORE_V17 ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type, LocalFileStorageManager.CORE_V17), "test.shotgunstudio.com" ) ) cache_path = LocalFileStorageManager.get_site_root( "http://shotgun", path_type, LocalFileStorageManager.CORE_V17 ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root(path_type, LocalFileStorageManager.CORE_V17), "shotgun" ) ) cache_path = LocalFileStorageManager.get_site_root( "https://shotgun.int", path_type, LocalFileStorageManager.CORE_V17 ) self.assertEqual( cache_path, os.path.join( LocalFileStorageManager.get_global_root( path_type, LocalFileStorageManager.CORE_V17 ), "shotgun.int" ) )