1#!/usr/bin/env python
2#
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import os
19import random
20import string
21import unittest
22
23from vts.utils.python.io import file_util
24
25
26class FileUtilTest(unittest.TestCase):
27    def setUp(self):
28        """Resets generated directory list"""
29        self._dirs = []
30
31    def tearDown(self):
32        """Removes existing directories"""
33        for path in self._dirs:
34            file_util.Rmdirs(path)
35
36    def testMakeAndRemoveDirs(self):
37        """Tests making and removing directories """
38        dir_name = ''.join(
39            random.choice(string.ascii_lowercase + string.digits)
40            for _ in range(12))
41        self._dirs.append(dir_name)
42
43        # make non-existing directory
44        result = file_util.Makedirs(dir_name)
45        self.assertEqual(True, result)
46
47        # make existing directory
48        result = file_util.Makedirs(dir_name)
49        self.assertEqual(False, result)
50
51        # delete existing directory
52        result = file_util.Rmdirs(dir_name)
53        self.assertEqual(True, result)
54
55        # delete non-existing directory
56        result = file_util.Rmdirs(dir_name)
57        self.assertEqual(False, result)
58
59    def testMakeTempDir(self):
60        """Tests making temp directory """
61        base_dir = ''.join(
62            random.choice(string.ascii_lowercase + string.digits)
63            for _ in range(12))
64        self._dirs.append(base_dir)
65
66        # make temp directory
67        result = file_util.MakeTempDir(base_dir)
68        self.assertTrue(os.path.join(base_dir, "tmp"))
69
70    def testMakeException(self):
71        """Tests making directory and raise exception """
72        dir_name = ''.join(
73            random.choice(string.ascii_lowercase + string.digits)
74            for _ in range(12))
75        self._dirs.append(dir_name)
76
77        file_util.Makedirs(dir_name)
78        self.assertRaises(Exception,
79                          file_util.Makedirs(dir_name, skip_if_exists=False))
80
81    def testRemoveException(self):
82        """Tests removing directory and raise exception """
83        dir_name = ''.join(
84            random.choice(string.ascii_lowercase + string.digits)
85            for _ in range(12))
86        self._dirs.append(dir_name)
87
88        link_name = ''.join(
89            random.choice(string.ascii_lowercase + string.digits)
90            for _ in range(12))
91
92        file_util.Makedirs(dir_name)
93        os.symlink(dir_name, link_name)
94        try:
95            self.assertRaises(Exception,
96                              file_util.Rmdirs(link_name, ignore_errors=False))
97        finally:
98            os.remove(link_name)
99
100
101if __name__ == "__main__":
102    unittest.main()
103