from io import StringIO
from pathlib import Path
from typing import List

import pytest

from stubber.minify import minify, python_minifier

# mark all tests
pytestmark = [pytest.mark.stubber, pytest.mark.minify]

SOURCE = '''
"""
Create stubs for (all) modules on a MicroPython board
"""
# Copyright (c) 2019-2022 Jos Verlinde
# pylint: disable= invalid-name, missing-function-docstring, import-outside-toplevel, logging-not-lazy
import gc
import logging
import sys

import uos as os
from ujson import dumps
print("test")

x = 1
'''


# test minify with different sources and targets
# sources:              Targets:
# - file                Path
# - string              StringIO
# - stringIO
def test_minify_file_to_folder(tmp_path: Path, pytestconfig: pytest.Config):
    "file -> folder"
    # Skip minification test if no minifier tool is available
    if not python_minifier:
        pytest.skip("Python minifier not available")

    source = "createstubs.py"
    source_path = pytestconfig.rootpath / "src" / "stubber" / "board" / source
    result = minify(source=source_path, target=tmp_path)
    assert result == 0
    # now test that log statements have been removed
    with open(tmp_path / source) as f:
        content = f.readlines()
    check_results(content)


def test_minify_str_to_folder(tmp_path: Path, pytestconfig: pytest.Config):
    """string -> folder"""
    # Skip minification test if no minifier tool is available
    if not python_minifier:
        pytest.skip("Python minifier not available")

    result = minify(source=SOURCE, target=tmp_path)
    assert result == 0
    # now test that log statements have been removed
    with open(tmp_path / "minified.py") as f:
        content = f.readlines()
    check_results(content)


def test_minify_str_to_strio(tmp_path: Path, pytestconfig: pytest.Config):
    "string -> file in folder"
    # Skip minification test if no minifier tool is available
    if not python_minifier:
        pytest.skip("Python minifier not available")

    target = StringIO()
    ret = minify(source=SOURCE, target=target)
    assert ret == 0
    result = target.getvalue()
    target.close()
    content = result.splitlines()
    check_results(content)


def test_minify_strio_to_strio(tmp_path: Path, pytestconfig: pytest.Config):
    "string -> file in folder"
    # Skip minification test if no minifier tool is available
    if not python_minifier:
        pytest.skip("Python minifier not available")

    source = StringIO(initial_value=SOURCE)
    target = StringIO()
    ret = minify(source=source, target=target)
    assert ret == 0
    result = target.getvalue()
    target.close()
    content = result.splitlines()
    check_results(content)


def check_results(content: List[str]):
    for line in content:
        assert line.find("._log") == -1, "Not all references to ._log have been removed"
        assert line.find(".log") == -1, "Not all references to .log have been removed"

