commit 909a60ec458da7e0aed557ddc3831dae0e025fe1 Author: Daniel Swanson Date: Tue Aug 10 16:45:37 2021 -0500 add option for subfolders of `test/` rather than flat (closes #2) diff --git a/apertium-regtest.py b/apertium-regtest.py index 7418af6..08562f2 100755 --- a/apertium-regtest.py +++ b/apertium-regtest.py @@ -144,6 +144,11 @@ def run_command(cmd, intxt, outfile, shell=False): stdout += ('\n[/%s]\n' % h).encode('utf-8') fout.write(stdout) +def ensure_dir_exists(name): + pth = os.path.join('test', name) + if not os.path.isdir(pth): + os.mkdir(pth) + class Step: prognames = { 'cg-proc': 'disam', @@ -219,11 +224,17 @@ class Mode: s.name += str(nm[s.name]) self.commands[s.name] = i Mode.all_modes[self.name] = self - def run(self, corpusname, filename, start=None): + def run(self, corpusname, filename, start=None, flat=True): + fname = 'test/' + if flat: + fname += '%s-%s-output.txt' + else: + ensure_dir_exists('output') + fname += 'output/%s-%s.txt' fin = filename idx = self.commands.get(start, 0) for i, step in enumerate(self.steps[idx:]): - fout = 'test/%s-%s-output.txt' % (corpusname, step.name) + fout = fname % (corpusname, step.name) step.run(fin, fout, first=(i == 0)) fin = fout def get_commands(self): @@ -299,6 +310,7 @@ def check_git(): sys.exit(1) class Corpus: + flat = True all_corpora = {} def __init__(self, name, blob): self.name = name @@ -340,7 +352,8 @@ class Corpus: def run(self): if self.mode: Mode.all_modes[self.mode].run(self.name, self.infile, - start=self.start_step) + start=self.start_step, + flat=Corpus.flat) else: txt = '' if self.infile: @@ -348,12 +361,23 @@ class Corpus: run_command(self.shell, txt, self.out_name('all'), shell=True) self.loaded = False def exp_name(self, cmd): - return 'test/%s-%s-expected.txt' % (self.name, cmd) + if Corpus.flat: + return 'test/%s-%s-expected.txt' % (self.name, cmd) + else: + return 'test/expected/%s-%s.txt' % (self.name, cmd) def out_name(self, cmd): - return 'test/%s-%s-output.txt' % (self.name, cmd) + if Corpus.flat: + return 'test/%s-%s-output.txt' % (self.name, cmd) + else: + return 'test/output/%s-%s.txt' % (self.name, cmd) def gold_name(self, cmd): - return 'test/%s-%s-gold.txt' % (self.name, cmd) + if Corpus.flat: + return 'test/%s-%s-gold.txt' % (self.name, cmd) + else: + return 'test/gold/%s-%s.txt' % (self.name, cmd) def save(self): + if not Corpus.flat: + ensure_dir_exists('expected') for blob in self.data['cmds']: if blob['cmd'] in self.unsaved: save_output(self.exp_name(blob['cmd']), blob['expect']) @@ -380,6 +404,8 @@ class Corpus: if os.path.isfile(expfile): expdata = load_output(expfile) else: + if not Corpus.flat: + ensure_dir_exists('expected') save_output(expfile, outdata) expdata = outdata golddata = {} @@ -514,6 +540,8 @@ class Corpus: def set_gold(self, hsh, vals, step=None): blob = self.step(step) blob['gold'][hsh] = vals + if not Corpus.flat: + ensure_dir_exists('gold') save_gold(self.gold_name(blob['cmd']), blob['gold']) def load_corpora(names, static=False): @@ -535,6 +563,9 @@ def load_corpora(names, static=False): try: blob = json.load(ts) for k in blob: + if k == 'settings': + Corpus.flat = (blob[k].get('structure', 'flat') != 'nested') + continue for p in pats: if p.search(k): Corpus(k, blob[k]) diff --git a/tools/flat2nest.py b/tools/flat2nest.py new file mode 100755 index 0000000..fd7e4e5 --- /dev/null +++ b/tools/flat2nest.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +import argparse +import json +import subprocess +import sys +import os + +parser = argparse.ArgumentParser('convert flat apertium-regtest directory to one with subfolders') +args = parser.parse_args() + +if not os.path.isdir('test'): + print('test/ not found.') + print('Please run this script from the top level of an Apertium directory.') + sys.exit(1) + +os.mkdir('test/output') +os.mkdir('test/gold') +os.mkdir('test/expected') + +replace = { + '-output.txt': 'test/output/', + '-gold.txt': 'test/gold/', + '-expected.txt': 'test/expected/' +} + +for fname in os.listdir('test'): + for old, new in replace.items(): + if fname.endswith(old): + os.rename('test/'+fname, new+fname[:-len(old)]+'.txt') + +tests = {} +if os.path.isfile('test/tests.json'): + with open('test/tests.json') as fin: + tests = json.loads(fin.read()) +tests['settings'] = {'structure': 'nested'} +with open('test/tests.json', 'w') as js: + js.write(json.dumps(tests, indent=4) + '\n') + +txt = '' +end = '\n' +if os.path.isfile('.gitignore'): + with open('.gitignore') as fin: + txt = fin.read() + if '\r\n' in txt: + end = '\r\n' + if not txt.endswith(end): + txt += end +txt += '/test/output' + end +with open('.gitignore', 'w') as fout: + fout.write(txt) + +subprocess.run(['git', 'add', '.gitignore', 'test/tests.json']) +subprocess.run(['git', 'add', 'test'])