<commit_before>""" Tests for the various cli programs """

import os
import sys
import unittest
import subprocess
from importlib import import_module
from pathlib import Path
from git import Repo
from pyontutils.utils import working_dir, TermColors as tc

from pyontutils.config import devconfig
p1 = Path(__file__).resolve().absolute().parent.parent.parent
p2 = Path(devconfig.git_local_base).resolve().absolute()
print(p1, p2)
if not (p2 / devconfig.ontology_repo).exists():
    if p1 != p2:
        devconfig.git_local_base = p1

from pyontutils import scigraph
from pyontutils import core
from pyontutils import scigraph_client

# orig_basepath = scigraph_client.BASEPATH
orig_basepath = 'https://scicrunch.org/api/1/scigraph'

if 'SCICRUNCH_API_KEY' in os.environ:
    devconfig.scigraph_api = orig_basepath
    scigraph.scigraph_client.BASEPATH = orig_basepath
else:
    local_basepath = 'http://localhost:9000/scigraph'
    devconfig.scigraph_api = local_basepath
    scigraph.scigraph_client.BASEPATH = local_basepath

checkout_ok = 'NIFSTD_CHECKOUT_OK' in os.environ

class Folders(unittest.TestCase):
    _folders =  ('ttl', 'ttl/generated', 'ttl/generated/parcellation', 'ttl/bridge')
    def setUp(self):
        #print('SET UP')
        #print(devconfig.ontology_local_repo)
        if devconfig.ontology_local_repo.isDefault:
            self.fake_local_repo = Path(devconfig.git_local_base, devconfig.ontology_repo)
            if not self.fake_local_repo.exists():  # do not klobber existing
                self.folders = [(self.fake_local_repo / folder)
                                for folder in self._folders]
                self.addCleanup(self._tearDown)
                #print(f'CREATING FOLDERS {self.folders}')
                for folder in self.folders:
                    folder.mkdir(parents=True)
                    # if the parent doesn't exist then there should never
                    # be a case where there is a collision (yes?)

        else:
            self.folders = []

    def recursive_clean(self, d):
        for thing in d.iterdir():
            if thing.is_dir():
                self.recursive_clean(thing)
            else:
                thing.unlink()  # will rm the file

        d.rmdir()

    def _tearDown(self):
        #print('TEAR DOWN')
        if self.folders:
            #print(f'DELETING FOLDERS {self.folders}')
            self.recursive_clean(self.fake_local_repo)


class TestCli(Folders):
    commands = (
        ['graphml-to-ttl', '--help'],
        ['ilxcli', '--help'],
        ['necromancy', '--help'],
        ['ontload', '--help'],
        ['ontree', '--help'],
        ['overlaps', '--help'],
        ['qnamefix', '--help'],
        ['registry-sync', '--test'],
        ['scigraph-codegen', '--help'],
        ['scigraph-deploy', '--help'],
        ['scig', '--help'],
        ['ttlfmt', '--help'],
        [sys.executable, 'resolver/make_config.py'],
        # strange that make_config failed in travis in a pipenv as couldn't find pyontutils?
    )

    def test_cli(self):
        # we still run these tests to make sure that the install process works as expected
        failed = []
        for command in self.commands:
            try:
                output = subprocess.check_output(command,
                                                 stderr=subprocess.STDOUT).decode().rstrip()
            except BaseException as e:
                failed.append((command, e, e.stdout if hasattr(e, 'stdout') else '', ''))

        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)


class TestScripts(Folders):
    """ Import everything and run main() on a subset of those
        NOTE If you are debugging this. Most of the functions in this
        class are defined dynamically by populate_tests, and you will not
        find their code here. """
    # NOTE printing issues here have to do with nose not suppressing printing during coverage tests

    def setUp(self, checkout_ok=checkout_ok):
        super().setUp()
        if not hasattr(self, '_modules'):
            self.__class__._modules = {}

        if not hasattr(self, '_do_mains'):
            self.__class__._do_mains = []
            self.__class__._do_tests = []

    def notest_mains(self):
        failed = []
        if not self._do_mains:
            raise ValueError('test_imports did not complete successfully')
        for script, argv in self._do_mains:
            pass
        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)

    def notest_tests(self):
        failed = []
        for script in self._do_tests:
            try:
                script.test()
            except BaseException as e:
                failed.append((script, e))

        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)


def populate_tests():
    skip = ('cocomac_uberon',  # known broken
            'old_neuron_example',  # known broken
            'cuts',  # issues with neuron_models.compiled vs load from ontology
    )
    if 'TRAVIS' in os.environ:
        skip += ('librdf',  # getting python3-librdf installed is too much of a pain atm
        )

    lasts = tuple()
    neurons = ('neurons',
               'neuron_lang',
               'neuron_example',
               'phenotype_namespaces',
               'neuron_models/allen_cell_types',
               'neuron_models/phenotype_direct',
               'neuron_models/basic_neurons',
               'neuron_models/huang2017',
               'neuron_models/ma2015',
               'neuron_models/cuts',
               'nif_neuron',
              )
    print('checkout ok:', checkout_ok)

    ont_branch = Repo(devconfig.ontology_local_repo).active_branch.name
    if not checkout_ok and ont_branch != 'neurons':
        skip += tuple(n.split('/')[-1] for n in neurons)  # FIXME don't use stem below
    else:
        lasts += tuple(f'pyontutils/{s}.py' for s in neurons)

    ban = Path(devconfig.ontology_local_repo, 'ttl/BIRNLex_annotation_properties.ttl').as_posix()
    mba = Path(devconfig.ontology_local_repo, 'ttl/generated/parcellation/mbaslim.ttl').as_posix()
    nifttl = Path(devconfig.ontology_local_repo, 'ttl/nif.ttl').as_posix()
    nsmethodsobo = Path(devconfig.git_local_base, 'methodsOntology/source-material/ns_methods.obo').as_posix()
    zap = 'git checkout $(git ls-files {*,*/*,*/*/*}.ttl)'
    mains = {'nif_cell':None,
             'methods':None,
             'scigraph':None,
             'hbp_cells':None,
             'combinators':None,
             'chebi_bridge':None,
             'closed_namespaces':None,
             'gen_nat_models':None,
             'hierarchies':None,
             'nif_neuron':None,
             'cocomac_uberon':None,
             #'docs':['ont-docs'],  # can't seem to get this to work correctly on travis so leaving it out for now
             'make_catalog':['ont-catalog', '--jobs', '1'],
             'parcellation':['parcellation', '--jobs', '1'],
             'graphml_to_ttl':['graphml-to-ttl', 'development/methods/methods_isa.graphml'],
    #['ilxcli', '--help'],
             'obo_io':['obo-io', '--ttl', nsmethodsobo],
    'ttlfmt':[['ttlfmt', ban],
              #[zap]
             ],
    'qnamefix':[['qnamefix', ban],
                ['qnamefix', mba],
                #[zap]
               ],<commit_msg>test cli test ttlfmt version<commit_after>""" Tests for the various cli programs """

import os
import sys
import unittest
import subprocess
from importlib import import_module
from pathlib import Path
from git import Repo
from pyontutils.utils import working_dir, TermColors as tc

from pyontutils.config import devconfig
p1 = Path(__file__).resolve().absolute().parent.parent.parent
p2 = Path(devconfig.git_local_base).resolve().absolute()
print(p1, p2)
if not (p2 / devconfig.ontology_repo).exists():
    if p1 != p2:
        devconfig.git_local_base = p1

from pyontutils import scigraph
from pyontutils import core
from pyontutils import scigraph_client

# orig_basepath = scigraph_client.BASEPATH
orig_basepath = 'https://scicrunch.org/api/1/scigraph'

if 'SCICRUNCH_API_KEY' in os.environ:
    devconfig.scigraph_api = orig_basepath
    scigraph.scigraph_client.BASEPATH = orig_basepath
else:
    local_basepath = 'http://localhost:9000/scigraph'
    devconfig.scigraph_api = local_basepath
    scigraph.scigraph_client.BASEPATH = local_basepath

checkout_ok = 'NIFSTD_CHECKOUT_OK' in os.environ

class Folders(unittest.TestCase):
    _folders =  ('ttl', 'ttl/generated', 'ttl/generated/parcellation', 'ttl/bridge')
    def setUp(self):
        #print('SET UP')
        #print(devconfig.ontology_local_repo)
        if devconfig.ontology_local_repo.isDefault:
            self.fake_local_repo = Path(devconfig.git_local_base, devconfig.ontology_repo)
            if not self.fake_local_repo.exists():  # do not klobber existing
                self.folders = [(self.fake_local_repo / folder)
                                for folder in self._folders]
                self.addCleanup(self._tearDown)
                #print(f'CREATING FOLDERS {self.folders}')
                for folder in self.folders:
                    folder.mkdir(parents=True)
                    # if the parent doesn't exist then there should never
                    # be a case where there is a collision (yes?)

        else:
            self.folders = []

    def recursive_clean(self, d):
        for thing in d.iterdir():
            if thing.is_dir():
                self.recursive_clean(thing)
            else:
                thing.unlink()  # will rm the file

        d.rmdir()

    def _tearDown(self):
        #print('TEAR DOWN')
        if self.folders:
            #print(f'DELETING FOLDERS {self.folders}')
            self.recursive_clean(self.fake_local_repo)


class TestCli(Folders):
    commands = (
        ['graphml-to-ttl', '--help'],
        ['ilxcli', '--help'],
        ['necromancy', '--help'],
        ['ontload', '--help'],
        ['ontree', '--help'],
        ['overlaps', '--help'],
        ['qnamefix', '--help'],
        ['registry-sync', '--test'],
        ['scigraph-codegen', '--help'],
        ['scigraph-deploy', '--help'],
        ['scig', '--help'],
        ['ttlfmt', '--help'],
        [sys.executable, 'resolver/make_config.py'],
        # strange that make_config failed in travis in a pipenv as couldn't find pyontutils?
    )

    def test_cli(self):
        # we still run these tests to make sure that the install process works as expected
        failed = []
        for command in self.commands:
            try:
                output = subprocess.check_output(command,
                                                 stderr=subprocess.STDOUT).decode().rstrip()
            except BaseException as e:
                failed.append((command, e, e.stdout if hasattr(e, 'stdout') else '', ''))

        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)


class TestScripts(Folders):
    """ Import everything and run main() on a subset of those
        NOTE If you are debugging this. Most of the functions in this
        class are defined dynamically by populate_tests, and you will not
        find their code here. """
    # NOTE printing issues here have to do with nose not suppressing printing during coverage tests

    def setUp(self, checkout_ok=checkout_ok):
        super().setUp()
        if not hasattr(self, '_modules'):
            self.__class__._modules = {}

        if not hasattr(self, '_do_mains'):
            self.__class__._do_mains = []
            self.__class__._do_tests = []

    def notest_mains(self):
        failed = []
        if not self._do_mains:
            raise ValueError('test_imports did not complete successfully')
        for script, argv in self._do_mains:
            pass
        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)

    def notest_tests(self):
        failed = []
        for script in self._do_tests:
            try:
                script.test()
            except BaseException as e:
                failed.append((script, e))

        assert not failed, '\n'.join('\n'.join(str(e) for e in f) for f in failed)


def populate_tests():
    skip = ('cocomac_uberon',  # known broken
            'old_neuron_example',  # known broken
            'cuts',  # issues with neuron_models.compiled vs load from ontology
    )
    if 'TRAVIS' in os.environ:
        skip += ('librdf',  # getting python3-librdf installed is too much of a pain atm
        )

    lasts = tuple()
    neurons = ('neurons',
               'neuron_lang',
               'neuron_example',
               'phenotype_namespaces',
               'neuron_models/allen_cell_types',
               'neuron_models/phenotype_direct',
               'neuron_models/basic_neurons',
               'neuron_models/huang2017',
               'neuron_models/ma2015',
               'neuron_models/cuts',
               'nif_neuron',
              )
    print('checkout ok:', checkout_ok)

    ont_branch = Repo(devconfig.ontology_local_repo).active_branch.name
    if not checkout_ok and ont_branch != 'neurons':
        skip += tuple(n.split('/')[-1] for n in neurons)  # FIXME don't use stem below
    else:
        lasts += tuple(f'pyontutils/{s}.py' for s in neurons)

    ban = Path(devconfig.ontology_local_repo, 'ttl/BIRNLex_annotation_properties.ttl').as_posix()
    mba = Path(devconfig.ontology_local_repo, 'ttl/generated/parcellation/mbaslim.ttl').as_posix()
    nifttl = Path(devconfig.ontology_local_repo, 'ttl/nif.ttl').as_posix()
    nsmethodsobo = Path(devconfig.git_local_base, 'methodsOntology/source-material/ns_methods.obo').as_posix()
    zap = 'git checkout $(git ls-files {*,*/*,*/*/*}.ttl)'
    mains = {'nif_cell':None,
             'methods':None,
             'scigraph':None,
             'hbp_cells':None,
             'combinators':None,
             'chebi_bridge':None,
             'closed_namespaces':None,
             'gen_nat_models':None,
             'hierarchies':None,
             'nif_neuron':None,
             'cocomac_uberon':None,
             #'docs':['ont-docs'],  # can't seem to get this to work correctly on travis so leaving it out for now
             'make_catalog':['ont-catalog', '--jobs', '1'],
             'parcellation':['parcellation', '--jobs', '1'],
             'graphml_to_ttl':['graphml-to-ttl', 'development/methods/methods_isa.graphml'],
    #['ilxcli', '--help'],
             'obo_io':['obo-io', '--ttl', nsmethodsobo],
    'ttlfmt':[['ttlfmt', ban],
              ['ttlfmt', '--version'],
              #[zap]
             ],
    'qnamefix':[['qnamefix', ban],
                ['qnamefix', mba],
                #[zap]
               ],
{'fraction_non_alphanumeric': 0.12259810554803789, 'fraction_numerical': 0.0033829499323410014, 'mean_word_length': 2.2308196721311475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '301482', 'n_tokens_mistral': 4659, 'n_tokens_neox': 4484, 'n_words': 1076}