| # Copyright 2026 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Test to ensure MODULE.bazel.lock does not contain absolute filesystem paths.""" |
| |
| import unittest |
| import re |
| import os |
| |
| class LockfilePortabilityTest(unittest.TestCase): |
| def test_no_absolute_paths(self): |
| # Locate lockfile via runfiles |
| runfiles_dir = os.environ.get("TEST_SRCDIR") |
| workspace = os.environ.get("TEST_WORKSPACE", "examples") |
| |
| if runfiles_dir: |
| lockfile_path = os.path.join(runfiles_dir, workspace, "MODULE.bazel.lock") |
| else: |
| # Fallback for manual running |
| lockfile_path = "MODULE.bazel.lock" |
| |
| self.assertTrue(os.path.exists(lockfile_path), f"Lockfile not found at {lockfile_path}") |
| |
| with open(lockfile_path, "r") as f: |
| content = f.read() |
| |
| # Patterns that indicate absolute paths: |
| # 1. Linux/macOS home directories: /home/username/ or /Users/username/ |
| # 2. Linux/macOS temp/var directories: /var/folders/... |
| # 3. General absolute paths that shouldn't be there (e.g. starting with /usr/local/google/home) |
| forbidden_patterns = [ |
| r"/usr/local/google/home", |
| r"/home/[^/]+", |
| r"/Users/[^/]+", |
| r"/private/var/[^/]+", |
| ] |
| |
| for pattern in forbidden_patterns: |
| matches = re.findall(pattern, content) |
| self.assertEqual(len(matches), 0, f"Found forbidden absolute path pattern '{pattern}': {matches}") |
| |
| if __name__ == "__main__": |
| unittest.main() |