File: test_glob.py

package info (click to toggle)
python-fs 2.4.16-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: python: 13,048; makefile: 226; sh: 3
file content (98 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (2)
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
from __future__ import unicode_literals

import unittest

from fs import glob, open_fs


class TestGlob(unittest.TestCase):
    def setUp(self):
        fs = self.fs = open_fs("mem://")
        fs.writetext("foo.py", "Hello, World")
        fs.touch("bar.py")
        fs.touch("baz.py")
        fs.makedirs("egg")
        fs.writetext("egg/foo.py", "from fs import open_fs")
        fs.touch("egg/foo.pyc")
        fs.makedirs("a/b/c/").writetext("foo.py", "import fs")
        repr(fs.glob)

    def test_match(self):
        tests = [
            ("*.?y", "/test.py", True),
            ("*.py", "/test.py", True),
            ("*.py", "/test.pc", False),
            ("*.py", "/foo/test.py", False),
            ("foo/*.py", "/foo/test.py", True),
            ("foo/*.py", "/bar/foo/test.py", False),
            ("?oo/*.py", "/foo/test.py", True),
            ("*/*.py", "/foo/test.py", True),
            ("foo/*.py", "/bar/foo/test.py", False),
            ("**/foo/*.py", "/bar/foo/test.py", True),
            ("foo/**/bar/*.py", "/foo/bar/test.py", True),
            ("foo/**/bar/*.py", "/foo/baz/egg/bar/test.py", True),
            ("foo/**/bar/*.py", "/foo/baz/egg/bar/egg/test.py", False),
            ("**", "/test.py", True),
            ("**", "/test", True),
            ("**", "/test/", True),
            ("**/", "/test/", True),
            ("**/", "/test.py", False),
        ]
        for pattern, path, expected in tests:
            self.assertEqual(glob.match(pattern, path), expected)
        # Run a second time to test cache
        for pattern, path, expected in tests:
            self.assertEqual(glob.match(pattern, path), expected)

    def test_count_1dir(self):
        globber = glob.BoundGlobber(self.fs)
        counts = globber("*.py").count()
        self.assertEqual(counts, glob.Counts(files=3, directories=0, data=12))
        repr(globber("*.py"))

    def test_count_2dir(self):
        globber = glob.BoundGlobber(self.fs)
        counts = globber("*/*.py").count()
        self.assertEqual(counts, glob.Counts(files=1, directories=0, data=22))

    def test_count_recurse_dir(self):
        globber = glob.BoundGlobber(self.fs)
        counts = globber("**/*.py").count()
        self.assertEqual(counts, glob.Counts(files=5, directories=0, data=43))

    def test_count_lines(self):
        globber = glob.BoundGlobber(self.fs)
        line_counts = globber("**/*.py").count_lines()
        self.assertEqual(line_counts, glob.LineCounts(lines=3, non_blank=3))

    def test_count_dirs(self):
        globber = glob.BoundGlobber(self.fs)
        counts = globber("**/?/").count()
        self.assertEqual(counts, glob.Counts(files=0, directories=3, data=0))

    def test_count_all(self):
        globber = glob.BoundGlobber(self.fs)
        counts = globber("**").count()
        self.assertEqual(counts, glob.Counts(files=6, directories=4, data=43))
        counts = globber("**/").count()
        self.assertEqual(counts, glob.Counts(files=0, directories=4, data=0))

    def test_remove(self):
        globber = glob.BoundGlobber(self.fs)
        self.assertTrue(self.fs.exists("egg/foo.pyc"))
        removed_count = globber("**/*.pyc").remove()
        self.assertEqual(removed_count, 1)
        self.assertFalse(self.fs.exists("egg/foo.pyc"))

    def test_remove_dir(self):
        globber = glob.BoundGlobber(self.fs)
        self.assertTrue(self.fs.exists("egg/foo.pyc"))
        removed_count = globber("**/?/").remove()
        self.assertEqual(removed_count, 3)
        self.assertFalse(self.fs.exists("a"))
        self.assertTrue(self.fs.exists("egg"))

    def test_remove_all(self):
        globber = glob.BoundGlobber(self.fs)
        globber("**").remove()
        self.assertEqual(sorted(self.fs.listdir("/")), [])