summaryrefslogtreecommitdiffstats
path: root/tests/cmd/test_recurse_path.py
blob: 2fb3eb32d7fe356d8a8a59f672e567a963d69c0d (plain)
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
import os

from tests.base import mock
import testtools

from jenkins_jobs import utils


def fake_os_walk(paths):
    """Helper function for mocking os.walk() where must test that manipulation
    of the returned dirs variable works as expected
    """
    paths_dict = dict(paths)

    def os_walk(top, topdown=True):
        dirs, nondirs = paths_dict[top]
        yield top, dirs, nondirs
        for name in dirs:
            # hard code use of '/' to ensure the test data can be defined as
            # simple strings otherwise tests using this helper will break on
            # platforms where os.path.sep is different.
            new_path = "/".join([top, name])
            for x in os_walk(new_path, topdown):
                yield x
    return os_walk


# Testing the utils module can sometimes result in the JobCache class
# attempting to create the cache directory multiple times as the tests
# are run in parallel.  Stub out the JobCache to ensure that each
# test can safely create the object without effect.
@mock.patch('jenkins_jobs.builder.JobCache', mock.MagicMock)
class CmdRecursePath(testtools.TestCase):

    @mock.patch('jenkins_jobs.utils.os.walk')
    def test_recursive_path_option_exclude_pattern(self, oswalk_mock):
        """
        Test paths returned by the recursive processing when using pattern
        excludes.

        testing paths
            /jjb_configs/dir1/test1/
            /jjb_configs/dir1/file
            /jjb_configs/dir2/test2/
            /jjb_configs/dir3/bar/
            /jjb_configs/test3/bar/
            /jjb_configs/test3/baz/
        """

        os_walk_paths = [
            ('/jjb_configs', (['dir1', 'dir2', 'dir3', 'test3'], ())),
            ('/jjb_configs/dir1', (['test1'], ('file'))),
            ('/jjb_configs/dir2', (['test2'], ())),
            ('/jjb_configs/dir3', (['bar'], ())),
            ('/jjb_configs/dir3/bar', ([], ())),
            ('/jjb_configs/test3/bar', None),
            ('/jjb_configs/test3/baz', None)
        ]

        paths = [k for k, v in os_walk_paths if v is not None]

        oswalk_mock.side_effect = fake_os_walk(os_walk_paths)
        self.assertEqual(paths, utils.recurse_path('/jjb_configs', ['test*']))

    @mock.patch('jenkins_jobs.utils.os.walk')
    def test_recursive_path_option_exclude_absolute(self, oswalk_mock):
        """
        Test paths returned by the recursive processing when using absolute
        excludes.

        testing paths
            /jjb_configs/dir1/test1/
            /jjb_configs/dir1/file
            /jjb_configs/dir2/test2/
            /jjb_configs/dir3/bar/
            /jjb_configs/test3/bar/
            /jjb_configs/test3/baz/
        """

        os_walk_paths = [
            ('/jjb_configs', (['dir1', 'dir2', 'dir3', 'test3'], ())),
            ('/jjb_configs/dir1', None),
            ('/jjb_configs/dir2', (['test2'], ())),
            ('/jjb_configs/dir3', (['bar'], ())),
            ('/jjb_configs/test3', (['bar', 'baz'], ())),
            ('/jjb_configs/dir2/test2', ([], ())),
            ('/jjb_configs/dir3/bar', ([], ())),
            ('/jjb_configs/test3/bar', ([], ())),
            ('/jjb_configs/test3/baz', ([], ()))
        ]

        paths = [k for k, v in os_walk_paths if v is not None]

        oswalk_mock.side_effect = fake_os_walk(os_walk_paths)

        self.assertEqual(paths, utils.recurse_path('/jjb_configs',
                                                   ['/jjb_configs/dir1']))

    @mock.patch('jenkins_jobs.utils.os.walk')
    def test_recursive_path_option_exclude_relative(self, oswalk_mock):
        """
        Test paths returned by the recursive processing when using relative
        excludes.

        testing paths
            ./jjb_configs/dir1/test/
            ./jjb_configs/dir1/file
            ./jjb_configs/dir2/test/
            ./jjb_configs/dir3/bar/
            ./jjb_configs/test3/bar/
            ./jjb_configs/test3/baz/
        """

        os_walk_paths = [
            ('jjb_configs', (['dir1', 'dir2', 'dir3', 'test3'], ())),
            ('jjb_configs/dir1', (['test'], ('file'))),
            ('jjb_configs/dir2', (['test2'], ())),
            ('jjb_configs/dir3', (['bar'], ())),
            ('jjb_configs/test3', (['bar', 'baz'], ())),
            ('jjb_configs/dir1/test', ([], ())),
            ('jjb_configs/dir2/test2', ([], ())),
            ('jjb_configs/dir3/bar', ([], ())),
            ('jjb_configs/test3/bar', None),
            ('jjb_configs/test3/baz', ([], ()))
        ]

        rel_os_walk_paths = [
            (os.path.abspath(
                os.path.join(os.path.curdir, k)), v) for k, v in os_walk_paths]

        paths = [k for k, v in rel_os_walk_paths if v is not None]

        oswalk_mock.side_effect = fake_os_walk(rel_os_walk_paths)

        self.assertEqual(paths, utils.recurse_path('jjb_configs',
                                                   ['jjb_configs/test3/bar']))