def test_as_descriptor_uri(self): """ Test the descriptor URI method """ empty_path = ShotgunPath() self.assertRaises(ValueError, empty_path.as_descriptor_uri) mac_only = ShotgunPath(macosx_path="/foo/bar") self.assertEqual( mac_only.as_descriptor_uri(), "sgtk:descriptor:path?mac_path=/foo/bar" ) # dev flag self.assertEqual( mac_only.as_descriptor_uri(for_development=True), "sgtk:descriptor:dev?mac_path=/foo/bar" ) # full path and escaping full_path = ShotgunPath(macosx_path="/foo/bar", windows_path="C:\\foo\\bar", linux_path="/foo bar/baz") self.assertEqual( full_path.as_descriptor_uri(), "sgtk:descriptor:path?linux_path=/foo%20bar/baz&mac_path=/foo/bar&windows_path=C:\\foo\\bar" )
def test_shotgun(self): """ Tests site cache root """ p1 = ShotgunPath("C:\\temp", "/tmp") self.assertEqual(p1.as_shotgun_dict(), {"windows_path": "C:\\temp", "linux_path": "/tmp", "mac_path": None}) self.assertEqual(p1.as_shotgun_dict(include_empty=False), {"windows_path": "C:\\temp", "linux_path": "/tmp"})
def test_storage_roots_get_local_storages(self): """Test the get_local_storages method.""" single_root = StorageRoots.from_metadata(self._single_root_metadata) (single_root_lookup, unmapped_roots) = single_root.get_local_storages(self.mockgun) self.assertTrue("primary" in single_root_lookup) single_root_default = single_root_lookup["primary"] self.assertEqual(single_root_default["code"], "primary") self.assertEqual(single_root_default["type"], "LocalStorage") self.assertEqual(single_root_default["id"], 1) single_root_storage_paths = single_root.as_shotgun_paths["primary"] self.assertEqual(single_root_storage_paths, ShotgunPath.from_shotgun_dict(single_root_default)) self.assertEqual(unmapped_roots, []) multiple_roots = StorageRoots.from_metadata(self._multiple_roots_metadata) (multiple_root_lookup, unmapped_roots) = multiple_roots.get_local_storages(self.mockgun) self.assertTrue("work" in multiple_root_lookup) multiple_roots_default = multiple_root_lookup["work"] self.assertEqual(multiple_roots_default["code"], "work") self.assertEqual(multiple_roots_default["type"], "LocalStorage") self.assertEqual(multiple_roots_default["id"], 2) multiple_roots_storage_paths = multiple_roots.as_shotgun_paths["work"] self.assertEqual(multiple_roots_storage_paths, ShotgunPath.from_shotgun_dict(multiple_roots_default)) self.assertEqual(unmapped_roots, ["foobar"])
def test_storage_roots_get_local_storages(self): """Test the get_local_storages method.""" single_root = StorageRoots.from_metadata(self._single_root_metadata) (single_root_lookup, unmapped_roots) = single_root.get_local_storages(self.mockgun) self.assertTrue("primary" in single_root_lookup) single_root_default = single_root_lookup["primary"] self.assertEqual(single_root_default["code"], "primary") self.assertEqual(single_root_default["type"], "LocalStorage") self.assertEqual(single_root_default["id"], 1) single_root_storage_paths = single_root.as_shotgun_paths["primary"] self.assertEqual(single_root_storage_paths, ShotgunPath.from_shotgun_dict(single_root_default)) self.assertEqual(unmapped_roots, []) multiple_roots = StorageRoots.from_metadata( self._multiple_roots_metadata) (multiple_root_lookup, unmapped_roots) = multiple_roots.get_local_storages(self.mockgun) self.assertTrue("work" in multiple_root_lookup) multiple_roots_default = multiple_root_lookup["work"] self.assertEqual(multiple_roots_default["code"], "work") self.assertEqual(multiple_roots_default["type"], "LocalStorage") self.assertEqual(multiple_roots_default["id"], 2) multiple_roots_storage_paths = multiple_roots.as_shotgun_paths["work"] self.assertEqual(multiple_roots_storage_paths, ShotgunPath.from_shotgun_dict(multiple_roots_default)) self.assertEqual(unmapped_roots, ["foobar"])
def test_normalize(self): """ Tests get_shotgun_storage_key """ if sys.platform == "win32": self.assertEqual(ShotgunPath.normalize("C:/foo\\bar\\"), r"C:\foo\bar") else: self.assertEqual(ShotgunPath.normalize("/foo\\bar/"), "/foo/bar")
def test_truthiness(self): """ Tests that a ShotgunPath that has no path set evaluates to False. """ self.assertFalse(bool(ShotgunPath())) self.assertTrue(bool(ShotgunPath(windows_path="abc"))) self.assertTrue(bool(ShotgunPath(linux_path="abc"))) self.assertTrue(bool(ShotgunPath(macosx_path="abc")))
def test_normalize(self): """ Tests get_shotgun_storage_key """ if is_windows(): self.assertEqual(ShotgunPath.normalize("C:/foo\\bar\\"), r"C:\foo\bar") else: self.assertEqual(ShotgunPath.normalize("/foo\\bar/"), "/foo/bar")
def test_property_access(self): """ Test getters and setters """ # check that setters sanitize the input std_constructor = ShotgunPath() std_constructor.windows = "C:\\temp\\" std_constructor.macosx = "/tmp/" std_constructor.linux = "/tmp2/" self.assertEqual(std_constructor.windows, "C:\\temp") self.assertEqual(std_constructor.macosx, "/tmp") self.assertEqual(std_constructor.linux, "/tmp2")
def test_hashing(self): """ Ensures two ShotgunPath with the same path generate the same hash. """ first = ShotgunPath("/a/b/c", None, None) second = ShotgunPath("/a/b/c", None, None) self.assertEqual(first, second) self.assertEqual(hash(first), hash(second)) first = ShotgunPath("/a/b/c", None, None) second = ShotgunPath("/a/b/d", None, None) self.assertNotEqual(first, second) self.assertNotEqual(hash(first), hash(second))
def test_shotgun(self): """ Tests site cache root """ p1 = ShotgunPath("C:\\temp", "/tmp") self.assertEqual(p1.as_shotgun_dict(), { "windows_path": "C:\\temp", "linux_path": "/tmp", "mac_path": None }) self.assertEqual(p1.as_shotgun_dict(include_empty=False), { "windows_path": "C:\\temp", "linux_path": "/tmp" })
def test_equality(self): """ Tests site cache root """ p1 = ShotgunPath("C:\\temp", "/tmp", "/tmp2") p2 = p1 p3 = ShotgunPath("C:\\temp", "/tmp", "/tmp2") self.assertEqual(p1, p2) self.assertEqual(p1, p3) self.assertEqual(p3, p2) p4 = ShotgunPath("C:\\temp", "/tmp") self.assertNotEqual(p1, p4)
def test_as_descriptor_uri(self): """ Test the descriptor URI method """ empty_path = ShotgunPath() self.assertRaises(ValueError, empty_path.as_descriptor_uri) mac_only = ShotgunPath(macosx_path="/foo/bar") self.assertEqual(mac_only.as_descriptor_uri(), "sgtk:descriptor:path?mac_path=/foo/bar") # dev flag self.assertEqual( mac_only.as_descriptor_uri(for_development=True), "sgtk:descriptor:dev?mac_path=/foo/bar", ) # full path and escaping full_path = ShotgunPath( macosx_path="/foo/bar", windows_path="C:\\foo\\bar", linux_path="/foo bar/baz", ) self.assertEqual( full_path.as_descriptor_uri(), "sgtk:descriptor:path?linux_path=/foo%20bar/baz&mac_path=/foo/bar&windows_path=C:\\foo\\bar", )
def test_token_resolution_with_installed_configuration(self): """ Tests an installed configuration's resolving of the CONFIG_FOLDER and PIPELINE_CONFIG """ self.setup_fixtures(parameters={"installed_config": True}) # For path and the platform specific path token... for path_key in ["path", ShotgunPath.get_shotgun_storage_key()]: # For both path based descriptors.. for desc_type in ["path", "dev"]: # For both tokens that can point to the bunldes that have been copied inside the # pipeline configuration... for desc_str in [ "sgtk:descriptor:%s?%s={PIPELINE_CONFIG}/config/bundles/test_app" % (desc_type, path_key), "sgtk:descriptor:%s?%s={CONFIG_FOLDER}/bundles/test_app" % (desc_type, path_key) ]: desc = self.tk.pipeline_configuration.get_app_descriptor( desc_str) # Ensure the bundle is resolved inside the installed configuration. self.assertEqual( desc.get_path(), os.path.join(self.pipeline_config_root, "config", "bundles", "test_app"))
def test_storage_roots_default_path(self): """Test the default_path property.""" single_root = StorageRoots.from_config(self._single_root_config_folder) single_root_default_path = ShotgunPath.from_shotgun_dict( self._single_root_metadata["primary"]) self.assertTrue(single_root.default_path, single_root_default_path) multiple_roots = StorageRoots.from_config( self._multiple_roots_config_folder) multiple_roots_default_path = ShotgunPath.from_shotgun_dict( self._multiple_roots_metadata["work"]) self.assertTrue(multiple_roots.default_path, multiple_roots_default_path) no_roots = StorageRoots.from_config(self._no_roots_config_folder) self.assertEqual(no_roots.default_path, None)
def test_storage_roots_default_path(self): """Test the default_path property.""" single_root = StorageRoots.from_config(self._single_root_config_folder) single_root_default_path = ShotgunPath.from_shotgun_dict( self._single_root_metadata["primary"]) self.assertTrue(single_root.default_path, single_root_default_path) multiple_roots = StorageRoots.from_config(self._multiple_roots_config_folder) multiple_roots_default_path = ShotgunPath.from_shotgun_dict( self._multiple_roots_metadata["work"]) self.assertTrue(multiple_roots.default_path, multiple_roots_default_path) empty_roots = StorageRoots.from_config(self._empty_roots_config_folder) self.assertEqual(empty_roots.default_path, None) no_roots = StorageRoots.from_config(self._no_roots_config_folder) self.assertEqual(no_roots.default_path, None)
def test_sanitize(self): """ Tests site cache root """ std_constructor = ShotgunPath("C:\\temp\\", "/tmp/", "/tmp2/") self.assertEqual(std_constructor.windows, "C:\\temp") self.assertEqual(std_constructor.macosx, "/tmp2") self.assertEqual(std_constructor.linux, "/tmp") std_constructor = ShotgunPath("C:/temp/", "///tmp//", "//tmp2/") self.assertEqual(std_constructor.windows, "C:\\temp") self.assertEqual(std_constructor.macosx, "/tmp2") self.assertEqual(std_constructor.linux, "/tmp") std_constructor = ShotgunPath("C:\\", "///tmp//", "//tmp2/") self.assertEqual(std_constructor.windows, "C:\\") self.assertEqual(std_constructor.macosx, "/tmp2") self.assertEqual(std_constructor.linux, "/tmp") # test raw sanitize logic sp = std_constructor._sanitize_path self.assertEqual(sp("/foo/bar/baz", "/"), "/foo/bar/baz") self.assertEqual(sp("/foo/bar/baz/", "/"), "/foo/bar/baz") self.assertEqual(sp("//foo//bar//baz", "/"), "/foo/bar/baz") self.assertEqual(sp("/foo/bar//baz", "/"), "/foo/bar/baz") self.assertEqual(sp("/foo\\bar//baz/////", "/"), "/foo/bar/baz") self.assertEqual(sp("/foo/bar/baz", "\\"), "\\foo\\bar\\baz") self.assertEqual(sp("c:/foo/bar/baz", "\\"), "c:\\foo\\bar\\baz") self.assertEqual(sp("c:/foo///bar\\\\baz//", "\\"), "c:\\foo\\bar\\baz") self.assertEqual(sp("/foo///bar\\\\baz//", "\\"), "\\foo\\bar\\baz") self.assertEqual(sp("\\\\server\\share\\foo\\bar", "\\"), "\\\\server\\share\\foo\\bar") self.assertEqual(sp("\\\\server\\share\\foo\\bar\\", "\\"), "\\\\server\\share\\foo\\bar") self.assertEqual(sp("//server/share/foo//bar", "\\"), "\\\\server\\share\\foo\\bar") self.assertEqual(sp("z:/", "\\"), "z:\\") self.assertEqual(sp("z:\\", "\\"), "z:\\") self.assertEqual(sp(None, "/"), None)
def test_join(self): """ Tests site cache root """ p1 = ShotgunPath("C:\\temp", "/linux", "/mac") p2 = p1.join("foo") p3 = p2.join("bar") self.assertEqual(p1.windows, "C:\\temp") self.assertEqual(p1.macosx, "/mac") self.assertEqual(p1.linux, "/linux") self.assertEqual(p2.windows, "C:\\temp\\foo") self.assertEqual(p2.macosx, "/mac/foo") self.assertEqual(p2.linux, "/linux/foo") self.assertEqual(p3.windows, "C:\\temp\\foo\\bar") self.assertEqual(p3.macosx, "/mac/foo/bar") self.assertEqual(p3.linux, "/linux/foo/bar")
def _create_core_file(self, config_root, path): """ Creates a core file in a configuration. :param str config_root_name: Path to the configuration root. :param str path: Path to write in the interpreter file. """ self.create_file( ShotgunPath.get_file_name_from_template( os.path.join(config_root, "install", "core", "core_%s.cfg")), path)
def test_current_platform_file(self): """ Ensures the get_file_name_from_template subtitutes the OS name correctly. """ self.assertEqual( ShotgunPath.get_file_name_from_template(r"C:\%s.yml", "win32"), r"C:\Windows.yml" ) self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "linux2"), "/Linux.yml" ) self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "linux3"), "/Linux.yml" ) self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "darwin"), "/Darwin.yml" ) with self.assertRaisesRegexp( ValueError, "Cannot resolve file name - unsupported os platform 'potato'" ): ShotgunPath.get_file_name_from_template("/%s.yml", "potato")
def test_current_platform_file(self): """ Ensures the get_file_name_from_template subtitutes the OS name correctly. """ self.assertEqual( ShotgunPath.get_file_name_from_template(r"C:\%s.yml", "win32"), r"C:\Windows.yml", ) self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "linux2"), "/Linux.yml") self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "linux3"), "/Linux.yml") self.assertEqual( ShotgunPath.get_file_name_from_template("/%s.yml", "darwin"), "/Darwin.yml") with self.assertRaisesRegex( ValueError, "Cannot resolve file name - unsupported os platform 'potato'"): ShotgunPath.get_file_name_from_template("/%s.yml", "potato")
def test_token_resolution_with_cached_configuration(self): """ Tests a cached configuration's resolving of the CONFIG_FOLDER """ self.setup_fixtures() # For path and the platform specific path token... for path_key in ["path", ShotgunPath.get_shotgun_storage_key()]: # For both path based descriptors... for desc_type in ["path", "dev"]: desc_str = "sgtk:descriptor:%s?%s={CONFIG_FOLDER}/bundles/test_app" % (desc_type, path_key) desc = self.tk.pipeline_configuration.get_app_descriptor(desc_str) # Ensure the bundle is resolved inside the source configuration. self.assertEqual( desc.get_path(), os.path.join(self.fixtures_root, "config", "bundles", "test_app") )
def _create_core_file(self, config_root, path): """ Creates a core file in a configuration. :param str config_root_name: Path to the configuration root. :param str path: Path to write in the interpreter file. """ self.create_file( ShotgunPath.get_file_name_from_template( os.path.join( config_root, "install", "core", "core_%s.cfg" ) ), path )
def test_token_resolution_with_cached_configuration(self): """ Tests a cached configuration's resolving of the CONFIG_FOLDER """ self.setup_fixtures() # For path and the platform specific path token... for path_key in ["path", ShotgunPath.get_shotgun_storage_key()]: # For both path based descriptors... for desc_type in ["path", "dev"]: desc_str = "sgtk:descriptor:%s?%s={CONFIG_FOLDER}/bundles/test_app" % ( desc_type, path_key) desc = self.tk.pipeline_configuration.get_app_descriptor( desc_str) # Ensure the bundle is resolved inside the source configuration. self.assertEqual( desc.get_path(), os.path.join(self.fixtures_root, "config", "bundles", "test_app"))
def test_token_resolution_with_installed_configuration(self): """ Tests an installed configuration's resolving of the CONFIG_FOLDER and PIPELINE_CONFIG """ self.setup_fixtures(parameters={"installed_config": True}) # For path and the platform specific path token... for path_key in ["path", ShotgunPath.get_shotgun_storage_key()]: # For both path based descriptors.. for desc_type in ["path", "dev"]: # For both tokens that can point to the bunldes that have been copied inside the # pipeline configuration... for desc_str in [ "sgtk:descriptor:%s?%s={PIPELINE_CONFIG}/config/bundles/test_app" % (desc_type, path_key), "sgtk:descriptor:%s?%s={CONFIG_FOLDER}/bundles/test_app" % (desc_type, path_key) ]: desc = self.tk.pipeline_configuration.get_app_descriptor(desc_str) # Ensure the bundle is resolved inside the installed configuration. self.assertEqual( desc.get_path(), os.path.join(self.pipeline_config_root, "config", "bundles", "test_app") )
def test_construction(self): """ Tests get_cache_root """ self.assertEqual(ShotgunPath.SHOTGUN_PATH_FIELDS, ["windows_path", "linux_path", "mac_path"]) sg = ShotgunPath.from_shotgun_dict({ "windows_path": "C:\\temp", "mac_path": "/tmp", "linux_path": "/tmp2", "foo": "bar" }) self.assertEqual(sg.windows, "C:\\temp") self.assertEqual(sg.macosx, "/tmp") self.assertEqual(sg.linux, "/tmp2") sg = ShotgunPath.from_shotgun_dict({ "windows_path": "C:\\temp", "mac_path": None, "foo": "bar" }) self.assertEqual(sg.windows, "C:\\temp") self.assertEqual(sg.macosx, None) self.assertEqual(sg.linux, None) sys_paths = ShotgunPath.from_system_dict({ "win32": "C:\\temp", "darwin": "/tmp", "linux2": "/tmp2", "foo": "bar" }) self.assertEqual(sys_paths.windows, "C:\\temp") self.assertEqual(sys_paths.macosx, "/tmp") self.assertEqual(sys_paths.linux, "/tmp2") sys_paths = ShotgunPath.from_system_dict({ "win32": "C:\\temp", "darwin": None, "foo": "bar" }) self.assertEqual(sys_paths.windows, "C:\\temp") self.assertEqual(sys_paths.macosx, None) self.assertEqual(sys_paths.linux, None) if sys.platform == "win32": curr = ShotgunPath.from_current_os_path("\\\\server\\mount\\path") self.assertEqual(curr.windows, "\\\\server\\mount\\path") self.assertEqual(curr.macosx, None) self.assertEqual(curr.linux, None) self.assertEqual(curr.current_os, curr.windows) if sys.platform == "linux2": curr = ShotgunPath.from_current_os_path("/tmp/foo/bar") self.assertEqual(curr.windows, None) self.assertEqual(curr.macosx, None) self.assertEqual(curr.linux, "/tmp/foo/bar") self.assertEqual(curr.current_os, curr.linux) if sys.platform == "darwin": curr = ShotgunPath.from_current_os_path("/tmp/foo/bar") self.assertEqual(curr.windows, None) self.assertEqual(curr.macosx, "/tmp/foo/bar") self.assertEqual(curr.linux, None) self.assertEqual(curr.current_os, curr.macosx) std_constructor = ShotgunPath("C:\\temp", "/tmp", "/tmp2") self.assertEqual(std_constructor.windows, "C:\\temp") self.assertEqual(std_constructor.macosx, "/tmp2") self.assertEqual(std_constructor.linux, "/tmp")
def test_construction(self): """ Tests get_cache_root """ self.assertEqual( ShotgunPath.SHOTGUN_PATH_FIELDS, ["windows_path", "linux_path", "mac_path"] ) sg = ShotgunPath.from_shotgun_dict( {"windows_path": "C:\\temp", "mac_path": "/tmp", "linux_path": "/tmp2", "foo": "bar"} ) self.assertEqual(sg.windows, "C:\\temp") self.assertEqual(sg.macosx, "/tmp") self.assertEqual(sg.linux, "/tmp2") sg = ShotgunPath.from_shotgun_dict( {"windows_path": "C:\\temp", "mac_path": None, "foo": "bar"} ) self.assertEqual(sg.windows, "C:\\temp") self.assertEqual(sg.macosx, None) self.assertEqual(sg.linux, None) sys_paths = ShotgunPath.from_system_dict( {"win32": "C:\\temp", "darwin": "/tmp", "linux2": "/tmp2", "foo": "bar"} ) self.assertEqual(sys_paths.windows, "C:\\temp") self.assertEqual(sys_paths.macosx, "/tmp") self.assertEqual(sys_paths.linux, "/tmp2") sys_paths = ShotgunPath.from_system_dict( {"win32": "C:\\temp", "darwin": None, "foo": "bar"} ) self.assertEqual(sys_paths.windows, "C:\\temp") self.assertEqual(sys_paths.macosx, None) self.assertEqual(sys_paths.linux, None) if sys.platform == "win32": curr = ShotgunPath.from_current_os_path("\\\\server\\mount\\path") self.assertEqual(curr.windows, "\\\\server\\mount\\path") self.assertEqual(curr.macosx, None) self.assertEqual(curr.linux, None) self.assertEqual(curr.current_os, curr.windows) if sys.platform == "linux2": curr = ShotgunPath.from_current_os_path("/tmp/foo/bar") self.assertEqual(curr.windows, None) self.assertEqual(curr.macosx, None) self.assertEqual(curr.linux, "/tmp/foo/bar") self.assertEqual(curr.current_os, curr.linux) if sys.platform == "darwin": curr = ShotgunPath.from_current_os_path("/tmp/foo/bar") self.assertEqual(curr.windows, None) self.assertEqual(curr.macosx, "/tmp/foo/bar") self.assertEqual(curr.linux, None) self.assertEqual(curr.current_os, curr.macosx) std_constructor = ShotgunPath("C:\\temp", "/tmp", "/tmp2") self.assertEqual(std_constructor.windows, "C:\\temp") self.assertEqual(std_constructor.macosx, "/tmp2") self.assertEqual(std_constructor.linux, "/tmp")