# Path: matl_online/matl.py
# def matl(octave, flags, code='', inputs='', version='', folder='', line_handler=None):
#     """Open a session with Octave and manages input/output as well as errors."""
#     # Remember what directory octave is current in
#     def escape(x):
#         return x.replace("'", "''")
# 
#     # Change directories to the temporary folder so that all temporary
#     # files are placed in here and won't interfere with other requests
#     octave.eval("cd('%s');" % escape(folder))
# 
#     # Add the folder for the appropriate MATL version
#     matl_folder = get_matl_folder(version)
#     cmd = "addpath('%s')" % escape(matl_folder)
#     octave.eval(cmd)
# 
#     code = ["'%s'" % escape(item) for item in code.split('\n')]
#     code = '{' + ','.join(code) + '}'
# 
#     if len(inputs):
#         inputs = ["'%s'" % escape(item) for item in inputs.split('\n')]
#         cmd = "matl_runner('%s', %s, %s);\n" % (flags, code, ','.join(inputs))
#     else:
#         inputs = None
#         cmd = "matl_runner('%s', %s);\n" % (flags, code)
# 
#     # Actually run the MATL code
#     octave.eval(cmd, line_handler=line_handler)
# 
#     # Change back to the original directory
#     cmd = "cd('%s')" % escape(current_app.config['PROJECT_ROOT'])
#     octave.eval(cmd)
# 
# Path: matl_online/utils.py
# def parse_iso8601(date):
#     """Convert a date in ISO 8601 format (used by github) to a datetime object."""
#     return datetime.strptime(date, ISO8601_FORMAT)
# 
# ISO8601_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
# 
# Path: matl_online/public/models.py
# class Release(Model):
#     """Model for storing metadata associated with MATL releases."""
# 
#     __tablename__ = 'releases'
# 
#     id = Column(db.Integer, primary_key=True)
#     tag = Column(db.String, unique=True, nullable=False)
#     date = Column(db.DateTime, unique=True, nullable=False)
# 
#     def __repr__(self):
#         """Create a custom string representation."""
#         return '<Release %r>' % self.tag
# 
#     @hybrid_property
#     def version(self):
#         """Convert release number to tuple for comparisons."""
#         return tuple(int(x) for x in self.tag.split('.'))
# 
#     @classmethod
#     def latest(cls):
#         """Get the latest release from Github."""
#         releases = cls.query.all()
#         if len(releases) == 0:
#             return None
# 
#         releases.sort(key=lambda x: x.version)
#         return releases[-1]
# 
# Path: tests/factories.py
# class DocumentationLinkFactory(BaseFactory):
#     """Factory for creating DocumentationLink objects on demand."""
# 
#     # The default link will simply append .html to the end of the name
#     link = LazyAttribute(lambda o: o.name + '.html')
# 
#     class Meta:
#         """Meta information for the factory."""
# 
#         model = DocumentationLink


import base64
import json
import os
import pytest
import shutil
from bs4 import BeautifulSoup
from datetime import datetime
from matl_online import matl
from matl_online.utils import parse_iso8601, ISO8601_FORMAT
from matl_online.public.models import Release
from .factories import DocumentationLinkFactory as DocLink
"""Unit tests for module for interacting with octave / MATL."""




TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')


class TestSourceCache:
    """Series of tests to check if source code is managed properly."""

    def test_no_source_no_install(self, app, tmpdir):
        """The source folder does not exist and we won't create it."""
        app.config['MATL_FOLDER'] = tmpdir.strpath

# Path: matl_online/matl.py
# def matl(octave, flags, code='', inputs='', version='', folder='', line_handler=None):
#     """Open a session with Octave and manages input/output as well as errors."""
#     # Remember what directory octave is current in
#     def escape(x):
#         return x.replace("'", "''")
# 
#     # Change directories to the temporary folder so that all temporary
#     # files are placed in here and won't interfere with other requests
#     octave.eval("cd('%s');" % escape(folder))
# 
#     # Add the folder for the appropriate MATL version
#     matl_folder = get_matl_folder(version)
#     cmd = "addpath('%s')" % escape(matl_folder)
#     octave.eval(cmd)
# 
#     code = ["'%s'" % escape(item) for item in code.split('\n')]
#     code = '{' + ','.join(code) + '}'
# 
#     if len(inputs):
#         inputs = ["'%s'" % escape(item) for item in inputs.split('\n')]
#         cmd = "matl_runner('%s', %s, %s);\n" % (flags, code, ','.join(inputs))
#     else:
#         inputs = None
#         cmd = "matl_runner('%s', %s);\n" % (flags, code)
# 
#     # Actually run the MATL code
#     octave.eval(cmd, line_handler=line_handler)
# 
#     # Change back to the original directory
#     cmd = "cd('%s')" % escape(current_app.config['PROJECT_ROOT'])
#     octave.eval(cmd)
# 
# Path: matl_online/utils.py
# def parse_iso8601(date):
#     """Convert a date in ISO 8601 format (used by github) to a datetime object."""
#     return datetime.strptime(date, ISO8601_FORMAT)
# 
# ISO8601_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
# 
# Path: matl_online/public/models.py
# class Release(Model):
#     """Model for storing metadata associated with MATL releases."""
# 
#     __tablename__ = 'releases'
# 
#     id = Column(db.Integer, primary_key=True)
#     tag = Column(db.String, unique=True, nullable=False)
#     date = Column(db.DateTime, unique=True, nullable=False)
# 
#     def __repr__(self):
#         """Create a custom string representation."""
#         return '<Release %r>' % self.tag
# 
#     @hybrid_property
#     def version(self):
#         """Convert release number to tuple for comparisons."""
#         return tuple(int(x) for x in self.tag.split('.'))
# 
#     @classmethod
#     def latest(cls):
#         """Get the latest release from Github."""
#         releases = cls.query.all()
#         if len(releases) == 0:
#             return None
# 
#         releases.sort(key=lambda x: x.version)
#         return releases[-1]
# 
# Path: tests/factories.py
# class DocumentationLinkFactory(BaseFactory):
#     """Factory for creating DocumentationLink objects on demand."""
# 
#     # The default link will simply append .html to the end of the name
#     link = LazyAttribute(lambda o: o.name + '.html')
# 
#     class Meta:
#         """Meta information for the factory."""
# 
#         model = DocumentationLink


# Path: tests/test_matl.py
import base64
import json
import os
import pytest
import shutil
from bs4 import BeautifulSoup
from datetime import datetime
from matl_online import matl
from matl_online.utils import parse_iso8601, ISO8601_FORMAT
from matl_online.public.models import Release
from .factories import DocumentationLinkFactory as DocLink

"""Unit tests for module for interacting with octave / MATL."""




TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')


class TestSourceCache:
    """Series of tests to check if source code is managed properly."""

    def test_no_source_no_install(self, app, tmpdir):
        """The source folder does not exist and we won't create it."""
        app.config['MATL_FOLDER'] = tmpdir.strpath

        folder = matl.get_matl_folder('18.3.0', install=False)