summaryrefslogtreecommitdiff
path: root/buildscripts/tests/resmokelib/utils/test_rmtree.py
blob: 1239558feb79cc2cd55f886ea95b4c047da6ef1e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
""" Unit tests for utils.rmtree. """

import os
import shutil
import sys
import tempfile
import unittest

from buildscripts.resmokelib import utils

# pylint: disable=missing-docstring,protected-access


def rmtree(dir_root):
    """Invoke utils.rmtree(dir_root) and return True if removed."""
    utils.rmtree(dir_root)
    return not os.path.exists(dir_root)


def create_file(path):
    """Create file named 'path'."""
    with open(path, "w") as fh:
        fh.write("")


def ascii_filesystemencoding():
    """Return True if the file system encoding is type ASCII.

    See https://www.iana.org/assignments/character-sets/character-sets.xhtml.
    """
    encoding = sys.getfilesystemencoding()
    return encoding.startswith("ANSI_X3.4") or encoding == "US-ASCII"


def string_for_ascii_filesystem_encoding(path):
    """Return encoded string type for unicode on ASCII file system encoding.

    Some file system encodings are set to ASCII if LANG=C or LC_ALL=C is specified.
    """
    if ascii_filesystemencoding() and isinstance(path, str):
        return path.encode("utf-8")
    return path


class RmtreeTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.temp_dir_root = tempfile.mkdtemp()
        cls.cwd = os.getcwd()

    @classmethod
    def tearDownClass(cls):
        os.chdir(cls.cwd)
        shutil.rmtree(cls.temp_dir_root, ignore_errors=True)

    def do_test(self, name):
        pass

    def test_ascii(self):
        # ASCII name
        self.do_test("ascii")

    def test_unicode(self):
        # Unicode name
        self.do_test("unicode")

    def test_greek(self):
        # Name with Greek
        self.do_test(string_for_ascii_filesystem_encoding("ελληνικά"))

    def test_japanese(self):
        # Name with Japanese
        self.do_test(string_for_ascii_filesystem_encoding("会社案"))


class RmtreeFileTests(RmtreeTestCase):
    def do_test(self, file_name):  # pylint: disable=arguments-differ
        """Execute file test."""
        temp_dir = tempfile.mkdtemp(dir=self.temp_dir_root)
        os.chdir(temp_dir)
        create_file(file_name)
        os.chdir(self.temp_dir_root)
        self.assertTrue(rmtree(temp_dir))


class RmtreeDirectoryTests(RmtreeTestCase):
    def do_test(self, dir_name):  # pylint: disable=arguments-differ
        """Execute directory test."""
        os.chdir(self.temp_dir_root)
        os.mkdir(dir_name)
        self.assertTrue(rmtree(dir_name))


class RmtreeDirectoryWithNonAsciiTests(RmtreeTestCase):
    def do_test(self, name):
        """Execute directory with non-ASCII file test."""
        os.chdir(self.temp_dir_root)
        os.mkdir(name)
        os.chdir(name)
        create_file(name)
        os.chdir(self.temp_dir_root)
        self.assertTrue(rmtree(name))