import os
from unittest import mock

import pytest
from pyjob.lsf import LoadSharingFacilityTask


@pytest.mark.skipif(pytest.on_windows, reason="Unavailable on Windows")
@mock.patch("pyjob.lsf.LoadSharingFacilityTask._check_requirements")
class TestCreateRunscript(object):
    def test_1(self, check_requirements_mock):
        check_requirements_mock.return_value = None
        scripts = [pytest.helpers.get_py_script(i, 1) for i in range(1)]
        [s.write() for s in scripts]
        paths = [s.path for s in scripts]
        task = LoadSharingFacilityTask(paths)
        runscript = task._create_runscript()
        pytest.helpers.unlink(paths)
        assert runscript.shebang == "#!/bin/bash"
        assert runscript.content == [
            "#BSUB -cwd " + os.getcwd(),
            '#BSUB -R "span[ptile=1]"',
            "#BSUB -J pyjob",
            "#BSUB -o " + paths[0].replace(".py", ".log"),
            paths[0],
        ]

    def test_2(self, check_requirements_mock):
        check_requirements_mock.return_value = None
        scripts = [pytest.helpers.get_py_script(i, 1) for i in range(3)]
        [s.write() for s in scripts]
        paths = [s.path for s in scripts]
        task = LoadSharingFacilityTask(paths)
        runscript = task._create_runscript()
        logf = runscript.path.replace(".script", ".log")
        jobsf = runscript.path.replace(".script", ".jobs")
        with open(jobsf, "r") as f_in:
            jobs = [l.strip() for l in f_in]
        pytest.helpers.unlink(paths + [jobsf])
        assert runscript.shebang == "#!/bin/bash"
        assert runscript.content == [
            "#BSUB -cwd " + os.getcwd(),
            '#BSUB -R "span[ptile=1]"',
            "#BSUB -J pyjob[1-3]%3",
            "#BSUB -o {}".format(logf),
            'script=$(awk "NR==$(($LSB_JOBINDEX + 1))" {})'.format(jobsf),
            'log=$(echo $script | sed "s/\\.${script##*.}/\\.log/")',
            "$script > $log 2>&1",
        ]
        assert jobs == paths

    def test_3(self, check_requirements_mock):
        check_requirements_mock.return_value = None
        scripts = [pytest.helpers.get_py_script(i, 1) for i in range(3)]
        [s.write() for s in scripts]
        paths = [s.path for s in scripts]
        task = LoadSharingFacilityTask(paths, max_array_size=1)
        runscript = task._create_runscript()
        logf = runscript.path.replace(".script", ".log")