bpo-15526: test_startfile changes the cwd (#1537)

Try to fix test_startfile's inability to clean up after itself in
time. Patch by Jeremy Kloth.

Fix the following support.rmtree() error while trying to remove the
temporary working directory used by Python tests:

WindowsError: [Error 32] The process cannot access the file because
it is being used by another process: ...

Original commit: 8a53dbeb7a9d0ae3e446f36e56cf9acfabe443c1
diff --git a/Lib/test/test_startfile.py b/Lib/test/test_startfile.py
index 3c22f3f..61212ad 100644
--- a/Lib/test/test_startfile.py
+++ b/Lib/test/test_startfile.py
@@ -10,8 +10,8 @@
 import unittest
 from test import test_support
 import os
+import sys
 from os import path
-from time import sleep
 
 startfile = test_support.get_attribute(os, 'startfile')
 
@@ -23,20 +23,23 @@
     def test_nonexisting_u(self):
         self.assertRaises(OSError, startfile, u"nonexisting.vbs")
 
+    def check_empty(self, empty):
+        # We need to make sure the child process starts in a directory
+        # we're not about to delete. If we're running under -j, that
+        # means the test harness provided directory isn't a safe option.
+        # See http://bugs.python.org/issue15526 for more details
+        with test_support.change_cwd(path.dirname(sys.executable)):
+            startfile(empty)
+            startfile(empty, "open")
+
     def test_empty(self):
         empty = path.join(path.dirname(__file__), "empty.vbs")
-        startfile(empty)
-        startfile(empty, "open")
-        # Give the child process some time to exit before we finish.
-        # Otherwise the cleanup code will not be able to delete the cwd,
-        # because it is still in use.
-        sleep(0.1)
+        self.check_empty(empty)
 
-    def test_empty_u(self):
+    def test_empty_unicode(self):
         empty = path.join(path.dirname(__file__), "empty.vbs")
-        startfile(unicode(empty, "mbcs"))
-        startfile(unicode(empty, "mbcs"), "open")
-        sleep(0.1)
+        empty = unicode(empty, "mbcs")
+        self.check_empty(empty)
 
 def test_main():
     test_support.run_unittest(TestCase)