[config_types.Path] Add parent and name properties Also change some tests to use the new properties for coverage. Bug: 329113288 Change-Id: Idd429ce68b618223acbf4ea5b3494a1ec4321291 Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/recipes-py/+/5447093 Auto-Submit: Rob Mohr <mohrr@google.com> Commit-Queue: Chan Li <chanli@chromium.org> Reviewed-by: Chan Li <chanli@chromium.org>
diff --git a/recipe_engine/config_types.py b/recipe_engine/config_types.py index e0633fc..8f9a110 100644 --- a/recipe_engine/config_types.py +++ b/recipe_engine/config_types.py
@@ -258,6 +258,16 @@ object.__setattr__(self, 'base', base) object.__setattr__(self, 'pieces', tuple(normalized_pieces)) + @property + def parent(self) -> Path: + """For 'foo/bar/baz', return 'foo/bar'.""" + return Path(self.base, *self.pieces[0:-1]) + + @property + def name(self) -> str: + """For 'foo/bar/baz', return 'baz'.""" + return self.pieces[-1] + def _resolve(self) -> Path: """If self.base is a ResolvedBasePath, this will return self.
diff --git a/recipe_modules/file/examples/copytree.py b/recipe_modules/file/examples/copytree.py index d80b945..db4df9e 100644 --- a/recipe_modules/file/examples/copytree.py +++ b/recipe_modules/file/examples/copytree.py
@@ -28,7 +28,7 @@ assert paths == [dest2.join('a'), dest2.join('aa')], paths for pth in paths: - assert api.file.read_text('read %s' % pth, pth, api.path.basename(pth)) + assert api.file.read_text('read %s' % pth, pth, pth.name) api.file.remove('rm a', dest2.join('a')) paths = api.file.glob_paths('glob *a', dest2, '*a', test_data=['aa']) @@ -44,4 +44,3 @@ def GenTests(api): yield api.test('basic') -
diff --git a/recipe_modules/path/examples/full.py b/recipe_modules/path/examples/full.py index c3d5179..4db52c3 100644 --- a/recipe_modules/path/examples/full.py +++ b/recipe_modules/path/examples/full.py
@@ -74,7 +74,9 @@ assert api.path.pathsep == ':' assert api.path.basename(file_path) == 'new_file' + assert file_path.name == 'new_file' assert api.path.dirname(file_path) == api.path.tmp_base_dir + assert file_path.parent == api.path.tmp_base_dir assert api.path.split(file_path) == (api.path.tmp_base_dir, 'new_file') thing_bat = api.path.tmp_base_dir.join('thing.bat')