blob: 7a42de015213c5d103edcfc6cec7c17e97f58c6b [file] [log] [blame]
Mike Frysinger9d96f582021-09-28 11:27:24 -04001# Copyright 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the platform_utils.py module."""
16
17import os
18import tempfile
19import unittest
20
21import platform_utils
22
23
24class RemoveTests(unittest.TestCase):
Gavin Makea2e3302023-03-11 06:46:20 +000025 """Check remove() helper."""
Mike Frysinger9d96f582021-09-28 11:27:24 -040026
Gavin Makea2e3302023-03-11 06:46:20 +000027 def testMissingOk(self):
28 """Check missing_ok handling."""
29 with tempfile.TemporaryDirectory() as tmpdir:
30 path = os.path.join(tmpdir, "test")
Mike Frysinger9d96f582021-09-28 11:27:24 -040031
Gavin Makea2e3302023-03-11 06:46:20 +000032 # Should not fail.
33 platform_utils.remove(path, missing_ok=True)
Mike Frysinger9d96f582021-09-28 11:27:24 -040034
Gavin Makea2e3302023-03-11 06:46:20 +000035 # Should fail.
36 self.assertRaises(OSError, platform_utils.remove, path)
37 self.assertRaises(
38 OSError, platform_utils.remove, path, missing_ok=False
39 )
Mike Frysinger9d96f582021-09-28 11:27:24 -040040
Gavin Makea2e3302023-03-11 06:46:20 +000041 # Should not fail if it exists.
42 open(path, "w").close()
43 platform_utils.remove(path, missing_ok=True)
44 self.assertFalse(os.path.exists(path))
Mike Frysinger9d96f582021-09-28 11:27:24 -040045
Gavin Makea2e3302023-03-11 06:46:20 +000046 open(path, "w").close()
47 platform_utils.remove(path)
48 self.assertFalse(os.path.exists(path))
Mike Frysinger9d96f582021-09-28 11:27:24 -040049
Gavin Makea2e3302023-03-11 06:46:20 +000050 open(path, "w").close()
51 platform_utils.remove(path, missing_ok=False)
52 self.assertFalse(os.path.exists(path))