# Path: siptools/utils.py
# def decode_path(path, suffix=''):
#     """
#     Decode given path from URL encoding and remove given suffix.
# 
#     :path: Path to decode
#     :suffix: Suffix to remove
#     :returns: Decoded string without given suffix
#     """
#     if six.PY2:
#         path = unquote_plus(path.encode("utf-8")).decode("utf-8")
#     else:
#         path = unquote_plus(path)
# 
#     if path.endswith(suffix):
#         path = path.replace(suffix, '', 1)
#     return path
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None


import os
import pytest
import lxml.etree as ET
import siptools.scripts.create_addml as create_addml
from siptools.utils import decode_path, read_md_references

@pytest.mark.parametrize("file_, base_path", [
    ('tests/data/csvfile.csv', ''),
    ('./tests/data/csvfile.csv', ''),
    ('csvfile.csv', 'tests/data'),
    ('./csvfile.csv', './tests/data'),
    ('data/csvfile.csv', 'absolute')
])
def test_paths(testpath, file_, base_path, run_cli):
    """ Test the following path arguments:
    (1) Path without base_path
    (2) Path without base bath, but with './'
    (3) Path with base path
    (4) Path with base path and with './'
    (5) Absolute base path
    """
    if 'absolute' in base_path:
        base_path = os.path.join(os.getcwd(), 'tests')

    if base_path != '':
        run_cli(create_addml.main, [
            '--delim', DELIMITER, '--charset', CHARSET,
            '--sep', RECORDSEPARATOR, '--quot', QUOTINGCHAR,
            '--workspace', testpath, '--base_path', base_path, file_])
    else:
        run_cli(create_addml.main, [
            '--delim', DELIMITER, '--charset', CHARSET,
            '--sep', RECORDSEPARATOR, '--quot', QUOTINGCHAR,
            '--workspace', testpath, file_])


# Path: siptools/utils.py
# def decode_path(path, suffix=''):
#     """
#     Decode given path from URL encoding and remove given suffix.
# 
#     :path: Path to decode
#     :suffix: Suffix to remove
#     :returns: Decoded string without given suffix
#     """
#     if six.PY2:
#         path = unquote_plus(path.encode("utf-8")).decode("utf-8")
#     else:
#         path = unquote_plus(path)
# 
#     if path.endswith(suffix):
#         path = path.replace(suffix, '', 1)
#     return path
# 
# def read_md_references(workspace, ref_file):
#     """Read all the MD IDs as a dictionary.
# 
#     :workspace: path to workspace directory
#     :ref_file: Metadata reference file
#     :returns: A dict of references or None if reference file doesn't
#               exist
#     """
#     reference_file = os.path.join(workspace, ref_file)
# 
#     if os.path.isfile(reference_file):
#         references = {}
#         with open(reference_file) as in_file:
#             for line in in_file:
#                 references.update(json.loads(line))
#         return references
#     return None


# Path: tests/scripts/test_create_addml.py
import os
import pytest
import lxml.etree as ET
import siptools.scripts.create_addml as create_addml
from siptools.utils import decode_path, read_md_references


@pytest.mark.parametrize("file_, base_path", [
    ('tests/data/csvfile.csv', ''),
    ('./tests/data/csvfile.csv', ''),
    ('csvfile.csv', 'tests/data'),
    ('./csvfile.csv', './tests/data'),
    ('data/csvfile.csv', 'absolute')
])
def test_paths(testpath, file_, base_path, run_cli):
    """ Test the following path arguments:
    (1) Path without base_path
    (2) Path without base bath, but with './'
    (3) Path with base path
    (4) Path with base path and with './'
    (5) Absolute base path
    """
    if 'absolute' in base_path:
        base_path = os.path.join(os.getcwd(), 'tests')

    if base_path != '':
        run_cli(create_addml.main, [
            '--delim', DELIMITER, '--charset', CHARSET,
            '--sep', RECORDSEPARATOR, '--quot', QUOTINGCHAR,
            '--workspace', testpath, '--base_path', base_path, file_])
    else:
        run_cli(create_addml.main, [
            '--delim', DELIMITER, '--charset', CHARSET,
            '--sep', RECORDSEPARATOR, '--quot', QUOTINGCHAR,
            '--workspace', testpath, file_])


    references = read_md_references(testpath,