pw_watch: Update watch_test.py to get it passing
Also, use a relative import for windows_env_start.py to fix Pylint
complaints.
Change-Id: Ic9f52de12bef6ccc937d2d34bf7966fda6b1f1ea
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/32741
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Rob Mohr <mohrr@google.com>
diff --git a/pw_env_setup/py/pw_env_setup/windows_env_start.py b/pw_env_setup/py/pw_env_setup/windows_env_start.py
index 62770a8..4d33075 100644
--- a/pw_env_setup/py/pw_env_setup/windows_env_start.py
+++ b/pw_env_setup/py/pw_env_setup/windows_env_start.py
@@ -26,7 +26,7 @@
import os
import sys
-from colors import Color, enable_colors # type: ignore
+from .colors import Color, enable_colors # type: ignore
_PIGWEED_BANNER = u'''
▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
diff --git a/pw_watch/py/BUILD.gn b/pw_watch/py/BUILD.gn
index f2105fe..e14d675 100644
--- a/pw_watch/py/BUILD.gn
+++ b/pw_watch/py/BUILD.gn
@@ -22,8 +22,8 @@
"pw_watch/__init__.py",
"pw_watch/debounce.py",
"pw_watch/watch.py",
- "watch_test.py",
]
+ tests = [ "watch_test.py" ]
pylintrc = "$dir_pigweed/.pylintrc"
python_deps = [ "$dir_pw_cli/py" ]
}
diff --git a/pw_watch/py/watch_test.py b/pw_watch/py/watch_test.py
index ff79e14..6c9323a 100755
--- a/pw_watch/py/watch_test.py
+++ b/pw_watch/py/watch_test.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env python
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
@@ -14,26 +15,30 @@
"""Tests for pw_watch.minimal_watch_directories."""
import unittest
-import os
import tempfile
from pathlib import Path
-import pw_watch.watch as watch
-
-
-def make_tree(root, directories):
- for directory in directories:
- os.mkdir(Path(root, directory))
+from pw_watch import watch
class TestMinimalWatchDirectories(unittest.TestCase):
"""Tests for pw_watch.watch.minimal_watch_directories."""
+ def setUp(self):
+ self._tempdir = tempfile.TemporaryDirectory()
+ self._root = Path(self._tempdir.name)
+
+ def tearDown(self):
+ self._tempdir.cleanup()
+
+ def make_tree(self, *directories: str) -> None:
+ for directory in directories:
+ self._root.joinpath(directory).mkdir(parents=True)
+
def test_empty_directory(self):
subdirectories_to_watch = []
- with tempfile.TemporaryDirectory() as tmpdir:
- ans_subdirectories_to_watch = [(Path(tmpdir), False)]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, 'f1')
+ ans_subdirectories_to_watch = [(self._root, False)]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, 'f1')
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -41,15 +46,14 @@
def test_non_exist_directories_to_exclude(self):
subdirectories_to_watch = []
exclude_list = ['f3']
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, ['f1', 'f2'])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f1'), True),
- (Path(tmpdir, 'f2'), True),
- (Path(tmpdir), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree('f1', 'f2')
+ ans_subdirectories_to_watch = [
+ (self._root / 'f1', True),
+ (self._root / 'f2', True),
+ (self._root, False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -57,20 +61,17 @@
def test_one_layer_directories(self):
subdirectories_to_watch = []
exclude_list = ['f1']
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, [
- 'f1',
- 'f1/f1',
- 'f1/f2',
- 'f2',
- 'f2/f1',
- ])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f2'), True),
- (Path(tmpdir), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree(
+ 'f1/f1',
+ 'f1/f2',
+ 'f2/f1',
+ )
+ ans_subdirectories_to_watch = [
+ (self._root / 'f2', True),
+ (self._root, False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -78,22 +79,19 @@
def test_two_layers_direcories(self):
subdirectories_to_watch = []
exclude_list = ['f1/f2']
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, [
- 'f1',
- 'f1/f1',
- 'f1/f2',
- 'f2',
- 'f2/f1',
- ])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f2'), True),
- (Path(tmpdir, 'f1/f1'), True),
- (Path(tmpdir), False),
- (Path(tmpdir, 'f1'), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree(
+ 'f1/f1',
+ 'f1/f2',
+ 'f2/f1',
+ )
+ ans_subdirectories_to_watch = [
+ (self._root / 'f2', True),
+ (self._root / 'f1/f1', True),
+ (self._root, False),
+ (self._root / 'f1', False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -101,21 +99,18 @@
def test_empty_exclude_list(self):
subdirectories_to_watch = []
exclude_list = []
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, [
- 'f1',
- 'f1/f1',
- 'f1/f2',
- 'f2',
- 'f2/f1',
- ])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f2'), True),
- (Path(tmpdir, 'f1'), True),
- (Path(tmpdir), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree(
+ 'f1/f1',
+ 'f1/f2',
+ 'f2/f1',
+ )
+ ans_subdirectories_to_watch = [
+ (self._root / 'f2', True),
+ (self._root / 'f1', True),
+ (self._root, False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -128,21 +123,24 @@
'f3/f1',
'f3/f3',
]
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, [
- 'f1', 'f1/f1', 'f1/f2', 'f2', 'f2/f1', 'f3', 'f3/f1', 'f3/f2',
- 'f3/f3'
- ])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f2'), True),
- (Path(tmpdir, 'f1/f1'), True),
- (Path(tmpdir, 'f3/f2'), True),
- (Path(tmpdir), False),
- (Path(tmpdir, 'f1'), False),
- (Path(tmpdir, 'f3'), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree(
+ 'f1/f1',
+ 'f1/f2',
+ 'f2/f1',
+ 'f3/f1',
+ 'f3/f2',
+ 'f3/f3',
+ )
+ ans_subdirectories_to_watch = [
+ (self._root / 'f2', True),
+ (self._root / 'f1/f1', True),
+ (self._root / 'f3/f2', True),
+ (self._root, False),
+ (self._root / 'f1', False),
+ (self._root / 'f3', False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))
@@ -153,28 +151,23 @@
'f1/f1/f1/f1/f1',
'f1/f1/f1/f2',
]
- with tempfile.TemporaryDirectory() as tmpdir:
- make_tree(tmpdir, [
- 'f1',
- 'f1/f1',
- 'f1/f1/f1',
- 'f1/f1/f1/f1',
- 'f1/f1/f1/f1/f1',
- 'f1/f1/f1/f1/f2',
- 'f1/f1/f1/f1/f3',
- 'f1/f1/f1/f2',
- ])
- ans_subdirectories_to_watch = [
- (Path(tmpdir, 'f1/f1/f1/f1/f2'), True),
- (Path(tmpdir, 'f1/f1/f1/f1/f3'), True),
- (Path(tmpdir), False),
- (Path(tmpdir, 'f1'), False),
- (Path(tmpdir, 'f1/f1'), False),
- (Path(tmpdir, 'f1/f1/f1'), False),
- (Path(tmpdir, 'f1/f1/f1/f1'), False),
- ]
- subdirectories_to_watch = \
- watch.minimal_watch_directories(tmpdir, exclude_list)
+ self.make_tree(
+ 'f1/f1/f1/f1/f1',
+ 'f1/f1/f1/f1/f2',
+ 'f1/f1/f1/f1/f3',
+ 'f1/f1/f1/f2',
+ )
+ ans_subdirectories_to_watch = [
+ (self._root / 'f1/f1/f1/f1/f2', True),
+ (self._root / 'f1/f1/f1/f1/f3', True),
+ (self._root, False),
+ (self._root / 'f1', False),
+ (self._root / 'f1/f1', False),
+ (self._root / 'f1/f1/f1', False),
+ (self._root / 'f1/f1/f1/f1', False),
+ ]
+ subdirectories_to_watch = watch.minimal_watch_directories(
+ self._root, exclude_list)
self.assertEqual(set(subdirectories_to_watch),
set(ans_subdirectories_to_watch))