#!/usr/bin/python3
"""
Test Driver for Shlib parsing in lib.py

Copyright (C) 2026 Alastair McKinstry <mckinstry@debian.org>
Released under the GPL-3 Gnu Public License.
"""

from click.testing import CliRunner
import dhfortran.lib as lib
from dhfortran.compilers import multiarch
import dhfortran.debhelper as dh
import dhfortran.tests.helpers as helpers
import pytest
import os


@pytest.fixture()
def set_env():
    os.environ["NO_ACT"] = "1"


class DummyHelper1:
    libs_pkgs = [ "libfoo1"]
    libdevel_pkgs = [ "libfoo-dev" ]

    def __init__(self):
        self.options = dh.build_options()
        self.options.flavor = "gfortran"

class DummyHelper2:
    libs_pkgs = [ 'libfoo1', 'libbar1' ]
    libdevel_pkgs = [ 'libfoo-dev' ]

    def __init__(self):
        self.options = dh.build_options()
        self.options.flavor = "gfortran"


def test_parse_shlib_normal1():
    """ Test shlib initial object and parsing """

    # standard library type
    libdir= f"/usr/lib/{multiarch}"    
    s = lib.Shlib(DummyHelper1(), f"{libdir}/libm.so.6", "libdummy-dev", "")
    assert s.kind == lib.Shlib.TYPE_NORMAL
    assert s.major == '6'
    assert s.ext == ".6"
    assert s.orig_soname == 'libm.so.6'

def test_parse_shlib_type_normal():
    """ 'Normal' type, with libfoo.so.1.2 or libfoo.so.1 naming, SOVER defined (TYPE_NORMAL)
    """
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), "dhfortran/tests/test01/libpoints.so.1.2", "libdummy-dev", "")
    assert s.kind == lib.Shlib.TYPE_NORMAL
    assert s.major == '1'
    assert s.ext == '.1.2'
    assert s.orig_soname == 'libpoints.so.1'

def test_parse_shlib_nover():
    """ Can we correcltly parse an SO with no versioning? (TYPE_NOVER)
    """
    # Nover library
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), "dhfortran/tests/test03/libpoints.so", "libpoints-dev", "")
    assert s.kind == lib.Shlib.TYPE_NOVER
    assert s.major == None
    assert s.ext == ''
    assert s.orig_soname == 'libpoints.so'

def test_parse_shlib_postfix1():
    """ gctp-type: libgctp-2.0.0.so (TYPE_POSTFIX1)
    """
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), f"dhfortran/tests/test04/libpoints-1.2.3.so", "libgctp-dev", "")    
    assert s.kind == lib.Shlib.TYPE_POSTFIX1
    assert s.major == '1'
    assert s.ext == ''
    assert s.orig_soname == 'libpoints-1.2.3.so'
    
def test_parse_shlib_postfix2():
    """" libzzipmmapped-0.so.13.0.78 type (TYPE_POSTFIX2)
    """
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), f"dhfortran/tests/test05/libpoints-0.so.13.0.78", "libdummy-dev", "")
    assert s.kind == lib.Shlib.TYPE_POSTFIX2
    assert s.major == '13'
    assert s.ext == '.13.0.78'
    assert s.orig_soname == 'libpoints-0.so.13'
    
def test_new_soname_normal1():
    """ Test the setting of a new soname """
    libdir= f"/usr/lib/{multiarch}"    
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), f"{libdir}/libm.so.6", "libpoints-dev", "")
    s.set_new_soname_and_libname()
    assert s.new_soname == 'libm-gnu.so.6'

def test_new_soname_normal2():
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), "dhfortran/tests/test01/libpoints.so.1.2", "libdummy-dev", "")
    s.set_new_soname_and_libname()
    assert s.new_soname == 'libpoints-gnu.so.1'
        
def test_new_soname_postfix1():
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), f"dhfortran/tests/test04/libpoints-1.2.3.so", "libpoints-dev", "")
    s.set_new_soname_and_libname()
    assert s.new_soname == 'libpoints-gnu-1.2.3.so'

def test_new_soname_postfix2():
    # Postfix wierd case (2)
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), f"dhfortran/tests/test05/libpoints-0.so.13.0.78", "libpoints-dev", "")
    s.set_new_soname_and_libname()    
    assert s.new_soname == "libpoints-gnu-0.so.13"

def test_new_soname_nover1():
    # Non-versioned library
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(DummyHelper1(), "dhfortran/tests/test03/libpoints.so", "libpoints-dev", "")
    s.set_new_soname_and_libname()
    assert s.new_soname == 'libpoints-gnu.so.1' # guessed from libfoo1

def test_new_soname_nover2():
    # non-versioned, no hint in libname
    d = DummyHelper1()
    d.libs_pkgs = []
    lib.Shlib.objs = {} # reset
    s = lib.Shlib(d, "dhfortran/tests/test03/libpoints.so", "libpoints-dev", "")
    s.set_new_soname_and_libname()
    assert s.new_soname == 'libpoints-gnu.so.0d' # guessed
    
    
if __name__ == "__main__":
    import pytest

    pytest.main()
