Best Python code snippet using autotest_python
test_package_tracker.py
Source:test_package_tracker.py  
...24		p.add_pkg(x1)25		self.assertTrue(x1 in p)26		self.assertTrue(p.contains(x1, installed=True))27		self.assertTrue(p.contains(x1, installed=False))28		p.remove_pkg(x1)29		self.assertTrue(x1 not in p)30		p.add_pkg(x1)31		self.assertTrue(x1 in p)32		p.add_pkg(x1)33		self.assertTrue(x1 in p)34		self.assertRaises(KeyError, p.remove_pkg, x2)35		p.add_pkg(x2)36		self.assertTrue(x2 in p)37		p.remove_pkg(x2)38		self.assertTrue(x2 not in p)39		p.discard_pkg(x2)40		self.assertTrue(x2 not in p)41		p.add_pkg(x2)42		self.assertTrue(x2 in p)43		all_pkgs = list(p.all_pkgs("/"))44		self.assertEqual(len(all_pkgs), 2)45		self.assertTrue(all_pkgs[0] is x1 and all_pkgs[1] is x2)46		self.assertEqual(len(list(p.all_pkgs("/"))), 2)47		self.assertEqual(len(list(p.all_pkgs("/xxx"))), 0)48	def test_match(self):49		p = PackageTracker()50		x1 = self.make_pkg("/", "=dev-libs/X-1:0")51		x2 = self.make_pkg("/", "=dev-libs/X-2:0")52		x3 = self.make_pkg("/", "=dev-libs/X-3:1")53		p.add_pkg(x2)54		p.add_pkg(x1)55		matches = list(p.match("/", Atom("=dev-libs/X-1")))56		self.assertTrue(x1 in matches)57		self.assertEqual(len(matches), 1)58		matches = list(p.match("/", Atom("dev-libs/X")))59		self.assertTrue(x1 is matches[0] and x2 is matches[1])60		self.assertEqual(len(matches), 2)61		matches = list(p.match("/xxx", Atom("dev-libs/X")))62		self.assertEqual(len(matches), 0)63		matches = list(p.match("/", Atom("dev-libs/Y")))64		self.assertEqual(len(matches), 0)65		p.add_pkg(x3)66		matches = list(p.match("/", Atom("dev-libs/X")))67		self.assertTrue(x1 is matches[0] and x2 is matches[1] and x3 is matches[2])68		self.assertEqual(len(matches), 3)69		p.remove_pkg(x3)70		matches = list(p.match("/", Atom("dev-libs/X")))71		self.assertTrue(x1 is matches[0] and x2 is matches[1])72		self.assertEqual(len(matches), 2)73	def test_dbapi_interface(self):74		p = PackageTracker()75		dbapi = PackageTrackerDbapiWrapper("/", p)76		installed = self.make_pkg("/", "=dev-libs/X-0:0")77		x1 = self.make_pkg("/", "=dev-libs/X-1:0")78		x2 = self.make_pkg("/", "=dev-libs/X-2:0")79		x3 = self.make_pkg("/", "=dev-libs/X-3:0")80		x4 = self.make_pkg("/", "=dev-libs/X-4:6")81		x5 = self.make_pkg("/xxx", "=dev-libs/X-5:6")82		def check_dbapi(pkgs):83			all_pkgs = set(dbapi)84			self.assertEqual(len(all_pkgs), len(pkgs))85			x_atom = "dev-libs/X"86			y_atom = "dev-libs/Y"87			matches = dbapi.cp_list(x_atom)88			for pkg in pkgs:89				if pkg.root == "/" and pkg.cp == x_atom:90					self.assertTrue(pkg in matches)91			self.assertTrue(not dbapi.cp_list(y_atom))92			matches = dbapi.match(Atom(x_atom))93			for pkg in pkgs:94				if pkg.root == "/" and pkg.cp == x_atom:95					self.assertTrue(pkg in matches)96			self.assertTrue(not dbapi.match(Atom(y_atom)))97		check_dbapi([])98		p.add_installed_pkg(installed)99		check_dbapi([installed])100		p.add_pkg(x1)101		check_dbapi([x1])102		p.remove_pkg(x1)103		check_dbapi([installed])104		dbapi.cpv_inject(x1)105		check_dbapi([x1])106		dbapi.cpv_inject(x2)107		check_dbapi([x1, x2])108		p.remove_pkg(x1)109		check_dbapi([x2])110		p.add_pkg(x5)111		check_dbapi([x2])112	def test_installed(self):113		p = PackageTracker()114		x1 = self.make_pkg("/", "=dev-libs/X-1:0")115		x1b = self.make_pkg("/", "=dev-libs/X-1.1:0")116		x2 = self.make_pkg("/", "=dev-libs/X-2:0")117		x3 = self.make_pkg("/", "=dev-libs/X-3:1")118		def check_installed(x, should_contain, num_pkgs):119			self.assertEqual(x in p, should_contain)120			self.assertEqual(p.contains(x), should_contain)121			self.assertEqual(p.contains(x1, installed=True), should_contain)122			self.assertEqual(p.contains(x1, installed=False), False)123			self.assertEqual(len(list(p.all_pkgs("/"))), num_pkgs)124		def check_matches(atom, expected):125			matches = list(p.match("/", Atom(atom)))126			self.assertEqual(len(matches), len(expected))127			for x, y in zip(matches, expected):128				self.assertTrue(x is y)129		p.add_installed_pkg(x1)130		check_installed(x1, True, 1)131		check_matches("dev-libs/X", [x1])132		p.add_installed_pkg(x1)133		check_installed(x1, True, 1)134		check_matches("dev-libs/X", [x1])135		p.add_pkg(x2)136		check_installed(x1, False, 1)137		check_matches("dev-libs/X", [x2])138		p.add_installed_pkg(x1)139		check_installed(x1, False, 1)140		check_matches("dev-libs/X", [x2])141		p.add_installed_pkg(x1b)142		check_installed(x1, False, 1)143		check_installed(x1b, False, 1)144		check_matches("dev-libs/X", [x2])145		p.remove_pkg(x2)146		check_installed(x1, True, 2)147		check_installed(x1b, True, 2)148		check_matches("dev-libs/X", [x1, x1b])149	def test_conflicts(self):150		p = PackageTracker()151		installed1 = self.make_pkg("/", "=dev-libs/X-0:0")152		installed2 = self.make_pkg("/", "=dev-libs/X-0.1:0")153		x1 = self.make_pkg("/", "=dev-libs/X-1:0")154		x2 = self.make_pkg("/", "=dev-libs/X-2:0")155		x3 = self.make_pkg("/", "=dev-libs/X-3:0")156		x4 = self.make_pkg("/", "=dev-libs/X-4:4")157		x4b = self.make_pkg("/", "=dev-libs/X-4:4b::x-repo")158		def check_conflicts(expected, slot_conflicts_only=False):159			if slot_conflicts_only:160				conflicts = list(p.slot_conflicts())161			else:162				conflicts = list(p.conflicts())163			self.assertEqual(len(conflicts), len(expected))164			for got, exp in zip(conflicts, expected):165				self.assertEqual(got.description, exp.description)166				self.assertEqual(got.root, exp.root)167				self.assertEqual(len(got.pkgs), len(exp.pkgs))168				self.assertEqual(len(got), len(exp.pkgs))169				for x, y in zip(got.pkgs, exp.pkgs):170					self.assertTrue(x is y)171				for x, y in zip(got, exp.pkgs):172					self.assertTrue(x is y)173				for x in exp.pkgs:174					self.assertTrue(x in got)175		check_conflicts([])176		check_conflicts([])177		p.add_installed_pkg(installed1)178		p.add_installed_pkg(installed2)179		check_conflicts([])180		p.add_pkg(x1)181		check_conflicts([])182		p.add_pkg(x2)183		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x2])])184		p.add_pkg(x3)185		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x2, x3])])186		p.remove_pkg(x3)187		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x2])])188		p.remove_pkg(x2)189		check_conflicts([])190		p.add_pkg(x3)191		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x3])])192		p.add_pkg(x2)193		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x3, x2])])194		p.add_pkg(x4)195		check_conflicts([self.make_conflict("slot conflict", "/", [x1, x3, x2])])196		p.add_pkg(x4b)197		check_conflicts(198			[199			self.make_conflict("slot conflict", "/", [x1, x3, x2]),200			self.make_conflict("cpv conflict", "/", [x4, x4b]),201			]202			)...main.py
Source:main.py  
...45            copy2("/" + file, join(self.root, file))46    def copy_folder(self, source, target):47        if exists("/" + source):48            copy_tree("/" + source, join(self.root, target), preserve_symlinks=1)49    def remove_pkg(self, pkg, path):50        if exists(join(self.root, path)):51            target_env_call(['pacman', '-R', '--noconfirm', pkg])52    def umount(self, mp):53        subprocess.call(["umount", "-l", join(self.root, mp)])54    def mount(self, mp):55        subprocess.call(["mount", "-B", "/" + mp, join(self.root, mp)])56    def rmdir(self, dir):57        subprocess.call(["rm", "-Rf", join(self.root, dir)])58    def mkdir(self, dir):59        subprocess.call(["mkdir", "-p", join(self.root, dir)])60    def run(self):61        self.init_keyring()62        self.populate_keyring()63        # Generate mirror list64        if exists(join(self.root, "usr/bin/pacman-mirrors")):65            if libcalamares.globalstorage.value("hasInternet"):66                target_env_call(["pacman-mirrors", "-f3"])67        else:68            self.copy_file('etc/pacman.d/mirrorlist')69        # Initialize package manager databases70        if libcalamares.globalstorage.value("hasInternet"):71            target_env_call(["pacman", "-Syy"])72        # Remove unneeded ucode73        cpu_ucode = subprocess.getoutput("hwinfo --cpu | grep Vendor: -m1 | cut -d\'\"\' -f2")74        if cpu_ucode == "AuthenticAMD":75            self.remove_pkg("intel-ucode", "boot/intel-ucode.img")76        elif cpu_ucode == "GenuineIntel":77            self.remove_pkg("amd-ucode", "boot/amd-ucode.img")78        # Remove calamares79        self.remove_pkg("calamares", "usr/bin/calamares")80        self.remove_pkg("calamares-git", "usr/bin/calamares")81        # Copy skel to root82        self.copy_folder('etc/skel', 'root')83        # Workaround for pacman-key bug84        # FS#45351 https://bugs.archlinux.org/task/4535185        # We have to kill gpg-agent because if it stays86        # around we can't reliably unmount87        # the target partition.88        self.terminate('gpg-agent')89        # Update grub.cfg90        if exists(join(self.root, "usr/bin/update-grub")):91            target_env_call(["update-grub"])92        # Enable 'menu_auto_hide' when supported in grubenv93        if exists(join(self.root, "usr/bin/grub-set-bootflag")):94            target_env_call(["grub-editenv", "-", "set", "menu_auto_hide=1", "boot_success=1"])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
