|
| 1 | +# Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for TempJunction.""" |
| 15 | + |
| 16 | +import os |
| 17 | +import unittest |
| 18 | + |
| 19 | +from src.test.py.bazel import test_base |
| 20 | +from tools.android import junction |
| 21 | + |
| 22 | + |
| 23 | +class JunctionTest(test_base.TestBase): |
| 24 | + """Unit tests for junction.py.""" |
| 25 | + |
| 26 | + def _AssertCreateJunctionWhenTargetsParentsDontExist(self, max_path=None): |
| 27 | + |
| 28 | + def tempdir(): |
| 29 | + return self.ScratchDir("junc temp") |
| 30 | + |
| 31 | + target = self.Path("this directory/should not\\yet exist") |
| 32 | + self.assertFalse(os.path.exists(os.path.dirname(os.path.dirname(target)))) |
| 33 | + # Make the `target` path a non-normalized Windows path with a space in it |
| 34 | + # which doesn't even exist. |
| 35 | + # TempJunction should still work; it should: |
| 36 | + # - normalize the path, and |
| 37 | + # - create all directories on the path |
| 38 | + # target = os.path.dirname(target) + "/junc target" |
| 39 | + juncpath = None |
| 40 | + with junction.TempJunction( |
| 41 | + target, testonly_mkdtemp=tempdir, testonly_maxpath=max_path) as j: |
| 42 | + juncpath = j |
| 43 | + # Ensure that `j` created the junction. |
| 44 | + self.assertTrue(os.path.exists(target)) |
| 45 | + self.assertTrue(os.path.exists(juncpath)) |
| 46 | + self.assertTrue( |
| 47 | + juncpath.endswith(os.path.join("junc temp", "j"))) |
| 48 | + self.assertTrue(os.path.isabs(juncpath)) |
| 49 | + # Create a file under the junction. |
| 50 | + filepath = os.path.join(juncpath, "some file.txt") |
| 51 | + with open(filepath, "w") as f: |
| 52 | + f.write("hello") |
| 53 | + # Ensure we can reach the file via the junction and the target directory. |
| 54 | + self.assertTrue(os.path.exists(os.path.join(target, "some file.txt"))) |
| 55 | + self.assertTrue(os.path.exists(os.path.join(juncpath, "some file.txt"))) |
| 56 | + # Ensure that after the `with` block the junction and temp directories no |
| 57 | + # longer exist, but we can still reach the file via the target directory. |
| 58 | + self.assertTrue(os.path.exists(os.path.join(target, "some file.txt"))) |
| 59 | + self.assertFalse(os.path.exists(os.path.join(juncpath, "some file.txt"))) |
| 60 | + self.assertFalse(os.path.exists(juncpath)) |
| 61 | + self.assertFalse(os.path.exists(os.path.dirname(juncpath))) |
| 62 | + |
| 63 | + def testCreateJunctionWhenTargetsParentsDontExistAndPathIsShort(self): |
| 64 | + self._AssertCreateJunctionWhenTargetsParentsDontExist() |
| 65 | + |
| 66 | + def testCreateJunctionWhenTargetsParentsDontExistAndPathIsLong(self): |
| 67 | + self._AssertCreateJunctionWhenTargetsParentsDontExist(1) |
| 68 | + |
| 69 | + def testCannotCreateJunction(self): |
| 70 | + |
| 71 | + def tempdir(): |
| 72 | + return self.ScratchDir("junc temp") |
| 73 | + |
| 74 | + target = self.ScratchDir("junc target") |
| 75 | + # Make the `target` path a non-normalized Windows path with a space in it. |
| 76 | + # TempJunction should still work. |
| 77 | + target = os.path.dirname(target) + "/junc target" |
| 78 | + with junction.TempJunction(target, testonly_mkdtemp=tempdir) as j: |
| 79 | + self.assertTrue(os.path.exists(j)) |
| 80 | + if os.name != "nt": |
| 81 | + # Ensure that TempJunction raises a JunctionCreationError if it cannot |
| 82 | + # create a junction. In this case the junction already exists in that |
| 83 | + # directory. |
| 84 | + # On Windows, we error out in a different way, so we skip the test and |
| 85 | + # rely on this particular feature being correct on the other platforms |
| 86 | + # as sufficient. |
| 87 | + self.assertRaises( |
| 88 | + junction.JunctionCreationError, |
| 89 | + junction.TempJunction, |
| 90 | + target, |
| 91 | + testonly_mkdtemp=tempdir) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + unittest.main() |
0 commit comments