make autotest_quickmerge remove stale test packages

This CL makes autotest_quickmerge search through its itemized changes
for changes to files within subdirectories of client/site_tests. From
the subdirectories of any new or modified files, it pulls the name of
the corresponding test package, and then deletes the premade pacakge
from /[sysroot]/usr/local/autotest/packages if that file exists.

BUG=chromium:233306
TEST=unit test for pacakge name parsing. Also, manual test as follows:
`build_packages`
edit client/site_tests/dummy_Pass/dummy_Pass.py to throw an error in
run_once (ie fail immediately on client side)
`autotest_quickmerge --board=[board]`
`run_remote_tests.sh --remote=[remote ip] --use_emerged
^client/site_tests/dummy_Pass/control$`
Test fails. Prior to this CL, test would still pass since the
pre-existing version of dummy_Pass.py was stored in bzipped package, and
was being used by autoserv instead of the version in the sysroot client/
tree.

Change-Id: Ib0e5a20dc9d3eba31e1160d75274b6bf1027601b
Reviewed-on: https://gerrit.chromium.org/gerrit/48550
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
Tested-by: Aviv Keshet <akeshet@chromium.org>
Commit-Queue: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/autotest_quickmerge_unittest.py b/scripts/autotest_quickmerge_unittest.py
index be0fd8b..21cdb7a 100755
--- a/scripts/autotest_quickmerge_unittest.py
+++ b/scripts/autotest_quickmerge_unittest.py
@@ -41,6 +41,23 @@
 # that would rarely or never be encountered in the wild, rsync quickmerge
 # will exclude all files which contain the substring " -> " in their name.
 
+RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE = \
+""">f..t...... client/ardvark.py
+.d..t...... client/site_tests/
+>f+++++++++ client/site_tests/nothing.py
+.d..t...... client/site_tests/factory_Leds/
+>f+++++++++ client/site_tests/factory_Leds/factory_Leds2.py
+>f..tpog... client/site_tests/login_UserPolicyKeys/control
+>f..tpog... client/site_tests/login_UserPolicyKeys/login_UserPolicyKeys.py
+>f..t...... client/site_tests/platform_Cryptohome/platform_Cryptohome.py
+>f..tpog... server/site_tests/security_DbusFuzzServer/control
+>f..t.og... utils/coverage_suite.py
+.d..t...... client/site_tests/power_Thermal/
+cd+++++++++ client/site_tests/power_Thermal/a/
+cd+++++++++ client/site_tests/power_Thermal/a/b/
+cd+++++++++ client/site_tests/power_Thermal/a/b/c/
+>f+++++++++ client/site_tests/power_Thermal/a/b/c/d.py"""
+
 RSYNC_TEST_DESTINATION_PATH = '/foo/bar/'
 
 TEST_PACKAGE_CP = 'a_cute/little_puppy'
@@ -80,6 +97,20 @@
     self.assertEqual(expected_dir, set(report.new_directories))
 
 
+class PackageNameParsingTest(unittest.TestCase):
+
+  def testGetStalePackageNames(self):
+    autotest_sysroot = '/an/arbitrary/path/'
+    change_report = autotest_quickmerge.ItemizeChangesFromRsyncOutput(
+        RSYNC_TEST_OUTPUT_FOR_PACKAGE_UPDATE, autotest_sysroot)
+    package_matches = autotest_quickmerge.GetStalePackageNames(
+        change_report.modified_files + change_report.new_files,
+        autotest_sysroot)
+    expected_set = set(['factory_Leds', 'login_UserPolicyKeys',
+                        'platform_Cryptohome', 'power_Thermal'])
+    self.assertEqual(set(package_matches), expected_set)
+
+
 class RsyncCommandTest(cros_build_lib_unittest.RunCommandTestCase):
 
   def testRsyncQuickmergeCommand(self):