def test_create_path_with_content_clear_option(tmp_dir: pathlib.Path):
    """Test create path with content clear option."""
    # create trestle project
    test_utils.ensure_trestle_config_dir(tmp_dir)

    # create directories and a file
    tmp_data_dir = tmp_dir.joinpath('data')
    tmp_data_dir_file = tmp_data_dir.joinpath('readme.md')
    cpa = CreatePathAction(tmp_data_dir_file)
    cpa.execute()
    assert len(cpa.get_created_paths()) == 2
    assert tmp_data_dir.exists()
    assert tmp_data_dir_file.exists()

    # write some content in the file
    file_pos = 0
    dummy_data = ''
    with open(tmp_data_dir_file, 'a+') as fp:
        fp.write(dummy_data)
        file_pos = fp.tell()
    assert file_pos >= 0

    # create action to create a file without clearing content
    cpa = CreatePathAction(tmp_data_dir_file, clear_content=False)
    cpa.execute()
    assert len(cpa.get_created_paths()) == 0
    assert tmp_data_dir_file.exists()
    with open(tmp_data_dir_file, 'a+') as fp:
        assert file_pos == fp.tell()

    # create action to create a file with clearing content
    cpa = CreatePathAction(tmp_data_dir_file, clear_content=True)
    cpa.execute()
    assert len(cpa.get_created_paths()) == 0
    assert tmp_data_dir_file.exists()
    with open(tmp_data_dir_file, 'a+') as fp:
        assert 0 == fp.tell()
        data = fp.readline()
        assert data == ''

    # rollback should bring back the cleared content
    cpa.rollback()
    assert tmp_data_dir.exists()
    assert tmp_data_dir_file.exists()
    with open(tmp_data_dir_file, 'a+') as fp:
        assert file_pos == fp.tell()
        fp.readline()

    # clearing content on direction should have no effect of the flag
    tmp_data_dir2 = tmp_dir / 'data2'
    tmp_data_dir2.mkdir()
    cpa = CreatePathAction(tmp_data_dir2, clear_content=True)
    cpa.execute()
    assert len(cpa.get_created_paths()) == 0
    assert tmp_data_dir2.exists()
    cpa.rollback()
    assert tmp_data_dir2.exists()
def test_create_path_execute(tmp_dir: pathlib.Path):
    """Test create path execute."""
    tmp_data_dir = tmp_dir.joinpath('data')

    try:
        # no trestle project should error
        cpa = CreatePathAction(tmp_data_dir)
    except TrestleError:
        pass

    test_utils.ensure_trestle_config_dir(tmp_dir)

    try:
        # invalid sub_path type should error
        cpa = CreatePathAction(('tests/invalid/sub_path'))
    except TrestleError:
        pass

    # create directories
    cpa = CreatePathAction(tmp_data_dir)

    assert tmp_data_dir.exists() is False
    cpa.execute()
    assert len(cpa.get_created_paths()) == 1
    assert tmp_data_dir.exists()

    cpa.rollback()
    assert tmp_data_dir.exists() is False

    # create directories and a file
    tmp_data_dir_file = tmp_data_dir.joinpath('readme.md')
    cpa = CreatePathAction(tmp_data_dir_file)
    assert cpa.get_trestle_project_root() == tmp_dir

    assert tmp_data_dir.exists() is False
    assert tmp_data_dir_file.exists() is False
    cpa.execute()
    assert len(cpa.get_created_paths()) == 2
    assert tmp_data_dir.exists()
    assert tmp_data_dir_file.exists()

    cpa.rollback()
    assert tmp_data_dir.exists() is False
    assert tmp_data_dir_file.exists() is False
Exemplo n.º 3
0
def test_create_path_execute(tmp_path: pathlib.Path):
    """Test create path execute."""
    tmp_data_dir = tmp_path.joinpath('data')

    with pytest.raises(TrestleError):
        # no trestle project should error
        cpa = CreatePathAction(tmp_data_dir)

    test_utils.ensure_trestle_config_dir(tmp_path)

    # create directories
    cpa = CreatePathAction(tmp_data_dir)

    assert tmp_data_dir.exists() is False
    cpa.execute()
    assert len(cpa.get_created_paths()) == 1
    assert tmp_data_dir.exists()

    cpa.rollback()
    assert tmp_data_dir.exists() is False

    # create directories and a file
    tmp_data_dir_file = tmp_data_dir.joinpath('readme.md')
    cpa = CreatePathAction(tmp_data_dir_file)
    assert cpa.get_trestle_project_root() == tmp_path

    assert tmp_data_dir.exists() is False
    assert tmp_data_dir_file.exists() is False
    cpa.execute()
    assert len(cpa.get_created_paths()) == 2
    assert tmp_data_dir.exists()
    assert tmp_data_dir_file.exists()

    cpa.rollback()
    assert tmp_data_dir.exists() is False
    assert tmp_data_dir_file.exists() is False