1import unittest 2from random import shuffle 3 4from python.private.pypi.whl_installer.platform import ( 5 OS, 6 Arch, 7 Platform, 8 host_interpreter_minor_version, 9) 10 11 12class MinorVersionTest(unittest.TestCase): 13 def test_host(self): 14 host = host_interpreter_minor_version() 15 self.assertIsNotNone(host) 16 17 18class PlatformTest(unittest.TestCase): 19 def test_can_get_host(self): 20 host = Platform.host() 21 self.assertIsNotNone(host) 22 self.assertEqual(1, len(Platform.from_string("host"))) 23 self.assertEqual(host, Platform.from_string("host")) 24 25 def test_can_get_linux_x86_64_without_py_version(self): 26 got = Platform.from_string("linux_x86_64") 27 want = Platform(os=OS.linux, arch=Arch.x86_64) 28 self.assertEqual(want, got[0]) 29 30 def test_can_get_specific_from_string(self): 31 got = Platform.from_string("cp33_linux_x86_64") 32 want = Platform(os=OS.linux, arch=Arch.x86_64, minor_version=3) 33 self.assertEqual(want, got[0]) 34 35 def test_can_get_all_for_py_version(self): 36 cp39 = Platform.all(minor_version=9) 37 self.assertEqual(18, len(cp39), f"Got {cp39}") 38 self.assertEqual(cp39, Platform.from_string("cp39_*")) 39 40 def test_can_get_all_for_os(self): 41 linuxes = Platform.all(OS.linux, minor_version=9) 42 self.assertEqual(6, len(linuxes)) 43 self.assertEqual(linuxes, Platform.from_string("cp39_linux_*")) 44 45 def test_can_get_all_for_os_for_host_python(self): 46 linuxes = Platform.all(OS.linux) 47 self.assertEqual(6, len(linuxes)) 48 self.assertEqual(linuxes, Platform.from_string("linux_*")) 49 50 def test_specific_version_specializations(self): 51 any_py33 = Platform(minor_version=3) 52 53 # When 54 all_specializations = list(any_py33.all_specializations()) 55 56 want = ( 57 [any_py33] 58 + [ 59 Platform(arch=arch, minor_version=any_py33.minor_version) 60 for arch in Arch 61 ] 62 + [Platform(os=os, minor_version=any_py33.minor_version) for os in OS] 63 + Platform.all(minor_version=any_py33.minor_version) 64 ) 65 self.assertEqual(want, all_specializations) 66 67 def test_aarch64_specializations(self): 68 any_aarch64 = Platform(arch=Arch.aarch64) 69 all_specializations = list(any_aarch64.all_specializations()) 70 want = [ 71 Platform(os=None, arch=Arch.aarch64), 72 Platform(os=OS.linux, arch=Arch.aarch64), 73 Platform(os=OS.osx, arch=Arch.aarch64), 74 Platform(os=OS.windows, arch=Arch.aarch64), 75 ] 76 self.assertEqual(want, all_specializations) 77 78 def test_linux_specializations(self): 79 any_linux = Platform(os=OS.linux) 80 all_specializations = list(any_linux.all_specializations()) 81 want = [ 82 Platform(os=OS.linux, arch=None), 83 Platform(os=OS.linux, arch=Arch.x86_64), 84 Platform(os=OS.linux, arch=Arch.x86_32), 85 Platform(os=OS.linux, arch=Arch.aarch64), 86 Platform(os=OS.linux, arch=Arch.ppc), 87 Platform(os=OS.linux, arch=Arch.s390x), 88 Platform(os=OS.linux, arch=Arch.arm), 89 ] 90 self.assertEqual(want, all_specializations) 91 92 def test_osx_specializations(self): 93 any_osx = Platform(os=OS.osx) 94 all_specializations = list(any_osx.all_specializations()) 95 # NOTE @aignas 2024-01-14: even though in practice we would only have 96 # Python on osx aarch64 and osx x86_64, we return all arch posibilities 97 # to make the code simpler. 98 want = [ 99 Platform(os=OS.osx, arch=None), 100 Platform(os=OS.osx, arch=Arch.x86_64), 101 Platform(os=OS.osx, arch=Arch.x86_32), 102 Platform(os=OS.osx, arch=Arch.aarch64), 103 Platform(os=OS.osx, arch=Arch.ppc), 104 Platform(os=OS.osx, arch=Arch.s390x), 105 Platform(os=OS.osx, arch=Arch.arm), 106 ] 107 self.assertEqual(want, all_specializations) 108 109 def test_platform_sort(self): 110 platforms = [ 111 Platform(os=OS.linux, arch=None), 112 Platform(os=OS.linux, arch=Arch.x86_64), 113 Platform(os=OS.osx, arch=None), 114 Platform(os=OS.osx, arch=Arch.x86_64), 115 Platform(os=OS.osx, arch=Arch.aarch64), 116 ] 117 shuffle(platforms) 118 platforms.sort() 119 want = [ 120 Platform(os=OS.linux, arch=None), 121 Platform(os=OS.linux, arch=Arch.x86_64), 122 Platform(os=OS.osx, arch=None), 123 Platform(os=OS.osx, arch=Arch.x86_64), 124 Platform(os=OS.osx, arch=Arch.aarch64), 125 ] 126 127 self.assertEqual(want, platforms) 128 129 def test_wheel_os_alias(self): 130 self.assertEqual("osx", str(OS.osx)) 131 self.assertEqual(str(OS.darwin), str(OS.osx)) 132 133 def test_wheel_arch_alias(self): 134 self.assertEqual("x86_64", str(Arch.x86_64)) 135 self.assertEqual(str(Arch.amd64), str(Arch.x86_64)) 136 137 def test_wheel_platform_alias(self): 138 give = Platform( 139 os=OS.darwin, 140 arch=Arch.amd64, 141 ) 142 alias = Platform( 143 os=OS.osx, 144 arch=Arch.x86_64, 145 ) 146 147 self.assertEqual("osx_x86_64", str(give)) 148 self.assertEqual(str(alias), str(give)) 149 150 151if __name__ == "__main__": 152 unittest.main() 153