Skip to content

Commit

Permalink
Don't take . folder into consideration for tools.files.copy() `ex…
Browse files Browse the repository at this point in the history
…cludes` patterns (#15349)

* Don't take . folder into consideration for tools.copy excludes patterns

* Simplify testing following review suggestion
  • Loading branch information
AbrilRBS authored Dec 27, 2023
1 parent f2a4de2 commit bde2eaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
12 changes: 7 additions & 5 deletions conan/tools/files/copy_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ def _filter_files(src, pattern, excludes, ignore_case, excluded_folder):

relative_path = os.path.relpath(root, src)
compare_relative_path = relative_path.lower() if ignore_case else relative_path
for exclude in excludes:
if fnmatch.fnmatch(compare_relative_path, exclude):
subfolders[:] = []
files = []
break
# Don't try to exclude the start folder, it conflicts with excluding names starting with dots
if not compare_relative_path == ".":
for exclude in excludes:
if fnmatch.fnmatch(compare_relative_path, exclude):
subfolders[:] = []
files = []
break
for f in files:
relative_name = os.path.normpath(os.path.join(relative_path, f))
filenames.append(relative_name)
Expand Down
21 changes: 20 additions & 1 deletion conans/test/unittests/tools/files/test_tool_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from conan.tools.files import copy
from conans.test.utils.test_files import temp_folder
from conans.util.files import load, save, mkdir
from conans.util.files import load, save, mkdir, save_files


class ToolCopyTest(unittest.TestCase):
Expand Down Expand Up @@ -161,6 +161,7 @@ def test_excludes(self):
save(os.path.join(folder1, "MyLib.txt"), "")
save(os.path.join(folder1, "MyLibImpl.txt"), "")
save(os.path.join(folder1, "MyLibTests.txt"), "")

folder2 = temp_folder()
copy(None, "*.txt", folder1, folder2, excludes="*Test*.txt")
self.assertEqual({'MyLib.txt', 'MyLibImpl.txt'}, set(os.listdir(folder2)))
Expand All @@ -169,6 +170,24 @@ def test_excludes(self):
copy(None, "*.txt", folder1, folder2, excludes=("*Test*.txt", "*Impl*"))
self.assertEqual(['MyLib.txt'], os.listdir(folder2))

def test_excludes_hidden_files(self):
folder1 = temp_folder()
save_files(folder1, {
"file1.txt": "",
".hiddenfile": "",
"foo/file2.txt": "",
"foo/.hiddenfile2": "",
".hiddenfolder/file3.txt": "",
"foo/bar/file4.txt": ""
})

folder2 = temp_folder()
copy(None, "*", folder1, folder2, excludes=(".*", "*/.*"))
assert os.listdir(folder2) == ['file1.txt', 'foo']
assert os.listdir(os.path.join(folder2, "foo")) == ['file2.txt', 'bar']
assert not os.path.exists(os.path.join(folder2, ".hiddenfolder"))
assert os.listdir(os.path.join(folder2, "foo", "bar")) == ['file4.txt']

def test_excludes_camelcase_folder(self):
# https://github.com/conan-io/conan/issues/8153
folder1 = temp_folder()
Expand Down

0 comments on commit bde2eaa

Please sign in to comment.