Beispiel #1
0
    def test_run(self):
        with self.subTest(order='invalid-order'):
            with self.assertRaises(ValueError):
                project = self.mock_project()
                self.project.run(order='invalid-order')

        def sort_key(op):
            return op.name, op.job.get_id()

        for order in (None, 'none', 'cyclic', 'by-job', 'random', sort_key):
            for job in self.project.find_jobs():  # clear
                job.remove()
            with self.subTest(order=order):
                project = self.mock_project()
                output = StringIO()
                with add_cwd_to_environment_pythonpath():
                    with switch_to_directory(project.root_directory()):
                        with redirect_stderr(output):
                            project.run(order=order)
                output.seek(0)
                output.read()
                even_jobs = [job for job in project if job.sp.b % 2 == 0]
                for job in project:
                    if job in even_jobs:
                        self.assertTrue(job.isfile('world.txt'))
                    else:
                        self.assertFalse(job.isfile('world.txt'))
Beispiel #2
0
    def call_subcmd(self, subcmd):
        # Determine path to project module and construct command.
        fn_script = inspect.getsourcefile(type(self.project))
        _cmd = 'python {} {}'.format(fn_script, subcmd)

        try:
            with add_path_to_environment_pythonpath(os.path.abspath(self.cwd)):
                with switch_to_directory(self.project.root_directory()):
                    return subprocess.check_output(_cmd.split(), stderr=subprocess.DEVNULL)
        except subprocess.CalledProcessError as error:
            print(error, file=sys.stderr)
            print(error.output, file=sys.stderr)
            raise
Beispiel #3
0
 def test_run_parallel(self):
     project = self.mock_project()
     output = StringIO()
     with add_cwd_to_environment_pythonpath():
         with switch_to_directory(project.root_directory()):
             with redirect_stderr(output):
                 project.run(np=2)
     output.seek(0)
     output.read()
     even_jobs = [job for job in project if job.sp.b % 2 == 0]
     for job in project:
         if job in even_jobs:
             self.assertTrue(job.isfile('world.txt'))
         else:
             self.assertFalse(job.isfile('world.txt'))
Beispiel #4
0
    def test_with_job_decorator(self):
        class A(FlowProject):
            pass

        @A.operation
        @with_job
        def test_context(job):
            self.assertEqual(os.getcwd(), job.ws)

        project = self.mock_project()
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with redirect_stderr(StringIO()):
                    A().run()
                self.assertTrue(os.getcwd(), starting_dir)
Beispiel #5
0
    def test_run_fork(self):
        project = self.mock_project()
        output = StringIO()
        for job in project:
            job.doc.fork = True
            break

        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                with redirect_stderr(output):
                    project.run()

        for job in project:
            if job.doc.get('fork'):
                self.assertNotEqual(os.getpid(), job.doc.test)
            else:
                self.assertEqual(os.getpid(), job.doc.test)
Beispiel #6
0
    def test_with_job_error_handling(self):
        class A(FlowProject):
            pass

        @A.operation
        @with_job
        def test_context(job):
            raise Exception

        project = self.mock_project()
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with self.assertRaises(Exception):
                    with redirect_stderr(StringIO()):
                        A().run()
                self.assertEqual(os.getcwd(), starting_dir)
Beispiel #7
0
    def test_cmd_with_job_error_handling(self):
        class A(FlowProject):
            pass

        @A.operation
        @with_job
        @cmd
        def test_context(job):
            return "exit 1"

        project = self.mock_project()
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with redirect_stderr():
                    A().run()
                self.assertEqual(os.getcwd(), starting_dir)
Beispiel #8
0
 def test_run_with_operation_selection(self):
     project = self.mock_project()
     even_jobs = [job for job in project if job.sp.b % 2 == 0]
     with add_cwd_to_environment_pythonpath():
         with switch_to_directory(project.root_directory()):
             with pytest.raises(ValueError):
                 # The names argument must be a sequence of strings, not a string.
                 project.run(names='op1')
             project.run(names=['non-existent-op'])
             assert not any(job.isfile('world.txt') for job in even_jobs)
             assert not any(job.doc.get('test') for job in project)
             project.run(names=['op1', 'non-existent-op'])
             assert all(job.isfile('world.txt') for job in even_jobs)
             assert not any(job.doc.get('test') for job in project)
             project.run(names=['op[^3]', 'non-existent-op'])
             assert all(job.isfile('world.txt') for job in even_jobs)
             assert all(job.doc.get('test') for job in project)
             assert all('dynamic' not in job.doc for job in project)
Beispiel #9
0
 def test_run_with_selection(self):
     project = self.mock_project()
     output = StringIO()
     with add_cwd_to_environment_pythonpath():
         with switch_to_directory(project.root_directory()):
             with redirect_stderr(output):
                 if StrictVersion(signac.__version__) < StrictVersion('0.9.4'):
                     project.run(list(project.find_jobs(dict(a=0))))
                 else:
                     project.run(project.find_jobs(dict(a=0)))
     output.seek(0)
     output.read()
     even_jobs = [job for job in project if job.sp.b % 2 == 0]
     for job in project:
         if job in even_jobs and job.sp.a == 0:
             assert job.isfile('world.txt')
         else:
             assert not job.isfile('world.txt')
Beispiel #10
0
    def test_with_job_works_with_cmd(self):
        class A(FlowProject):
            pass

        @A.operation
        @with_job
        @cmd
        def test_context(job):
            return "echo 'hello' > world.txt"

        project = self.mock_project()
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with redirect_stderr(StringIO()):
                    A().run()
                self.assertEqual(os.getcwd(), starting_dir)
                for job in project:
                    self.assertTrue(os.path.isfile(job.fn("world.txt")))
Beispiel #11
0
    def test_cmd_with_job_error_handling(self):

        class A(FlowProject):
            pass

        @A.operation
        @with_job
        @cmd
        def test_context(job):
            return "exit 1"

        project = self.mock_project()
        with add_cwd_to_environment_pythonpath():
            with switch_to_directory(project.root_directory()):
                starting_dir = os.getcwd()
                with pytest.raises(subprocess.CalledProcessError):
                    with redirect_stderr(StringIO()):
                        A().run()
                assert os.getcwd() == starting_dir