From 716345659161db4775c55eaf87e88a25daa66d1d Mon Sep 17 00:00:00 2001 From: ds Date: Wed, 3 Apr 2024 01:30:42 +0300 Subject: [PATCH] utilize pathlib.Path instead of os.path.join() --- lib/ansible/plugins/filter/core.py | 5 +++-- test/integration/targets/filter_core/files/foo.txt | 1 + test/integration/targets/filter_core/templates/foo.j2 | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 1c62dd569d7..d4fbbd4fc0a 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -9,6 +9,7 @@ import hashlib import json import ntpath import os.path +from pathlib import Path import re import shlex import sys @@ -576,9 +577,9 @@ def path_join(paths): ''' takes a sequence or a string, and return a concatenation of the different members ''' if isinstance(paths, string_types): - return os.path.join(paths) + return Path(paths).as_posix() if is_sequence(paths): - return os.path.join(*paths) + return Path(*paths).as_posix() raise AnsibleFilterTypeError("|path_join expects string or sequence, got %s instead." % type(paths)) diff --git a/test/integration/targets/filter_core/files/foo.txt b/test/integration/targets/filter_core/files/foo.txt index 9bd9b636719..85f1a3e1e26 100644 --- a/test/integration/targets/filter_core/files/foo.txt +++ b/test/integration/targets/filter_core/files/foo.txt @@ -55,6 +55,7 @@ files to exist and are passthrus to the python os.path functions path_join_simple = /etc/subdir/test path_join_with_slash = /test path_join_relative = etc/subdir/test +path_join_merged = /etc/subdir/test TODO: realpath follows symlinks. There isn't a test for this just now. diff --git a/test/integration/targets/filter_core/templates/foo.j2 b/test/integration/targets/filter_core/templates/foo.j2 index a69ba5ef219..b98836840be 100644 --- a/test/integration/targets/filter_core/templates/foo.j2 +++ b/test/integration/targets/filter_core/templates/foo.j2 @@ -49,6 +49,7 @@ files to exist and are passthrus to the python os.path functions path_join_simple = {{ ('/etc', 'subdir', 'test') | path_join }} path_join_with_slash = {{ ('/etc', 'subdir', '/test') | path_join }} path_join_relative = {{ ('etc', 'subdir', 'test') | path_join }} +path_join_merged = {{ ('/etc', 'subdir//test') | path_join }} TODO: realpath follows symlinks. There isn't a test for this just now.