def test_integration():
    """integration test"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    temp_path = os.path.join(tmpdir, "sample-text.odt")
    log = StringIO()

    worker = UnoConvWorker(in_path, err_path)
    log_all(worker, log)  # we log so we now what is done by our thread

    with controlled_worker(worker):
        # prepare a task
        shutil.copyfile(sample_path, temp_path)
        group_permissions(temp_path)
        task_path = os.path.join(tmpdir, "t1")
        with open(task_path, "w") as f:
            f.write("[unoconv]\n")
            f.write("src=%s\n" % temp_path)
            f.write("next=%s\n" % done_path)
        # push it
        os.rename(task_path, os.path.join(in_path, "t1"))
        # wait for completion
        while not "exit method run" in log.getvalue():
            time.sleep(0.1)
        assert "t1" in os.listdir(done_path)
        pdf_path = os.path.join(tmpdir, "sample-text.pdf")
        assert os.path.exists(pdf_path)
def test_run_simple():
    """test the run method isolated"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = SimpleWorker(in_path, err_path)
    # push a task
    task_path = os.path.join(in_path, 't1')
    with open(task_path, 'w') as f:
        f.write("""[simple]
a=1
b=some text
next=%s""" % done_path)
    worker.run(task_path)
    assert output.getvalue() == "runing with a = '1' and b = 'some text'"
    assert 't1' in os.listdir(done_path)
def test_run_raiser():
    """test the run method isolated with a raise in run_action"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = RaiserWorker(in_path, err_path)
    # push a task
    task_path = os.path.join(in_path, 't1')
    with open(task_path, 'w') as f:
        f.write("""[raiser]
next=%s""" % done_path)
    worker.run(task_path)
    assert 't1' in os.listdir(err_path)
    with open(os.path.join(err_path, 't1')) as t:
        content = t.read()
    assert "Error in raiser at" in content
    assert "Exception: I'm mean" in content
def test_main_loop():
    """test main worker loop, that is in start"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = ControlledWorker(in_path, err_path)

    log_all(worker, log)

    with controlled_worker(worker):
        # now we just wake it
        with worker.task_queue_guard:
            worker.task_queue.add('t1')
            worker.task_queue_signal.notify()
        while not "exit method run" in log.getvalue():
            time.sleep(0.2)
        assert output.getvalue() == "run t1"
def test_run_no_section():
    """test the run method isolated with a task without section"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = SimpleWorker(in_path, err_path)
    # push a task
    task_path = os.path.join(in_path, 't1')
    with open(task_path, 'w') as f:
        f.write("""[NOT FOR YOU DEAR]
next=%s""" % done_path)
    worker.run(task_path)
    assert 't1' in os.listdir(err_path)
    with open(os.path.join(err_path, 't1')) as t:
        content = t.read()
    assert "Error in simple at" in content
    assert "No section for me : simple" in content
def test_incoming_file():
    """integration test"""
    in_path, err_path, done_path = make_dirs(tmpdir)

    worker = SimpleWorker(in_path, err_path)
    log_all(worker, log)  # we log so we now what is done by our thread

    with controlled_worker(worker):
        # prepare a task
        task_path = os.path.join(tmpdir, 't1')
        with open(task_path, 'w') as f:
            f.write("""[simple]
a=1
b=some text
next=%s""" % done_path)
        # push it
        os.rename(task_path, os.path.join(in_path, 't1'))
        # wait for completion
        while not "exit method run" in log.getvalue():
            time.sleep(0.1)
        assert 't1' in os.listdir(done_path)
        assert output.getvalue() == "runing with a = '1' and b = 'some text'"
def test_observer():
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = NoRunWorker(in_path, err_path)
    log_all(worker, log)
    try:
        worker._launch_observer()
        assert worker.notifier is not None

        # add some file
        task_path = os.path.join(tmpdir, 't1')
        with open(task_path, 'w') as f:
            f.write(".")
            # take queue lock (to control the execution of notifier)
            with worker.task_queue_guard:
                # push task
                new_path = os.path.join(in_path, 't1')
                os.rename(task_path, new_path)
                # We nust be sure pyinotify have seen file before releasing
                # the lock
                worker.task_queue_signal.wait()  # notifier shall notify
                assert worker.task_queue == set([new_path])
    finally:
        worker.stop()
def test_init_with_files():
    """test worker when file are present in start directory"""
    in_path, err_path, done_path = make_dirs(tmpdir)
    worker = SimpleWorker(in_path, err_path)

    # push a task
    task_path = os.path.join(in_path, 't1')
    with open(task_path, 'w') as f:
        f.write("""[simple]
a=1
b=some text
next=%s""" % done_path)

    log_all(worker, log)

    worker._launch_observer = lambda: None

    # start
    with controlled_worker(worker):
        # shall execute task at startup
        while not "exit method run" in log.getvalue():
            time.sleep(0.1)
        assert output.getvalue() == "runing with a = '1' and b = 'some text'"