|
8 | 8 | from test import support |
9 | 9 | from test.support import os_helper |
10 | 10 |
|
| 11 | +FEDORA_OS_RELEASE = """\ |
| 12 | +NAME=Fedora |
| 13 | +VERSION="32 (Thirty Two)" |
| 14 | +ID=fedora |
| 15 | +VERSION_ID=32 |
| 16 | +VERSION_CODENAME="" |
| 17 | +PLATFORM_ID="platform:f32" |
| 18 | +PRETTY_NAME="Fedora 32 (Thirty Two)" |
| 19 | +ANSI_COLOR="0;34" |
| 20 | +LOGO=fedora-logo-icon |
| 21 | +CPE_NAME="cpe:/o:fedoraproject:fedora:32" |
| 22 | +HOME_URL="https://fedoraproject.org/" |
| 23 | +DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/" |
| 24 | +SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help" |
| 25 | +BUG_REPORT_URL="https://bugzilla.redhat.com/" |
| 26 | +REDHAT_BUGZILLA_PRODUCT="Fedora" |
| 27 | +REDHAT_BUGZILLA_PRODUCT_VERSION=32 |
| 28 | +REDHAT_SUPPORT_PRODUCT="Fedora" |
| 29 | +REDHAT_SUPPORT_PRODUCT_VERSION=32 |
| 30 | +PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy" |
| 31 | +""" |
| 32 | + |
| 33 | +UBUNTU_OS_RELEASE = """\ |
| 34 | +NAME="Ubuntu" |
| 35 | +VERSION="20.04.1 LTS (Focal Fossa)" |
| 36 | +ID=ubuntu |
| 37 | +ID_LIKE=debian |
| 38 | +PRETTY_NAME="Ubuntu 20.04.1 LTS" |
| 39 | +VERSION_ID="20.04" |
| 40 | +HOME_URL="https://www.ubuntu.com/" |
| 41 | +SUPPORT_URL="https://help.ubuntu.com/" |
| 42 | +BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" |
| 43 | +PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" |
| 44 | +VERSION_CODENAME=focal |
| 45 | +UBUNTU_CODENAME=focal |
| 46 | +""" |
| 47 | + |
| 48 | +TEST_OS_RELEASE = r""" |
| 49 | +# test data |
| 50 | +ID_LIKE="egg spam viking" |
| 51 | +EMPTY= |
| 52 | +# comments and empty lines are ignored |
| 53 | +
|
| 54 | +SINGLE_QUOTE='single' |
| 55 | +EMPTY_SINGLE='' |
| 56 | +DOUBLE_QUOTE="double" |
| 57 | +EMPTY_DOUBLE="" |
| 58 | +QUOTES="double\'s" |
| 59 | +SPECIALS="\$\`\\\'\"" |
| 60 | +# invalid lines |
| 61 | +=invalid |
| 62 | += |
| 63 | +INVALID |
| 64 | +IN-VALID=value |
| 65 | +IN VALID=value |
| 66 | +""" |
| 67 | + |
11 | 68 |
|
12 | 69 | class PlatformTest(unittest.TestCase): |
13 | 70 | def clear_caches(self): |
14 | 71 | platform._platform_cache.clear() |
15 | 72 | platform._sys_version_cache.clear() |
16 | 73 | platform._uname_cache = None |
| 74 | + platform._os_release_cache = None |
17 | 75 |
|
18 | 76 | def test_architecture(self): |
19 | 77 | res = platform.architecture() |
@@ -382,6 +440,54 @@ def test_macos(self): |
382 | 440 | self.assertEqual(platform.platform(terse=1), expected_terse) |
383 | 441 | self.assertEqual(platform.platform(), expected) |
384 | 442 |
|
| 443 | + def test_freedesktop_os_release(self): |
| 444 | + self.addCleanup(self.clear_caches) |
| 445 | + self.clear_caches() |
| 446 | + |
| 447 | + if any(os.path.isfile(fn) for fn in platform._os_release_candidates): |
| 448 | + info = platform.freedesktop_os_release() |
| 449 | + self.assertIn("NAME", info) |
| 450 | + self.assertIn("ID", info) |
| 451 | + |
| 452 | + info["CPYTHON_TEST"] = "test" |
| 453 | + self.assertNotIn( |
| 454 | + "CPYTHON_TEST", |
| 455 | + platform.freedesktop_os_release() |
| 456 | + ) |
| 457 | + else: |
| 458 | + with self.assertRaises(OSError): |
| 459 | + platform.freedesktop_os_release() |
| 460 | + |
| 461 | + def test_parse_os_release(self): |
| 462 | + info = platform._parse_os_release(FEDORA_OS_RELEASE.splitlines()) |
| 463 | + self.assertEqual(info["NAME"], "Fedora") |
| 464 | + self.assertEqual(info["ID"], "fedora") |
| 465 | + self.assertNotIn("ID_LIKE", info) |
| 466 | + self.assertEqual(info["VERSION_CODENAME"], "") |
| 467 | + |
| 468 | + info = platform._parse_os_release(UBUNTU_OS_RELEASE.splitlines()) |
| 469 | + self.assertEqual(info["NAME"], "Ubuntu") |
| 470 | + self.assertEqual(info["ID"], "ubuntu") |
| 471 | + self.assertEqual(info["ID_LIKE"], "debian") |
| 472 | + self.assertEqual(info["VERSION_CODENAME"], "focal") |
| 473 | + |
| 474 | + info = platform._parse_os_release(TEST_OS_RELEASE.splitlines()) |
| 475 | + expected = { |
| 476 | + "ID": "linux", |
| 477 | + "NAME": "Linux", |
| 478 | + "PRETTY_NAME": "Linux", |
| 479 | + "ID_LIKE": "egg spam viking", |
| 480 | + "EMPTY": "", |
| 481 | + "DOUBLE_QUOTE": "double", |
| 482 | + "EMPTY_DOUBLE": "", |
| 483 | + "SINGLE_QUOTE": "single", |
| 484 | + "EMPTY_SINGLE": "", |
| 485 | + "QUOTES": "double's", |
| 486 | + "SPECIALS": "$`\\'\"", |
| 487 | + } |
| 488 | + self.assertEqual(info, expected) |
| 489 | + self.assertEqual(len(info["SPECIALS"]), 5) |
| 490 | + |
385 | 491 |
|
386 | 492 | if __name__ == '__main__': |
387 | 493 | unittest.main() |
0 commit comments