# Path: prolog/builtin/sourcehelper.py
# def get_source(filename):
#     try:
#         assert isinstance(filename, str)
#         fd, actual_filename = get_filehandle(filename, True)
#     except OSError:
#         throw_existence_error("source_sink", Callable.build(filename))
#         assert 0, "unreachable" # make the flow space happy
#     try:
#         content = []
#         while 1:
#             s = os.read(fd, 4096)
#             if not s:
#                 break
#             content.append(s)
#         file_content = "".join(content)
#     finally:
#         os.close(fd)
#     return file_content, actual_filename
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# Path: prolog/interpreter/test/tool.py
# def prolog_raises(exc, query, e=None):
#     prolog_catch = "catch(((%s), fail), error(%s), true)." % (query, exc)
#     return assert_true(prolog_catch, e)
# 
# def create_file(name, content):
#     with open(name, "w") as f:
#         f.write(content)
# 
# def delete_file(name):
#     os.unlink(name)
# 
# Path: prolog/interpreter/error.py
# class CatchableError(TermedError): pass


import py
import os
from prolog.builtin.sourcehelper import get_source
from prolog.interpreter.test.tool import collect_all, assert_false, assert_true
from prolog.interpreter.test.tool import prolog_raises, create_file, delete_file
from prolog.interpreter.error import CatchableError

def test_get_source():
    content = "some important content"
    name = "__testfile__"
    try:
        create_file(name, content)
        source, file_name = get_source(name)
    finally:
        delete_file(name)
    assert source == content
    assert file_name == os.path.abspath(name)

def test_source_does_not_exist():
    py.test.raises(CatchableError, "get_source('this_file_does_not_exist')")

def test_file_ending():
    content = "some content"
    filename = "__testfile__.pl"
    searchname = filename[:len(filename) - 3]
    try:

# Path: prolog/builtin/sourcehelper.py
# def get_source(filename):
#     try:
#         assert isinstance(filename, str)
#         fd, actual_filename = get_filehandle(filename, True)
#     except OSError:
#         throw_existence_error("source_sink", Callable.build(filename))
#         assert 0, "unreachable" # make the flow space happy
#     try:
#         content = []
#         while 1:
#             s = os.read(fd, 4096)
#             if not s:
#                 break
#             content.append(s)
#         file_content = "".join(content)
#     finally:
#         os.close(fd)
#     return file_content, actual_filename
# 
# Path: prolog/interpreter/test/tool.py
# def collect_all(engine, s):
#     terms, vars = engine.parse(s)
#     term, = terms
#     collector = CollectAllContinuation(engine.modulewrapper.user_module, vars)
#     py.test.raises(UnificationFailed, engine.run_query, term,
#             engine.modulewrapper.current_module, collector)
#     return collector.heaps
# 
# def assert_false(query, e=None):
#     if e is None:
#         e = Engine()
#     term = e.parse(query)[0][0]
#     py.test.raises(UnificationFailed, e.run_query_in_current, term)
# 
# def assert_true(query, e=None):
#     if e is None:
#         e = Engine()
#     terms, vars = e.parse(query)
#     term, = terms
#     e.run_query_in_current(term)
#     return dict([(name, var.dereference(None))
#                      for name, var in vars.iteritems()])
# 
# Path: prolog/interpreter/test/tool.py
# def prolog_raises(exc, query, e=None):
#     prolog_catch = "catch(((%s), fail), error(%s), true)." % (query, exc)
#     return assert_true(prolog_catch, e)
# 
# def create_file(name, content):
#     with open(name, "w") as f:
#         f.write(content)
# 
# def delete_file(name):
#     os.unlink(name)
# 
# Path: prolog/interpreter/error.py
# class CatchableError(TermedError): pass


# Path: prolog/interpreter/test/test_sourcehelper.py
import py
import os
from prolog.builtin.sourcehelper import get_source
from prolog.interpreter.test.tool import collect_all, assert_false, assert_true
from prolog.interpreter.test.tool import prolog_raises, create_file, delete_file
from prolog.interpreter.error import CatchableError


def test_get_source():
    content = "some important content"
    name = "__testfile__"
    try:
        create_file(name, content)
        source, file_name = get_source(name)
    finally:
        delete_file(name)
    assert source == content
    assert file_name == os.path.abspath(name)

def test_source_does_not_exist():
    py.test.raises(CatchableError, "get_source('this_file_does_not_exist')")

def test_file_ending():
    content = "some content"
    filename = "__testfile__.pl"
    searchname = filename[:len(filename) - 3]
    try:

        create_file(filename, content)