# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


import sys, os
import subprocess
import pytest
from os.path import join as jp
from pytest import raises
from jenkinsflow.flow import serial
from jenkinsflow.utils.set_build_description import set_build_description
from jenkinsflow.cli.cli import cli
from .framework import api_select
from .framework.cfg import jenkins_security
from . import cfg as test_cfg
from .cfg import ApiType


_here = os.path.dirname(os.path.abspath(__file__))


def _clear_description(api, job):
    if api.api_type == ApiType.SCRIPT:
        # TODO: There is no build number concept for script api, so we need to ensure a clean start
        description_file = jp(job.workspace, 'description.txt')
        if os.path.exists(description_file):
            os.remove(description_file)


def _get_description(api, job, build_number):
    # Read back description and verify
    if api.api_type == ApiType.JENKINS:
        build_url = "/job/" + job.name + '/' + str(build_number)
        dct = api.get_json(build_url, tree="description")
        return dct['description']

    if api.api_type == ApiType.SCRIPT:
        with open(jp(job.workspace, 'description.txt')) as ff:
            return ff.read()

    if api.api_type == ApiType.MOCK:
        raise Exception(repr(ApiType.MOCK) + " 'description' not supported by test framework")


@pytest.mark.not_apis(ApiType.MOCK)
def test_set_build_description_flow_set(api_type):

# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


# Path: test/set_build_description_test.py
import sys, os
import subprocess
import pytest
from os.path import join as jp
from pytest import raises
from jenkinsflow.flow import serial
from jenkinsflow.utils.set_build_description import set_build_description
from jenkinsflow.cli.cli import cli
from .framework import api_select
from .framework.cfg import jenkins_security
from . import cfg as test_cfg
from .cfg import ApiType



_here = os.path.dirname(os.path.abspath(__file__))


def _clear_description(api, job):
    if api.api_type == ApiType.SCRIPT:
        # TODO: There is no build number concept for script api, so we need to ensure a clean start
        description_file = jp(job.workspace, 'description.txt')
        if os.path.exists(description_file):
            os.remove(description_file)


def _get_description(api, job, build_number):
    # Read back description and verify
    if api.api_type == ApiType.JENKINS:
        build_url = "/job/" + job.name + '/' + str(build_number)
        dct = api.get_json(build_url, tree="description")
        return dct['description']

    if api.api_type == ApiType.SCRIPT:
        with open(jp(job.workspace, 'description.txt')) as ff:
            return ff.read()

    if api.api_type == ApiType.MOCK:
        raise Exception(repr(ApiType.MOCK) + " 'description' not supported by test framework")


@pytest.mark.not_apis(ApiType.MOCK)
def test_set_build_description_flow_set(api_type):

    with api_select.api(__file__, api_type, login=True) as api: