#!/usr/bin/python

import sys
import os
import catbox

bad_path = "catboxtest.deleteme"

def test():
    try:
        file(bad_path, "w").write("hello world\n")
    except IOError, e:
        if e.errno != 13:
            raise

def restart():
    os.execve(sys.argv[0], [sys.argv[0], "second"], os.environ)

if len(sys.argv) == 1:
    if os.path.exists(bad_path):
        os.unlink(bad_path)
    
    canonical = os.path.realpath(os.getcwd() + "/" + bad_path)
    
    ret = catbox.run(test)
    assert(ret.code == 0)
    assert(ret.violations == [("open", bad_path, canonical)])
    
    ret = catbox.run(restart)
    assert(ret.code == 0)
    assert(ret.violations == [("open", bad_path, canonical)])
    
    assert(not os.path.exists(bad_path))
else:
    test()
