diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9fb3e0d6e3..9c57d2efac 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -38,6 +38,11 @@ variables: - group: deploy-secrets jobs: +- template: pip-install-packages.yml@templates + parameters: + packages: + - mock + - template: build-pipeline.yml@templates parameters: # After the tests have run, run the integration tests. diff --git a/python/tank/template.py b/python/tank/template.py index 77c135fc57..eba299a006 100644 --- a/python/tank/template.py +++ b/python/tank/template.py @@ -557,6 +557,17 @@ def root_path(self): """ return self._prefix + def _dirname(self): + """ + Returns the directory part of the template. + + :returns: String + """ + dirname, basename = os.path.split(self._repr_def) + if "[" in basename and "]" not in basename: + return dirname.split("[")[0] + return dirname + @property def parent(self): """ @@ -566,8 +577,8 @@ def parent(self): :returns: :class:`Template` """ - parent_definition = os.path.dirname(self.definition) - if parent_definition: + parent_definition = self._dirname() + if parent_definition and parent_definition != "/": return TemplatePath( parent_definition, self.keys, diff --git a/tests/core_tests/test_templatepath.py b/tests/core_tests/test_templatepath.py index ef7625bb9e..d9ea70df77 100644 --- a/tests/core_tests/test_templatepath.py +++ b/tests/core_tests/test_templatepath.py @@ -1184,3 +1184,43 @@ def test_aliased_key(self): template = TemplatePath(definition, keys, root_path=self.project_root) result = template.parent self.assertEqual("{new_name}", result.definition) + + def test_parent_with_optional(self): + definition = "shots/{Sequence}[-{seq_num}]/{Shot}/{Step}/work" + parent_expected_definitions = [ + "shots/{Sequence}-{seq_num}/{Shot}/{Step}", + "shots/{Sequence}/{Shot}/{Step}", + ] + parent_expected__repr_def = os.path.dirname(definition) + + template = TemplatePath(definition, self.keys, root_path=self.project_root) + parent = template.parent + self.assertEqual(parent_expected_definitions, parent._definitions) + self.assertEqual(parent_expected__repr_def, parent._repr_def) + + +class TestDirname(TestTemplatePath): + def test_dirname_with_word(self): + definition = "shots/{Sequence}/{Shot}/{Step}/work" + template = TemplatePath(definition, self.keys, root_path=self.project_root) + self.assertEqual("shots/{Sequence}/{Shot}/{Step}", template.dirname) + + def test_dirname_with_key(self): + definition = "shots/{Sequence}/{Shot}/{Step}" + template = TemplatePath(definition, self.keys, root_path=self.project_root) + self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) + + def test_dirname_with_optional(self): + definition = "shots/{Sequence}/{Shot}[/{Step}]" + template = TemplatePath(definition, {}, root_path=self.project_root) + self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) + + def test_dirname_with_optional_word_and_key(self): + definition = "shots/{Sequence}/{Shot}/[abc_{Step}]" + template = TemplatePath(definition, {}, root_path=self.project_root) + self.assertEqual("shots/{Sequence}/{Shot}", template.dirname) + + def test_dirname_with_optional_key_and_key(self): + definition = "shots/{Sequence}/[{Shot}]-{Step}" + template = TemplatePath(definition, {}, root_path=self.project_root) + self.assertEqual("shots/{Sequence}", template.dirname)