def test_create_jp2(monkeypatch, profile_name):
    import pykdu_compress
    workflow = workflow_make_jp2.MakeJp2Workflow()
    initial_results = []
    additional_data = {}
    user_args = {
        'Input': "some_source_path",
        "Output": "somepath",
        "Profile": profile_name
    }
    files_in_package = ["12345_1.tif", "12345_2.tif"]

    def mock_walk(root):
        return [("12345", ['access'], tuple(files_in_package))]

    def mock_scandir(path):
        for file_name in files_in_package:
            file_mock = Mock()
            file_mock.name = file_name
            file_mock.path = os.path.join(path, file_name)
            yield file_mock

    with monkeypatch.context() as mp:
        mp.setattr(os, "walk", mock_walk)
        mp.setattr(os, "scandir", mock_scandir)
        tasks_md = workflow.discover_task_metadata(
            initial_results=initial_results,
            additional_data=additional_data,
            **user_args)
    assert len(tasks_md) > 0
    working_dir = 'some_working_path'
    task_builder = speedwagon.tasks.TaskBuilder(
        speedwagon.tasks.MultiStageTaskBuilder(working_dir),
        working_dir=working_dir)
    for task_metadata in tasks_md:
        workflow.create_new_task(task_builder, **task_metadata)
    new_tasks = task_builder.build_task()

    created_files = []

    def mock_kdu_compress_cli2(infile: str,
                               outfile: str,
                               in_args=None,
                               out_args=None) -> int:
        created_files.append(outfile)
        return 0

    with monkeypatch.context() as mp:
        mp.setattr(workflow_make_jp2.os, "makedirs", Mock())
        mp.setattr(pykdu_compress, "kdu_compress_cli2", mock_kdu_compress_cli2)
        for n in new_tasks.subtasks:
            n.work()

    assert len(created_files) == len(files_in_package)
    assert all(
        source_file.replace(".tif", ".jp2") in
        [os.path.basename(created_file) for created_file in created_files]
        for source_file in files_in_package)
    def test_validate_user_options_success(self, monkeypatch):
        workflow = workflow_make_jp2.MakeJp2Workflow()
        user_args = {
            "Input": os.path.join("some", "source", "path"),
            "Output": os.path.join("some", "output", "path")
        }

        monkeypatch.setattr(workflow_make_jp2.os.path, "exists",
                            lambda path: True)

        monkeypatch.setattr(workflow_make_jp2.os.path, "isdir",
                            lambda path: True)

        assert workflow.validate_user_options(**user_args) is True
    def test_validate_user_options_output_file_is_error(self, monkeypatch):
        workflow = workflow_make_jp2.MakeJp2Workflow()
        user_args = {
            "Input": os.path.join("some", "source", "path"),
            "Output": os.path.join("some", "output", "file.txt")
        }

        # Simulate that the input is a file
        def isdir(path):
            if path == user_args["Output"]:
                return False
            return True

        monkeypatch.setattr(workflow_make_jp2.os.path, "exists",
                            lambda path: True)

        monkeypatch.setattr(workflow_make_jp2.os.path, "isdir", isdir)
        with pytest.raises(ValueError):
            workflow.validate_user_options(**user_args)
    def test_validate_user_options_input_not_exists(self, monkeypatch):
        workflow = workflow_make_jp2.MakeJp2Workflow()
        user_args = {
            "Input": os.path.join("some", "source", "path"),
            "Output": os.path.join("some", "output", "path")
        }

        # Simulate that the input directory does not exists
        def exists(path):
            if path == user_args["Output"]:
                return False
            return True

        monkeypatch.setattr(workflow_make_jp2.os.path, "exists", exists)

        monkeypatch.setattr(workflow_make_jp2.os.path, "isdir",
                            lambda path: True)
        with pytest.raises(ValueError):
            workflow.validate_user_options(**user_args)
def unconfigured_workflow():
    workflow = workflow_make_jp2.MakeJp2Workflow()
    user_options = {i.label_text: i.data for i in workflow.user_options()}

    return workflow, user_options
def test_make_jp2_workflow_options(index, label):
    workflow = workflow_make_jp2.MakeJp2Workflow()
    user_options = workflow.user_options()
    assert len(user_options) > 0
    assert user_options[index].label_text == label