1#!/usr/bin/env python 2# Copyright 2020 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for pw_watch.minimal_watch_directories.""" 16 17import unittest 18import tempfile 19from pathlib import Path 20 21from pw_watch import watch 22 23 24class TestMinimalWatchDirectories(unittest.TestCase): 25 """Tests for pw_watch.watch.minimal_watch_directories.""" 26 27 def setUp(self): 28 self._tempdir = tempfile.TemporaryDirectory() 29 self._root = Path(self._tempdir.name) 30 31 def tearDown(self): 32 self._tempdir.cleanup() 33 34 def make_tree(self, *directories: str) -> None: 35 for directory in directories: 36 self._root.joinpath(directory).mkdir(parents=True) 37 38 def test_empty_directory(self): 39 subdirectories_to_watch = [] 40 ans_subdirectories_to_watch = [(self._root, False)] 41 subdirectories_to_watch = watch.minimal_watch_directories( 42 self._root, 'f1' 43 ) 44 45 self.assertEqual( 46 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 47 ) 48 49 def test_non_exist_directories_to_exclude(self): 50 subdirectories_to_watch = [] 51 exclude_list = ['f3'] 52 self.make_tree('f1', 'f2') 53 ans_subdirectories_to_watch = [ 54 (self._root / 'f1', True), 55 (self._root / 'f2', True), 56 (self._root, False), 57 ] 58 subdirectories_to_watch = watch.minimal_watch_directories( 59 self._root, exclude_list 60 ) 61 62 self.assertEqual( 63 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 64 ) 65 66 def test_one_layer_directories(self): 67 subdirectories_to_watch = [] 68 exclude_list = ['f1'] 69 self.make_tree( 70 'f1/f1', 71 'f1/f2', 72 'f2/f1', 73 ) 74 ans_subdirectories_to_watch = [ 75 (self._root / 'f2', True), 76 (self._root, False), 77 ] 78 subdirectories_to_watch = watch.minimal_watch_directories( 79 self._root, exclude_list 80 ) 81 82 self.assertEqual( 83 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 84 ) 85 86 def test_two_layers_direcories(self): 87 subdirectories_to_watch = [] 88 exclude_list = ['f1/f2'] 89 self.make_tree( 90 'f1/f1', 91 'f1/f2', 92 'f2/f1', 93 ) 94 ans_subdirectories_to_watch = [ 95 (self._root / 'f2', True), 96 (self._root / 'f1/f1', True), 97 (self._root, False), 98 (self._root / 'f1', False), 99 ] 100 subdirectories_to_watch = watch.minimal_watch_directories( 101 self._root, exclude_list 102 ) 103 104 self.assertEqual( 105 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 106 ) 107 108 def test_empty_exclude_list(self): 109 subdirectories_to_watch = [] 110 exclude_list = [] 111 self.make_tree( 112 'f1/f1', 113 'f1/f2', 114 'f2/f1', 115 ) 116 ans_subdirectories_to_watch = [ 117 (self._root / 'f2', True), 118 (self._root / 'f1', True), 119 (self._root, False), 120 ] 121 subdirectories_to_watch = watch.minimal_watch_directories( 122 self._root, exclude_list 123 ) 124 125 self.assertEqual( 126 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 127 ) 128 129 def test_multiple_directories_in_exclude_list(self): 130 """test case for multiple directories to exclude""" 131 subdirectories_to_watch = [] 132 exclude_list = [ 133 'f1/f2', 134 'f3/f1', 135 'f3/f3', 136 ] 137 self.make_tree( 138 'f1/f1', 139 'f1/f2', 140 'f2/f1', 141 'f3/f1', 142 'f3/f2', 143 'f3/f3', 144 ) 145 ans_subdirectories_to_watch = [ 146 (self._root / 'f2', True), 147 (self._root / 'f1/f1', True), 148 (self._root / 'f3/f2', True), 149 (self._root, False), 150 (self._root / 'f1', False), 151 (self._root / 'f3', False), 152 ] 153 subdirectories_to_watch = watch.minimal_watch_directories( 154 self._root, exclude_list 155 ) 156 157 self.assertEqual( 158 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 159 ) 160 161 def test_nested_sibling_exclusion(self): 162 subdirectories_to_watch = [] 163 exclude_list = [ 164 'f1/f1/f1/f1/f1', 165 'f1/f1/f1/f2', 166 ] 167 self.make_tree( 168 'f1/f1/f1/f1/f1', 169 'f1/f1/f1/f1/f2', 170 'f1/f1/f1/f1/f3', 171 'f1/f1/f1/f2', 172 ) 173 ans_subdirectories_to_watch = [ 174 (self._root / 'f1/f1/f1/f1/f2', True), 175 (self._root / 'f1/f1/f1/f1/f3', True), 176 (self._root, False), 177 (self._root / 'f1', False), 178 (self._root / 'f1/f1', False), 179 (self._root / 'f1/f1/f1', False), 180 (self._root / 'f1/f1/f1/f1', False), 181 ] 182 subdirectories_to_watch = watch.minimal_watch_directories( 183 self._root, exclude_list 184 ) 185 186 self.assertEqual( 187 set(subdirectories_to_watch), set(ans_subdirectories_to_watch) 188 ) 189 190 191if __name__ == '__main__': 192 unittest.main() 193