test_startfile.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Ridiculously simple test of the os.startfile function for Windows.
  2. #
  3. # empty.vbs is an empty file (except for a comment), which does
  4. # nothing when run with cscript or wscript.
  5. #
  6. # A possible improvement would be to have empty.vbs do something that
  7. # we can detect here, to make sure that not only the os.startfile()
  8. # call succeeded, but also the script actually has run.
  9. import unittest
  10. from test import support
  11. from test.support import os_helper
  12. import os
  13. import platform
  14. import sys
  15. from os import path
  16. startfile = support.get_attribute(os, 'startfile')
  17. @unittest.skipIf(platform.win32_is_iot(), "starting files is not supported on Windows IoT Core or nanoserver")
  18. class TestCase(unittest.TestCase):
  19. def test_nonexisting(self):
  20. self.assertRaises(OSError, startfile, "nonexisting.vbs")
  21. def test_empty(self):
  22. # We need to make sure the child process starts in a directory
  23. # we're not about to delete. If we're running under -j, that
  24. # means the test harness provided directory isn't a safe option.
  25. # See http://bugs.python.org/issue15526 for more details
  26. with os_helper.change_cwd(path.dirname(sys.executable)):
  27. empty = path.join(path.dirname(__file__), "empty.vbs")
  28. startfile(empty)
  29. startfile(empty, "open")
  30. startfile(empty, cwd=path.dirname(sys.executable))
  31. def test_python(self):
  32. # Passing "-V" ensures that it closes quickly, though still not
  33. # quickly enough that we can run in the test directory
  34. cwd, name = path.split(sys.executable)
  35. startfile(name, arguments="-V", cwd=cwd)
  36. startfile(name, arguments="-V", cwd=cwd, show_cmd=0)
  37. if __name__ == "__main__":
  38. unittest.main()