Commit d4e21b3e authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

testing.py: replaced TFFN() by unique_name()

parent ac2a9952
......@@ -59,7 +59,7 @@ __all__ = ('unit_from_config',
'TEST_ARTIFACT',
'POSSIBLE_ARTIFACTS',
'grammar_unit',
'TFFN',
'unique_name',
'grammar_suite',
'SymbolsDictType',
'extract_symbols',
......@@ -577,18 +577,16 @@ def reset_unit(test_unit):
del tests[key]
def TFFN(file_name: str) -> str:
"""Thread Safe File Name: Returns a thread safe-version of a file
name by adding a unique process and thread identificator.
Thread-Safe filenames are needed when running test that use the same
tempory file names in parralel or when running the same test-suite
under different constellations, say different Python-interpreters,
in parallel. Otherwise it can happen that one thread cleans up a
temporary file that an other thread has created.
def unique_name(file_name: str) -> str:
"""Turns the file or dirname into a unique name by adding a time stamp.
This helps to avoid race conditions when running tests in parallel
that create and delete files on the disk.
"""
return concurrent_ident() + '_' + file_name
# return concurrent_ident() + '_' + file_name
resolution = 100000
name = 'unique_' + str(int(time.time() * resolution)) + '_' + file_name
time.sleep(1.0 / resolution)
return name
def grammar_suite(directory, parser_factory, transformer_factory,
fn_patterns=('*test*',),
......
......@@ -53,7 +53,7 @@ from DHParser.server import Server, spawn_tcp_server, stop_tcp_server, asyncio_r
split_header, has_server_stopped, STOP_SERVER_REQUEST_BYTES, IDENTIFY_REQUEST, \
SERVER_OFFLINE, connection_cb_dummy
from DHParser.lsp import gen_lsp_table
from DHParser.testing import TFFN
from DHParser.testing import unique_name
TEST_PORT = 8000 + os.getpid() % 1000
# print('>>> ', sys.version, TEST_PORT)
......
......@@ -31,7 +31,7 @@ scriptpath = os.path.dirname(__file__) or '.'
sys.path.append(os.path.abspath(os.path.join(scriptpath, '..')))
LOG_DIR = os.path.abspath(os.path.join(scriptpath, "LOGS"))
from DHParser.testing import TFFN
from DHParser.testing import unique_name
def system(s: str) -> int:
......@@ -47,7 +47,7 @@ class TestDHParserCommandLineTool:
counter = 10
while counter > 0:
try:
self.dirname = TFFN('test_dhparser_data')
self.dirname = unique_name('test_dhparser_data')
os.mkdir(self.dirname)
counter = 0
except FileExistsError:
......
......@@ -38,7 +38,7 @@ from DHParser.preprocess import make_token, tokenized_to_original_mapping, sourc
BEGIN_TOKEN, END_TOKEN, TOKEN_DELIMITER, SourceMapFunc, SourceMap, chain_preprocessors, \
strip_tokens, gen_find_include_func, preprocess_includes, IncludeInfo
from DHParser.toolkit import lstrip_docstring, typing, re
from DHParser.testing import TFFN
from DHParser.testing import unique_name
from typing import Tuple, Dict
......@@ -251,8 +251,8 @@ class TestIncludes:
counter = 10
while counter > 0:
try:
self.dirname = TFFN('test_preprocess_data')
os.mkdir(TFFN('test_preprocess_data'))
self.dirname = unique_name('test_preprocess_data')
os.mkdir(self.dirname)
counter = 0
except FileExistsError:
time.sleep(0.5)
......
......@@ -34,7 +34,7 @@ from DHParser.dsl import grammar_provider
from DHParser.error import Error, PARSER_LOOKAHEAD_FAILURE_ONLY, PARSER_LOOKAHEAD_MATCH_ONLY, \
MANDATORY_CONTINUATION_AT_EOF, ERROR, WARNING
from DHParser.testing import get_report, grammar_unit, unit_from_file, \
clean_report, TFFN
clean_report, unique_name
from DHParser.trace import set_tracer, trace_history
......@@ -104,24 +104,28 @@ class TestTestfiles:
def setup(self):
self.save_dir = os.getcwd()
os.chdir(scriptpath)
with open(TFFN('configfile_test_1.ini'), 'w', encoding="utf-8") as f:
self.cfg1 = unique_name('configfile_test_1.ini')
self.cfg2 = unique_name('configfile_test_2.ini')
self.cfg3 = unique_name('configfile_test_3.ini')
self.cfg4 = unique_name('configfile_test_4.ini')
with open(self.cfg1, 'w', encoding="utf-8") as f:
f.write(CFG_FILE_1)
with open(TFFN('configfile_test_2.ini'), 'w', encoding="utf-8") as f:
with open(self.cfg2, 'w', encoding="utf-8") as f:
f.write(CFG_FILE_2)
with open(TFFN('configfile_test_3.ini'), 'w', encoding="utf-8") as f:
with open(self.cfg3, 'w', encoding="utf-8") as f:
f.write(CFG_FILE_3)
with open(TFFN('configfile_test_4.ini'), 'w', encoding="utf-8") as f:
with open(self.cfg4, 'w', encoding="utf-8") as f:
f.write(CFG_FILE_4)
def teardown(self):
os.remove(TFFN('configfile_test_1.ini'))
os.remove(TFFN('configfile_test_2.ini'))
os.remove(TFFN('configfile_test_3.ini'))
os.remove(TFFN('configfile_test_4.ini'))
os.remove(self.cfg1)
os.remove(self.cfg2)
os.remove(self.cfg3)
os.remove(self.cfg4)
os.chdir(self.save_dir)
def test_unit_from_config_file(self):
unit = unit_from_file(TFFN('configfile_test_1.ini'))
unit = unit_from_file(self.cfg1)
assert list(unit.keys()) == ['ParserA']
assert list(unit['ParserA'].keys()) == ['match', 'fail'], str(list(unit['ParserA'].keys()))
assert list(unit['ParserA']['match'].keys()) == ['M1', 'M2', 'M3', 'M4']
......@@ -130,16 +134,16 @@ class TestTestfiles:
lines = testcase.split('\n')
assert len(lines[2]) - len(lines[2].lstrip()) == 4
unit = unit_from_file(TFFN('configfile_test_2.ini'))
unit = unit_from_file(self.cfg2)
txt = unit['BedeutungsPosition']['match']['M1']
txt.split('\n')
for line in txt:
assert line.rstrip()[0:1] != ' '
unit = unit_from_file(TFFN('configfile_test_3.ini'))
unit = unit_from_file(self.cfg3)
try:
unit = unit_from_file(TFFN('configfile_test_4.ini'))
unit = unit_from_file(self.cfg4)
assert False, "Same key used twice should raise a key error!!!"
except KeyError as e:
pass
......
......@@ -35,7 +35,7 @@ from DHParser import grammar_provider, all_descendants, \
set_config_value, resume_notices_on, Error
from DHParser.error import MANDATORY_CONTINUATION, PARSER_STOPPED_BEFORE_END, \
MANDATORY_CONTINUATION_AT_EOF, WARNING
from DHParser.testing import TFFN
from DHParser.testing import unique_name
def get_history(name, show: bool = REVEAL) -> str:
......@@ -210,7 +210,7 @@ class TestErrorReporting:
gr = grammar_provider(lang)()
def setup(self):
start_logging(TFFN("LOGS"))
start_logging(unique_name("LOGS"))
def teardown(self):
LOG_DIR = log_dir()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment