import os
import multiprocessing
from multiprocessing import Pool
import pp
from pp.build import run_python


def test_samples():
    """ Test that the whole tutorial can be run without errors """
    all_files = [
        os.path.join(dp, f)
        for dp, dn, filenames in os.walk(pp.CONFIG["samples_path"])
        for f in filenames
        if f.endswith(".py")
    ]
    with Pool(processes=multiprocessing.cpu_count()) as pool:
        for filename, rc in pool.imap_unordered(run_python, all_files):
            if rc != 0:
                raise ValueError(f"error in {filename}")
            else:
                print("Finished {} {}".format(filename, rc))


if __name__ == "__main__":
    test_samples()
