Skip to content

Commit

Permalink
Bug fix languages (#17)
Browse files Browse the repository at this point in the history
* testing started of this feature (test_vocola_header_new_file.py)
* improved the include messages for new vcl files
  • Loading branch information
quintijn committed Nov 17, 2022
1 parent 5579511 commit 6eda8a2
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/vocola2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Vocola"""

__version__ = '3.1.4' # bug fix
__version__ = '3.1.5' # bug fix
## now in versions 3.1. series
# __version__ = '2.9.6' # sendkeys again from Vocola, but via dtactions.
# __version__ = '2.9.5' # make ready for improved pip
Expand Down
24 changes: 20 additions & 4 deletions src/vocola2/_vocola_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import natlink
from natlinkcore import natlinkutils
from natlinkcore import readwritefile
import VocolaUtils
import natlinkvocolastartup # was natlinkstartup in natlinkmain...
from vocola2 import VocolaUtils
from vocola2 import natlinkvocolastartup # was natlinkstartup in natlinkmain...
from vocola2.exec.vcl2py.main import main_routine # main.main_routine compile function
from importlib.metadata import version
from pathlib import Path
Expand Down Expand Up @@ -106,6 +106,7 @@
if language != 'enx':
print(f' language: "{language}"')
if status.getVocolaTakesLanguages():
# addition to comment line in new .vcl files () (self.openCommandFile)
VocolaUserLanguageDirectory = os.path.join(VocolaUserDirectory, language)
if not os.path.exists(VocolaUserLanguageDirectory):
os.mkdir(VocolaUserLanguageDirectory)
Expand Down Expand Up @@ -457,9 +458,10 @@ def openCommandFile(self, file, comment):

path = self.FindExistingCommandFile(file)
if not path:
after_comment = self.get_after_comment_new_vcl_file()
path = commandFolder + '\\' + file
rwfile = readwritefile.ReadWriteFile()
rwfile.writeAnything(path, f'# {comment} \n\n')
rwfile.writeAnything(path, f'# {comment}{after_comment}\n')

# with open(path, 'w', encoding='ascii') as fp:
# fp.write(f'# {comment} \n\n')
Expand All @@ -481,7 +483,21 @@ def openCommandFile(self, file, comment):
# os.spawnv(os.P_NOWAIT, prog, [prog, path])
natlink.execScript("AppBringUp \"" + path + "\", \"" + path + "\"")


def get_after_comment_new_vcl_file(self):
"""get language dependent and unimacro actions dependent start
of a new vcl command file
"""
language_comment_addition = '' # at new command file
include_unimacro_line = ''
if status.getVocolaTakesLanguages():
if language != 'enx':
language_comment_addition = f' (language: {language})'

if status.getVocolaTakesUnimacroActions():
include_unimacro_line = 'include Unimacro.vch;\n'
if status.getVocolaTakesLanguages() and language != 'enx':
include_unimacro_line = 'include ..\\Unimacro.vch;\n'
return f'{language_comment_addition}\n{include_unimacro_line}\n'

###########################################################################
# #
Expand Down
2 changes: 1 addition & 1 deletion src/vocola2/natlinkvocolastartup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def create_new_language_subdirectory_if_needed():
commandFolder = None

if VocolaEnabled and status.getVocolaTakesLanguages():
if language != 'enx ' and commandFolder:
if language != 'enx' and commandFolder:
uDir = commandFolder
uDir2 = os.path.join(uDir, language)
if not os.path.isdir(uDir2):
Expand Down
113 changes: 113 additions & 0 deletions tests/test_vocola_header_new_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from pathlib import Path
import pytest
from natlinkcore import natlinkstatus
from natlinkcore.loader import *

status = natlinkstatus.NatlinkStatus()

thisDir = Path(__file__).parent
thisFileName = Path(__file__).stem + ".py"


## taken from natlinkcore, test_loader:

class MockLoggingHandler(logging.Handler):
"""Mock logging handler to check for expected logs."""

def __init__(self, *args, **kwargs):
self.messages: Dict[str, List[str]] = {}
self.reset()
logging.Handler.__init__(self, *args, **kwargs)

def emit(self, record):
self.messages[record.levelname.lower()].append(record.getMessage())

def reset(self):
self.messages = {
'debug': [],
'info': [],
'warning': [],
'error': [],
'critical': [],
}

@pytest.fixture()
def empty_config():
config = NatlinkConfig.get_default_config()
return config

@pytest.fixture()
def logger():
_logger = logging.Logger('natlink')
_logger.setLevel(logging.DEBUG)
log_handler = MockLoggingHandler()
_logger.addHandler(log_handler)
_logger.messages = log_handler.messages
_logger.reset = lambda: log_handler.reset()
return _logger

## comment out one of the two functions...
def test_get_content_new_vcl_file(empty_config, logger, monkeypatch):
"""see if the header of a new _vcl_file is correct with the options
"""
# patching the language property
main = NatlinkMain(logger, empty_config)
main.config = empty_config
main.language = 'enx'
assert main.language == 'enx'
# main.language = 'nld'
# assert main.language == 'nld'

## after this setting and imports of voc_main, the setting is not affected any more...
## language is a property in main (loader.NatlinkMain) and natlinkstatus.NatlinkStatus() (linked to main).
## so first test with lines 59 and 60 commented, testing 'enx'.
## then uncomment these 2 lines, and test with 'nld', which stands for every language being not 'enx'

monkeypatch.setattr(main, "trigger_load", lambda: None)

from vocola2 import _vocola_main
voc_main = _vocola_main.ThisGrammar()

## True, True, different result for 'enx' and other languages:
monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: True)
monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: True)

after_text = voc_main.get_after_comment_new_vcl_file()
if main.language == 'enx':
assert after_text == '\ninclude Unimacro.vch;\n\n'
else:
assert after_text == ' (language: nld)\ninclude ..\\Unimacro.vch;\n\n'


## False, False, same for different languages as set above.
monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: False)
monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: False)
after_text = voc_main.get_after_comment_new_vcl_file()
assert after_text == '\n\n'

# same for different languages:
monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: True)
monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: False)
after_text = voc_main.get_after_comment_new_vcl_file()
assert after_text == '\ninclude Unimacro.vch;\n\n'

## different for 'enx' and other languages:
monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: False)
monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: True)
after_text = voc_main.get_after_comment_new_vcl_file()
if main.language == 'enx':
assert after_text == '\n\n'
else:
assert after_text == ' (language: nld)\n\n'









if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 6eda8a2

Please sign in to comment.