<commit_before><commit_msg>test: add test_run<commit_after>"""
This Test will run through benchbuild's execution pipeline.
"""
import os
import unittest
from contextlib import contextmanager
from benchbuild.utils import cmd


def shadow_commands(command):
    def shadow_command_fun(func):
        def shadow_command_wrapped_fun(self, *args, **kwargs):
            cmd.__override_all__ = command
            res = func(self, *args, **kwargs)
            cmd.__override_all__ = None
            return res
        return shadow_command_wrapped_fun
    return shadow_command_fun


class TestShadow(unittest.TestCase):
    def test_shadow(self):
        inside = None
        true = cmd.true
        mkdir = cmd.mkdir
        with shadow_commands("true"):
            inside = cmd.mkdir
        outside = cmd.mkdir
        self.assertEqual(inside.formulate(), true.formulate(),
                         msg="true (before) is not the same as true (inside)")
        self.assertNotEqual(mkdir.formulate(), inside.formulate(),
                            msg="mkdir (before) is not the same as mkdir (inside)")
        self.assertNotEqual(inside.formulate(), outside.formulate(),
                            msg="true (before) is not the same as true (after)")
        self.assertEqual(mkdir.formulate(), outside.formulate(),
                         msg="mkdir (before) is not the same as mkdir (after)")


class TestRun(unittest.TestCase):
    @shadow_commands("true")
    def test_run(self):
        from benchbuild import experiment
        from benchbuild.utils.actions import Experiment

        class MockExp(experiment.Experiment):
            NAME = "mock-exp"

            def actions_for_project(self, project):
                from benchbuild.utils.actions import (
                    Prepare, Download, Configure, Build, Run, Clean)
                inside = None
                actns = []
                project.builddir = "/tmp/throwaway"
                actns = [Prepare(project),
                         Download(project),
                         Configure(project),
                         Build(project),
                         Run(project),
                         Clean(project)]
                return actns

        exp = MockExp(group="polybench")
        eactn = Experiment(exp, exp.actions())
        old_exists = os.path.exists
        os.path.exists = lambda p: True
        print(eactn)
        eactn()
        os.path.exists = old_exists


if __name__ == "__main__":
    from benchbuild.utils import log
    log.configure()
    TestRun().test_run()

{'fraction_non_alphanumeric': 0.09337466098411469, 'fraction_numerical': 0.0, 'mean_word_length': 1.9407744874715263, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6246574', 'n_tokens_mistral': 687, 'n_tokens_neox': 651, 'n_words': 172}