def test_fail_open_nonexistant_dir(): """ Test that program refuses to write to non-existant directory. """ try: open_filepath('ab/cd/ef/gh/ij/kl') assert False, "should have raised assertion" except AssertionError as err: assert err.args[0].startswith('directory'), err.args[0] assert err.args[0].endswith('does not exist.'), err.args[0]
def test_fail_cannot_write_to_folder(): """ Test that program refuses to write to directory without write access. """ try: open_filepath('/test.write') assert False, "should have raised assertion" except AssertionError as err: assert err.args[0].startswith('directory'), err.args[0] assert 'is not writable' in err.args[0], err.args[0] assert err.args[0].endswith('does not exist.'), err.args[0]
def test_fail_cannot_write_to_special_dev(): """ Test that program refuses to write to special device. """ # not necessarily something to prevent from a power user ... but # something to prevent from automated programs: such as # following a maliciously plotted symlink. try: open_filepath('/dev/stdout') assert False, "should have raised assertion" except AssertionError as err: assert err.args[0].startswith('/dev/stdout'), err.args[0] assert err.args[0].endswith('is not a file.'), err.args[0]
def test_fail_cannot_write_to_file(): """ Test that program refuses to write to file without write access. """ import os assert not os.path.exists('/tmp/test-minus-r') fp = open('/tmp/test-minus-r', 'w') fp.write('') fp.close() os.chmod('/tmp/test-minus-r', 0) assert os.path.isfile('/tmp/test-minus-r') try: open_filepath('/tmp/test-minus-r') assert False, "should have raised assertion" except AssertionError as err: assert err.args[0].startswith('no write access to'), err.args[0] assert err.args[0].endswith('/tmp/test-minus-r.'), err.args[0] os.unlink('/tmp/test-minus-r')