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 def setUp(self): 27 self._tempdir = tempfile.TemporaryDirectory() 28 self._root = Path(self._tempdir.name) 29 30 def tearDown(self): 31 self._tempdir.cleanup() 32 33 def make_tree(self, *directories: str) -> None: 34 for directory in directories: 35 self._root.joinpath(directory).mkdir(parents=True) 36 37 def test_empty_directory(self): 38 subdirectories_to_watch = [] 39 ans_subdirectories_to_watch = [(self._root, False)] 40 subdirectories_to_watch = watch.minimal_watch_directories( 41 self._root, 'f1') 42 43 self.assertEqual(set(subdirectories_to_watch), 44 set(ans_subdirectories_to_watch)) 45 46 def test_non_exist_directories_to_exclude(self): 47 subdirectories_to_watch = [] 48 exclude_list = ['f3'] 49 self.make_tree('f1', 'f2') 50 ans_subdirectories_to_watch = [ 51 (self._root / 'f1', True), 52 (self._root / 'f2', True), 53 (self._root, False), 54 ] 55 subdirectories_to_watch = watch.minimal_watch_directories( 56 self._root, exclude_list) 57 58 self.assertEqual(set(subdirectories_to_watch), 59 set(ans_subdirectories_to_watch)) 60 61 def test_one_layer_directories(self): 62 subdirectories_to_watch = [] 63 exclude_list = ['f1'] 64 self.make_tree( 65 'f1/f1', 66 'f1/f2', 67 'f2/f1', 68 ) 69 ans_subdirectories_to_watch = [ 70 (self._root / 'f2', True), 71 (self._root, False), 72 ] 73 subdirectories_to_watch = watch.minimal_watch_directories( 74 self._root, exclude_list) 75 76 self.assertEqual(set(subdirectories_to_watch), 77 set(ans_subdirectories_to_watch)) 78 79 def test_two_layers_direcories(self): 80 subdirectories_to_watch = [] 81 exclude_list = ['f1/f2'] 82 self.make_tree( 83 'f1/f1', 84 'f1/f2', 85 'f2/f1', 86 ) 87 ans_subdirectories_to_watch = [ 88 (self._root / 'f2', True), 89 (self._root / 'f1/f1', True), 90 (self._root, False), 91 (self._root / 'f1', False), 92 ] 93 subdirectories_to_watch = watch.minimal_watch_directories( 94 self._root, exclude_list) 95 96 self.assertEqual(set(subdirectories_to_watch), 97 set(ans_subdirectories_to_watch)) 98 99 def test_empty_exclude_list(self): 100 subdirectories_to_watch = [] 101 exclude_list = [] 102 self.make_tree( 103 'f1/f1', 104 'f1/f2', 105 'f2/f1', 106 ) 107 ans_subdirectories_to_watch = [ 108 (self._root / 'f2', True), 109 (self._root / 'f1', True), 110 (self._root, False), 111 ] 112 subdirectories_to_watch = watch.minimal_watch_directories( 113 self._root, exclude_list) 114 115 self.assertEqual(set(subdirectories_to_watch), 116 set(ans_subdirectories_to_watch)) 117 118 def test_multiple_directories_in_exclude_list(self): 119 """test case for multiple directories to exclude""" 120 subdirectories_to_watch = [] 121 exclude_list = [ 122 'f1/f2', 123 'f3/f1', 124 'f3/f3', 125 ] 126 self.make_tree( 127 'f1/f1', 128 'f1/f2', 129 'f2/f1', 130 'f3/f1', 131 'f3/f2', 132 'f3/f3', 133 ) 134 ans_subdirectories_to_watch = [ 135 (self._root / 'f2', True), 136 (self._root / 'f1/f1', True), 137 (self._root / 'f3/f2', True), 138 (self._root, False), 139 (self._root / 'f1', False), 140 (self._root / 'f3', False), 141 ] 142 subdirectories_to_watch = watch.minimal_watch_directories( 143 self._root, exclude_list) 144 145 self.assertEqual(set(subdirectories_to_watch), 146 set(ans_subdirectories_to_watch)) 147 148 def test_nested_sibling_exclusion(self): 149 subdirectories_to_watch = [] 150 exclude_list = [ 151 'f1/f1/f1/f1/f1', 152 'f1/f1/f1/f2', 153 ] 154 self.make_tree( 155 'f1/f1/f1/f1/f1', 156 'f1/f1/f1/f1/f2', 157 'f1/f1/f1/f1/f3', 158 'f1/f1/f1/f2', 159 ) 160 ans_subdirectories_to_watch = [ 161 (self._root / 'f1/f1/f1/f1/f2', True), 162 (self._root / 'f1/f1/f1/f1/f3', True), 163 (self._root, False), 164 (self._root / 'f1', False), 165 (self._root / 'f1/f1', False), 166 (self._root / 'f1/f1/f1', False), 167 (self._root / 'f1/f1/f1/f1', False), 168 ] 169 subdirectories_to_watch = watch.minimal_watch_directories( 170 self._root, exclude_list) 171 172 self.assertEqual(set(subdirectories_to_watch), 173 set(ans_subdirectories_to_watch)) 174 175 176if __name__ == '__main__': 177 unittest.main() 178