fix(test): make `//tests/mappings/filter_directory` work on Windows
The `filter_directory` tests were failing on Windows:
```
ERROR: test_directory_structure_matches_global (__main__.DirectoryStructureTest.test_directory_structure_matches_global)
[...]
os.path.isdir(real_directory_root),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[...]
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
```
This was because `Rlocation()` expects POSIX-style paths (forward
slashes), but `os.path.join()` produces backslashes on Windows.
After fixing it, a second error appeared:
```
FAIL: test_directory_structure_matches_global (__main__.DirectoryStructureTest.test_directory_structure_matches_global)
AssertionError: Items in the first set but not the second:
'extra/a'
'extra/subdir/c'
'extra/b'
'extra/subdir/d'
Items in the second set but not the first:
'extra\\subdir\\c'
'extra\\a'
'extra\\b'
'extra\\subdir\\d' : Directory structure mismatch
```
This was because paths collected via `os.walk()` also produce
backslashes on Windows against expected paths returned by `Rlocation`
that always use forward slashes.
The change consists in leveraging `pathlib` for that purpose:
`Path().as_posix()`, `Path.rglob()` and `Path.relative_to()`.
This enables the 9 `filter_directory` tests in Windows CI.diff --git a/.bazelci/tests.yml b/.bazelci/tests.yml
index 13056f4..c1e3b5e 100644
--- a/.bazelci/tests.yml
+++ b/.bazelci/tests.yml
@@ -50,7 +50,6 @@
# Bazel might be broken w.r.t. Unicode processing for windows. Multiple issues:
# https://github.com/bazelbuild/bazel/issues?q=is%3Aissue+is%3Aopen+%2Bunicode+%2Bwindows+
- "-//tests/mappings:utf8_manifest_test"
- - "-//tests/mappings/filter_directory/..."
- "-//tests/zip:unicode_test"
# rpmbuild(8) is not supported on Windows
- "-//tests/rpm/..."
diff --git a/tests/mappings/filter_directory/inspect_directory.py.tpl b/tests/mappings/filter_directory/inspect_directory.py.tpl
index cc935cf..96487e3 100644
--- a/tests/mappings/filter_directory/inspect_directory.py.tpl
+++ b/tests/mappings/filter_directory/inspect_directory.py.tpl
@@ -17,6 +17,7 @@
import json
import os
import unittest
+from pathlib import Path
from python.runfiles import runfiles
DIRECTORY_ROOT = "%DIRECTORY_ROOT%"
@@ -29,27 +30,22 @@
self.runfiles = runfiles.Create()
def test_directory_structure_matches_global(self):
- real_directory_root = self.runfiles.Rlocation(
- os.path.join(os.environ["TEST_WORKSPACE"], DIRECTORY_ROOT)
- )
+ real_directory_root = Path(self.runfiles.Rlocation(
+ (Path(os.environ["TEST_WORKSPACE"]) / DIRECTORY_ROOT).as_posix()
+ ))
# This may be a bazel bug -- shouldn't an empty directory be passed in
# anyway?
self.assertTrue(
- os.path.isdir(real_directory_root),
+ real_directory_root.is_dir(),
"TreeArtifact root does not exist, is the input empty?",
)
expected_set = set(json.loads(EXPECTED_STRUCTURE))
actual_set = set()
- for root, dirs, files in os.walk(real_directory_root):
- if root != real_directory_root:
- rel_root = os.path.relpath(root, real_directory_root)
- else:
- # We are in the root. Don't bother with path relativization.
- rel_root = ''
- for f in files:
- actual_set.add(os.path.join(rel_root, f))
+ for file_path in real_directory_root.rglob("*"):
+ if file_path.is_file():
+ actual_set.add(file_path.relative_to(real_directory_root).as_posix())
self.assertEqual(
expected_set,