aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/android/junction_test.py
blob: e3ff4211aed4e1270ff50cafc914f9c7068fd7fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# 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
#
#    http://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.
"""Tests for TempJunction."""

import os
import unittest

from src.test.py.bazel import test_base
from tools.android import junction


class JunctionTest(test_base.TestBase):
  """Unit tests for junction.py."""

  def _AssertCreateJunctionWhenTargetsParentsDontExist(self, max_path=None):

    def tempdir():
      return self.ScratchDir("junc temp")

    target = self.Path("this directory/should not\\yet exist")
    self.assertFalse(os.path.exists(os.path.dirname(os.path.dirname(target))))
    # Make the `target` path a non-normalized Windows path with a space in it
    # which doesn't even exist.
    # TempJunction should still work; it should:
    # - normalize the path, and
    # - create all directories on the path
    # target = os.path.dirname(target) + "/junc target"
    juncpath = None
    with junction.TempJunction(
        target, testonly_mkdtemp=tempdir, testonly_maxpath=max_path) as j:
      juncpath = j
      # Ensure that `j` created the junction.
      self.assertTrue(os.path.exists(target))
      self.assertTrue(os.path.exists(juncpath))
      self.assertTrue(juncpath.endswith(os.path.join("junc temp", "j")))
      self.assertTrue(os.path.isabs(juncpath))
      # Create a file under the junction.
      filepath = os.path.join(juncpath, "some file.txt")
      with open(filepath, "w") as f:
        f.write("hello")
      # Ensure we can reach the file via the junction and the target directory.
      self.assertTrue(os.path.exists(os.path.join(target, "some file.txt")))
      self.assertTrue(os.path.exists(os.path.join(juncpath, "some file.txt")))
    # Ensure that after the `with` block the junction and temp directories no
    # longer exist, but we can still reach the file via the target directory.
    self.assertTrue(os.path.exists(os.path.join(target, "some file.txt")))
    self.assertFalse(os.path.exists(os.path.join(juncpath, "some file.txt")))
    self.assertFalse(os.path.exists(juncpath))
    self.assertFalse(os.path.exists(os.path.dirname(juncpath)))

  def testCreateJunctionWhenTargetsParentsDontExistAndPathIsShort(self):
    self._AssertCreateJunctionWhenTargetsParentsDontExist()

  def testCreateJunctionWhenTargetsParentsDontExistAndPathIsLong(self):
    self._AssertCreateJunctionWhenTargetsParentsDontExist(1)

  def testCannotCreateJunction(self):

    def tempdir():
      return self.ScratchDir("junc temp")

    target = self.ScratchDir("junc target")
    # Make the `target` path a non-normalized Windows path with a space in it.
    # TempJunction should still work.
    target = os.path.dirname(target) + "/junc target"
    with junction.TempJunction(target, testonly_mkdtemp=tempdir) as j:
      self.assertTrue(os.path.exists(j))
      try:
        # Ensure that TempJunction raises a JunctionCreationError if it cannot
        # create a junction. In this case the junction already exists in that
        # directory.
        with junction.TempJunction(target, testonly_mkdtemp=tempdir) as _:
          self.fail("Expected exception")
      except junction.JunctionCreationError:
        pass  # expected


if __name__ == "__main__":
  unittest.main()