Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 1 | # 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 | |
| 17 | import os |
| 18 | import tempfile |
| 19 | import unittest |
| 20 | |
| 21 | import platform_utils |
| 22 | |
| 23 | |
| 24 | class RemoveTests(unittest.TestCase): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 25 | """Check remove() helper.""" |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 26 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 27 | def testMissingOk(self): |
| 28 | """Check missing_ok handling.""" |
| 29 | with tempfile.TemporaryDirectory() as tmpdir: |
| 30 | path = os.path.join(tmpdir, "test") |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 31 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | # Should not fail. |
| 33 | platform_utils.remove(path, missing_ok=True) |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 34 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 35 | # Should fail. |
| 36 | self.assertRaises(OSError, platform_utils.remove, path) |
| 37 | self.assertRaises( |
| 38 | OSError, platform_utils.remove, path, missing_ok=False |
| 39 | ) |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 40 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 41 | # 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 Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 45 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 46 | open(path, "w").close() |
| 47 | platform_utils.remove(path) |
| 48 | self.assertFalse(os.path.exists(path)) |
Mike Frysinger | 9d96f58 | 2021-09-28 11:27:24 -0400 | [diff] [blame] | 49 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 50 | open(path, "w").close() |
| 51 | platform_utils.remove(path, missing_ok=False) |
| 52 | self.assertFalse(os.path.exists(path)) |