1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#
# Copyright 2002 Ben Escoto
# Copyright 2007 Kenneth Loafman
# Copyright 2014 Aaron Whitehouse
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import unittest
import sys
from unittest.mock import patch
from duplicity.globmatch import * # pylint: disable=unused-wildcard-import,redefined-builtin
from duplicity.path import * # pylint: disable=unused-wildcard-import,redefined-builtin
from testing import _runtest_dir
from . import UnitTestCase
def sel_file(glob_str, include, file_path):
"""Returns the selection value for file_path, given the include value,
returning:
0 - if the file should be excluded
1 - if the file should be included
2 - if the folder should be scanned for any included/excluded files
None - if the selection function has nothing to say about the file
Note: including a folder implicitly includes everything within it."""
select_fn = select_fn_from_glob(glob_str, include)
selection_value = select_fn(Path(file_path))
return selection_value
def inc_sel_file(glob_str, file_path):
"""Returns result of sel_file with include value set to 1"""
# Aids readability of the testing code to only have one number (the
# result of the select function)
return sel_file(glob_str, 1, file_path)
def exc_sel_file(glob_str, file_path):
"""Returns result of sel_file with include value set to 0"""
return sel_file(glob_str, 0, file_path)
def sel_dir(glob_str, include, file_path):
"""As per sel_file, but mocks file_path to be a directory"""
with patch("duplicity.path.Path.isdir") as mock_isdir:
mock_isdir.return_value = True
return sel_file(glob_str, include, file_path)
def inc_sel_dir(glob_str, file_path):
"""Returns result of sel_dir with include value set to 1"""
return sel_dir(glob_str, 1, file_path)
def exc_sel_dir(glob_str, file_path):
"""Returns result of sel_dir with include value set to 0"""
return sel_dir(glob_str, 0, file_path)
class TestGlobToRegex(UnitTestCase):
"""Test translation of glob strings into regular expressions"""
def test_glob_to_regex(self):
"""test_glob_re - test translation of shell pattern to regular exp"""
self.assertEqual(glob_to_regex("hello"), "hello")
self.assertEqual(glob_to_regex(".e?ll**o"), "\\.e[^/]ll.*o")
self.assertEqual(glob_to_regex("[abc]el[^de][!fg]h"), "[abc]el[^de][^fg]h")
# see https://bugs.python.org/issue29995 for details
if sys.version_info[:2] == (3, 6):
self.assertEqual(glob_to_regex("/usr/*/bin/"), "\\/usr\\/[^/]*\\/bin\\/")
elif sys.version_info[:2] >= (3, 7):
self.assertEqual(glob_to_regex("/usr/*/bin/"), "/usr/[^/]*/bin/")
else:
pass
self.assertEqual(glob_to_regex("[a.b/c]"), "[a.b/c]")
self.assertEqual(glob_to_regex("[a*b-c]e[!]]"), "[a*b-c]e[^]]")
class TestSelectValuesFromGlobs(UnitTestCase):
"""Test the select values returned from various globs"""
def test_glob_scans_parent_directories(self):
"""Test glob scans parent"""
self.assertEqual(
inc_sel_dir(
f"{_runtest_dir}/testfiles/parent/sub",
f"{_runtest_dir}/testfiles/parent",
),
2,
) # noqa
self.assertEqual(
inc_sel_dir(
f"{_runtest_dir}/testfiles/select2/3/3sub2",
f"{_runtest_dir}/testfiles/select2/3",
),
2,
) # noqa
def test_double_asterisk_include(self):
"""Test a few globbing patterns, including **"""
self.assertEqual(inc_sel_file("**", "foo.txt"), 1)
self.assertEqual(inc_sel_dir("**", "folder"), 1)
def test_double_asterisk_extension_include(self):
"""Test **.py"""
self.assertEqual(inc_sel_file("**.py", "what/ever.py"), 1)
self.assertEqual(inc_sel_file("**.py", "what/ever.py/foo"), 1)
self.assertEqual(inc_sel_dir("**.py", "foo"), 2)
self.assertEqual(inc_sel_dir("**.py", "usr/local/bin"), 2)
self.assertEqual(inc_sel_dir("**.py", "/usr/local/bin"), 2)
class TestTrailingSlash(UnitTestCase):
"""Test glob matching where the glob has a trailing slash"""
def test_trailing_slash_matches_only_dirs(self):
"""Test matching where glob includes a trailing slash"""
# Test the folder named "folder" is included
self.assertEqual(inc_sel_dir("fold*/", "folder"), 1)
# Test the file (not folder) named "folder" is not included
self.assertEqual(inc_sel_file("fold*/", "folder"), None)
self.assertEqual(inc_sel_file("folder/", "folder"), None)
# Test miscellaneous file/folder
self.assertEqual(inc_sel_file("fo*/", "foo.txt"), None)
def test_included_files_are_matched_no_slash(self):
"""Test that files within an included folder are matched"""
self.assertEqual(inc_sel_file("fold*", "folder/file.txt"), 1)
self.assertEqual(inc_sel_file("fold*", "folder/file.txt"), 1)
self.assertEqual(inc_sel_file("fold*", "folder/2/file.txt"), 1)
def test_included_files_are_matched_no_slash_2(self):
"""Test that files within an included folder are matched"""
self.assertEqual(inc_sel_file("folder", "folder/file.txt"), 1)
self.assertEqual(inc_sel_file("folder/2", "folder/2/file.txt"), 1)
def test_included_files_are_matched_slash(self):
"""Test that files within an included folder are matched with /"""
# Bug #1624725
# https://bugs.launchpad.net/duplicity/+bug/1624725
self.assertEqual(inc_sel_file("folder/", "folder/file.txt"), 1)
def test_included_files_are_matched_slash_2(self):
"""Test that files within an included folder are matched with /"""
# Bug #1624725
# https://bugs.launchpad.net/duplicity/+bug/1624725
self.assertEqual(
inc_sel_file(
f"{_runtest_dir}/testfiles/select2/1/1sub1/1sub1sub1/",
f"{_runtest_dir}/testfiles/select2/1/1sub1/1sub1sub1/1sub1sub1_file.txt",
),
1,
)
def test_included_files_are_matched_slash_2_parents(self):
"""Test that duplicity will scan parent of glob/"""
# Bug #1624725
# https://bugs.launchpad.net/duplicity/+bug/1624725
self.assertEqual(
inc_sel_dir(
f"{_runtest_dir}/testfiles/select2/1/1sub1/1sub1sub1/",
f"{_runtest_dir}/testfiles/select2/1/1sub1/1sub1sub1",
),
1,
)
self.assertEqual(
inc_sel_dir(
f"{_runtest_dir}/testfiles/select2/1/1sub1/1sub1sub1/",
f"{_runtest_dir}/testfiles/select2/1/1sub1",
),
2,
)
def test_included_files_are_matched_slash_wildcard(self):
"""Test that files within an included folder are matched with /"""
# Bug #1624725
# https://bugs.launchpad.net/duplicity/+bug/1624725
self.assertEqual(inc_sel_file("fold*/", "folder/file.txt"), 1)
def test_slash_matches_everything(self):
"""Test / matches everything"""
self.assertEqual(inc_sel_dir("/", f"{_runtest_dir}/testfiles/select/1/2"), 1)
self.assertEqual(inc_sel_dir("/", "/test/random/path"), 1)
self.assertEqual(exc_sel_dir("/", "/test/random/path"), 0)
self.assertEqual(inc_sel_dir("/", "/"), 1)
self.assertEqual(inc_sel_dir("/", "/var/log"), 1)
self.assertEqual(inc_sel_file("/", "/var/log/log.txt"), 1)
def test_slash_star_scans_folder(self):
"""Test that folder/* scans folder/"""
# This behaviour is a bit ambiguous - either include or scan could be
# argued as most appropriate here, but only an empty folder is at stake
# so long as test_slash_star_includes_folder_contents passes.
self.assertEqual(inc_sel_dir("folder/*", "folder"), 2)
def test_slash_star_includes_folder_contents(self):
"""Test that folder/* includes folder contents"""
self.assertEqual(inc_sel_file("folder/*", "folder/file.txt"), 1)
self.assertEqual(inc_sel_file("folder/*", "folder/other_file.log"), 1)
def test_slash_star_star_scans_folder(self):
"""Test that folder/** scans folder/"""
self.assertEqual(inc_sel_dir("folder/**", "folder"), 2)
def test_simple_trailing_slash_match(self):
"""Test that a normal folder string ending in / matches that path"""
self.assertEqual(
inc_sel_dir(
f"{_runtest_dir}/testfiles/select/1/2/1/",
f"{_runtest_dir}/testfiles/select/1/2/1",
),
1,
)
def test_double_asterisk_string_slash(self):
"""Test string starting with ** and ending in /"""
self.assertEqual(inc_sel_dir("**/1/2/", f"{_runtest_dir}/testfiles/select/1/2"), 1)
def test_string_double_asterisk_string_slash(self):
"""Test string ** string /"""
self.assertEqual(
inc_sel_dir(f"{_runtest_dir}/testfiles**/2/", f"{_runtest_dir}/testfiles/select/1/2"),
1,
)
class TestDoubleAsterisk(UnitTestCase):
"""Test glob matching where the glob finishes with a **"""
def test_double_asterisk_no_match(self):
"""Test that a folder string ending /** does not match other paths"""
self.assertEqual(inc_sel_dir("/test/folder/**", "/test/foo"), None)
def test_double_asterisk_match(self):
"""Test that a folder string ending in /** matches that path"""
self.assertEqual(inc_sel_dir("/test/folder/**", "/test/folder/foo"), 1)
self.assertEqual(inc_sel_file("/test/folder/**", "/test/folder/foo.txt"), 1)
self.assertEqual(inc_sel_dir("/test/folder/**", "/test/folder/2/foo"), 1)
self.assertEqual(inc_sel_file("/test/folder/**", "/test/folder/2/foo.txt"), 1)
def test_asterisk_slash_double_asterisk(self):
"""Test folder string ending in */**"""
self.assertEqual(inc_sel_dir("fold*/**", "folder"), 2)
class TestSimpleUnicode(UnitTestCase):
"""Test simple unicode comparison"""
@unittest.skip("Doesn't seem to work with python2.x")
def test_simple_unicode(self):
"""Test simple unicode comparison"""
self.assertEqual(
inc_sel_file(
"прыклад/пример/例/Παράδειγμα/उदाहरण.txt",
"прыклад/пример/例/Παράδειγμα/उदाहरण.txt",
),
1,
)
class TestSquareBrackets(UnitTestCase):
"""Test glob matching where the glob includes []s and [!]s"""
def test_square_bracket_options(self):
"""Test file including options in []s"""
self.assertEqual(inc_sel_file("/test/f[o,s,p]lder/foo.txt", "/test/folder/foo.txt"), 1)
self.assertEqual(inc_sel_file("/test/f[i,s,p]lder/foo.txt", "/test/folder/foo.txt"), None)
self.assertEqual(inc_sel_file("/test/f[s,o,p]lder/foo.txt", "/test/folder/foo.txt"), 1)
def test_square_bracket_options_unicode(self):
"""Test file including options in []s"""
self.assertEqual(
inc_sel_file(
"прыклад/пр[и,j,l]мер/例/Παράδειγμα/उदाहरण.txt",
"прыклад/пример/例/Παράδειγμα/उदाहरण.txt",
),
1,
)
self.assertEqual(
inc_sel_file(
"прыклад/п[a,b,c]имер/例/Παράδειγμα/उदाहरण.txt",
"прыклад/пример/例/Παράδειγμα/उदाहरण.txt",
),
None,
)
def test_not_square_bracket_options(self):
"""Test file including options in [!]s"""
self.assertEqual(inc_sel_file("/test/f[!o,s,p]lder/foo.txt", "/test/folder/foo.txt"), None)
self.assertEqual(inc_sel_file("/test/f[!i,s,p]lder/foo.txt", "/test/folder/foo.txt"), 1)
self.assertEqual(inc_sel_file("/test/f[!s,o,p]lder/foo.txt", "/test/folder/foo.txt"), None)
def test_square_bracket_range(self):
"""Test file including range in []s"""
self.assertEqual(inc_sel_file("/test/folder[1-5]/foo.txt", "/test/folder4/foo.txt"), 1)
self.assertEqual(inc_sel_file("/test/folder[5-9]/foo.txt", "/test/folder4/foo.txt"), None)
self.assertEqual(inc_sel_file("/test/folder[1-5]/foo.txt", "/test/folder6/foo.txt"), None)
def test_square_bracket_not_range(self):
"""Test file including range in [!]s"""
self.assertEqual(inc_sel_file("/test/folder[!1-5]/foo.txt", "/test/folder4/foo.txt"), None)
self.assertEqual(inc_sel_file("/test/folder[!5-9]/foo.txt", "/test/folder4/foo.txt"), 1)
self.assertEqual(inc_sel_file("/test/folder[!1-5]/foo.txt", "/test/folder6/foo.txt"), 1)
|