aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_utils/py_utils/py_utils_unittest.py
blob: e614a4541c05f8007644333c9dc1824b51095e5f (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
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
import unittest

import py_utils


class PathTest(unittest.TestCase):

  def testIsExecutable(self):
    self.assertFalse(py_utils.IsExecutable('nonexistent_file'))
    # We use actual files on disk instead of pyfakefs because the executable is
    # set different on win that posix platforms and pyfakefs doesn't support
    # win platform well.
    self.assertFalse(py_utils.IsExecutable(_GetFileInTestDir('foo.txt')))
    self.assertTrue(py_utils.IsExecutable(sys.executable))


def _GetFileInTestDir(file_name):
  return os.path.join(os.path.dirname(__file__), 'test_data', file_name)


class WaitForTest(unittest.TestCase):

  def testWaitForTrue(self):
    def ReturnTrue():
      return True
    self.assertTrue(py_utils.WaitFor(ReturnTrue, .1))

  def testWaitForFalse(self):
    def ReturnFalse():
      return False

    with self.assertRaises(py_utils.TimeoutException):
      py_utils.WaitFor(ReturnFalse, .1)

  def testWaitForEventuallyTrue(self):
    # Use list to pass to inner function in order to allow modifying the
    # variable from the outer scope.
    c = [0]
    def ReturnCounterBasedValue():
      c[0] += 1
      return c[0] > 2

    self.assertTrue(py_utils.WaitFor(ReturnCounterBasedValue, .5))

  def testWaitForTrueLambda(self):
    self.assertTrue(py_utils.WaitFor(lambda: True, .1))

  def testWaitForFalseLambda(self):
    with self.assertRaises(py_utils.TimeoutException):
      py_utils.WaitFor(lambda: False, .1)