xref: /aosp_15_r20/prebuilts/sdk/update_prebuilts/test_utils.py (revision 344a7f5ef16c479e7a7f54ee6567a9d112f9e72b)
1#!/usr/bin/python3
2
3import os
4import tempfile
5import utils
6
7
8def test_append():
9    assert utils.append("x", "y") == "x, y"
10    assert utils.append(None, "y") == "y"
11
12
13def test_cp():
14    with tempfile.TemporaryDirectory() as tmpdir:
15        src_dir = os.path.join(tmpdir, "src")
16        src_dir_b = os.path.join(src_dir, "b")
17        dst_dir = os.path.join(tmpdir, "dst")
18        dst_dir_a = os.path.join(dst_dir, "a")
19
20        os.mkdir(src_dir)
21        os.mkdir(src_dir_b)
22        os.mkdir(dst_dir)
23        os.mkdir(dst_dir_a)
24
25        dst_dir_b = os.path.join(dst_dir, "b")
26
27        utils.cp(src_dir, dst_dir)
28
29        # Destination contents are not preserved.
30        assert not os.path.exists(dst_dir_a)
31
32        # Source tree is copied to destination.
33        assert os.path.exists(dst_dir_b)
34
35
36def test_cp_mk_dst():
37    with tempfile.TemporaryDirectory() as tmpdir:
38        src_dir = os.path.join(tmpdir, "src")
39        src_dir_b = os.path.join(src_dir, "b")
40        dst_dir = os.path.join(tmpdir, "dst")
41
42        os.mkdir(src_dir)
43        os.mkdir(src_dir_b)
44
45        dst_dir_b = os.path.join(dst_dir, "b")
46
47        utils.cp(src_dir, dst_dir)
48
49        # Missing destination is created.
50        assert os.path.exists(dst_dir)
51
52        # Source tree is copied to destination.
53        assert os.path.exists(dst_dir_b)
54
55
56def test_mv():
57    with tempfile.TemporaryDirectory() as tmpdir:
58        src_dir = os.path.join(tmpdir, "src")
59        src_dir_b = os.path.join(src_dir, "b")
60        dst_dir = os.path.join(tmpdir, "dst")
61        dst_dir_a = os.path.join(dst_dir, "a")
62
63        os.mkdir(src_dir)
64        os.mkdir(src_dir_b)
65        os.mkdir(dst_dir)
66        os.mkdir(dst_dir_a)
67
68        dst_dir_b = os.path.join(dst_dir, "b")
69
70        utils.mv(src_dir, dst_dir)
71
72        # Destination contents are not preserved.
73        assert not os.path.exists(dst_dir_a)
74
75        # Source tree is copied to destination.
76        assert os.path.exists(dst_dir_b)
77
78        # Source tree is removed.
79        assert not os.path.exists(src_dir)
80
81
82def test_mv_mk_dst():
83    with tempfile.TemporaryDirectory() as tmpdir:
84        src_dir = os.path.join(tmpdir, "src")
85        src_dir_b = os.path.join(src_dir, "b")
86        dst_dir = os.path.join(tmpdir, "dst")
87
88        os.mkdir(src_dir)
89        os.mkdir(src_dir_b)
90
91        dst_dir_b = os.path.join(dst_dir, "b")
92
93        utils.mv(src_dir, dst_dir)
94
95        # Missing destination is created.
96        assert os.path.exists(dst_dir)
97
98        # Source tree is copied to destination.
99        assert os.path.exists(dst_dir_b)
100
101        # Source tree is removed.
102        assert not os.path.exists(src_dir)
103
104
105def test_rm():
106    with tempfile.TemporaryDirectory() as tmpdir:
107        src_dir = os.path.join(tmpdir, "src")
108        src_dir_b = os.path.join(src_dir, "b")
109
110        os.mkdir(src_dir)
111        os.mkdir(src_dir_b)
112
113        utils.rm(src_dir)
114
115        # Source tree is removed.
116        assert not os.path.exists(src_dir)
117
118
119if __name__ == "__main__":
120    test_append()
121    test_cp()
122    test_cp_mk_dst()
123    test_mv()
124    test_rm()
125