Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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() |
Ezio Melotti | 3f5db39 | 2013-01-27 06:20:14 +0200 | [diff] [blame] | 8 | # call succeeded, but also the script actually has run. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9 | |
| 10 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
Hai Shi | 598a951 | 2020-08-07 23:18:38 +0800 | [diff] [blame] | 12 | from test.support import os_helper |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 13 | import os |
Paul Monson | 62dfd7d | 2019-04-25 11:36:45 -0700 | [diff] [blame] | 14 | import platform |
Antoine Pitrou | 8a53dbe | 2012-09-16 00:12:50 +0200 | [diff] [blame] | 15 | import sys |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 16 | from os import path |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 17 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 18 | startfile = support.get_attribute(os, 'startfile') |
| 19 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 20 | |
Steve Dower | 019e9e8 | 2021-04-23 18:03:17 +0100 | [diff] [blame] | 21 | @unittest.skipIf(platform.win32_is_iot(), "starting files is not supported on Windows IoT Core or nanoserver") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 22 | class TestCase(unittest.TestCase): |
| 23 | def test_nonexisting(self): |
| 24 | self.assertRaises(OSError, startfile, "nonexisting.vbs") |
| 25 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 26 | def test_empty(self): |
Nick Coghlan | c06c0ae | 2013-07-29 17:51:16 +1000 | [diff] [blame] | 27 | # We need to make sure the child process starts in a directory |
| 28 | # we're not about to delete. If we're running under -j, that |
| 29 | # means the test harness provided directory isn't a safe option. |
| 30 | # See http://bugs.python.org/issue15526 for more details |
Hai Shi | 598a951 | 2020-08-07 23:18:38 +0800 | [diff] [blame] | 31 | with os_helper.change_cwd(path.dirname(sys.executable)): |
Nick Coghlan | c06c0ae | 2013-07-29 17:51:16 +1000 | [diff] [blame] | 32 | empty = path.join(path.dirname(__file__), "empty.vbs") |
Antoine Pitrou | 8a53dbe | 2012-09-16 00:12:50 +0200 | [diff] [blame] | 33 | startfile(empty) |
Nick Coghlan | c06c0ae | 2013-07-29 17:51:16 +1000 | [diff] [blame] | 34 | startfile(empty, "open") |
Steve Dower | 019e9e8 | 2021-04-23 18:03:17 +0100 | [diff] [blame] | 35 | startfile(empty, cwd=path.dirname(sys.executable)) |
| 36 | |
| 37 | def test_python(self): |
| 38 | # Passing "-V" ensures that it closes quickly, though still not |
| 39 | # quickly enough that we can run in the test directory |
| 40 | cwd, name = path.split(sys.executable) |
| 41 | startfile(name, arguments="-V", cwd=cwd) |
| 42 | startfile(name, arguments="-V", cwd=cwd, show_cmd=0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 44 | if __name__ == "__main__": |
| 45 | unittest.main() |