Skip to content

Commit

Permalink
Fix tests when run as @rules_contest/...
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
nya3jp committed May 15, 2024
1 parent b993ad2 commit 4318509
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions tests/dataset_derive/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ py_test(
],
python_version = "PY3",
srcs_version = "PY3",
deps = ["//third_party/runfiles"],
)
10 changes: 9 additions & 1 deletion tests/dataset_derive/dataset_merge_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import unittest
import zipfile

from third_party.runfiles import runfiles

resolver = runfiles.Create()


class DatasetGenerateTest(unittest.TestCase):
def test_dataset1(self):
Expand All @@ -10,7 +14,11 @@ def test_dataset1(self):
'data2.in': b'2\n',
'data2.ans': b'22\n',
}
with zipfile.ZipFile('tests/dataset_derive/dataset1.zip') as zf:

zip_path = resolver.Rlocation(
'rules_contest/tests/dataset_derive/dataset1.zip')

with zipfile.ZipFile(zip_path) as zf:
self.assertEqual(sorted(zf.namelist()), sorted(expects))
for name, content in expects.items():
with zf.open(name, 'r') as f:
Expand Down
1 change: 1 addition & 0 deletions tests/dataset_generate/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ py_test(
],
python_version = "PY3",
srcs_version = "PY3",
deps = ["//third_party/runfiles"],
)
12 changes: 10 additions & 2 deletions tests/dataset_generate/dataset_generate_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import unittest
import zipfile

from third_party.runfiles import runfiles

resolver = runfiles.Create()


class DatasetGenerateTest(unittest.TestCase):
def test_dataset(self):
with zipfile.ZipFile('tests/dataset_generate/dataset.zip') as zf:
zip_path = resolver.Rlocation(
'rules_contest/tests/dataset_generate/dataset.zip')
with zipfile.ZipFile(zip_path) as zf:
self.assertEqual(
sorted(zf.namelist()),
['data1.in', 'data2.ans'])

def test_empty(self):
with zipfile.ZipFile('tests/dataset_generate/empty.zip') as zf:
zip_path = resolver.Rlocation(
'rules_contest/tests/dataset_generate/empty.zip')
with zipfile.ZipFile(zip_path) as zf:
self.assertEqual(zf.namelist(), [])


Expand Down
1 change: 1 addition & 0 deletions tests/dataset_merge/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ py_test(
],
python_version = "PY3",
srcs_version = "PY3",
deps = ["//third_party/runfiles"],
)
12 changes: 10 additions & 2 deletions tests/dataset_merge/dataset_merge_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import unittest
import zipfile

from third_party.runfiles import runfiles

resolver = runfiles.Create()


class DatasetMergeTest(unittest.TestCase):
def test_dataset(self):
with zipfile.ZipFile('tests/dataset_merge/dataset.zip') as zf:
zip_path = resolver.Rlocation(
'rules_contest/tests/dataset_merge/dataset.zip')
with zipfile.ZipFile(zip_path) as zf:
self.assertEqual(
sorted(zf.namelist()),
['data1.in', 'data2.in', 'data3.ans', 'data3.in'])

def test_empty(self):
with zipfile.ZipFile('tests/dataset_merge/empty.zip') as zf:
zip_path = resolver.Rlocation(
'rules_contest/tests/dataset_merge/empty.zip')
with zipfile.ZipFile(zip_path) as zf:
self.assertEqual(zf.namelist(), [])


Expand Down
6 changes: 5 additions & 1 deletion tests/interactive_judge/interactive_judge_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
solution_with_runfiles_path = resolver.Rlocation(
'rules_contest/tests/interactive_judge/solution_with_runfiles')

current_repository_root = os.environ['TEST_TARGET'].split('//')[0] + '//'


class InteractiveJudgeTest(unittest.TestCase):
def test_results(self):
Expand All @@ -35,7 +37,9 @@ def test_results(self):
self.assertEqual(sorted(os.listdir(out_dir)), sorted(output_files))

with open(os.path.join(out_dir, 'results.json')) as f:
results = json.load(f)
content = f.read()
content = content.replace(current_repository_root, '//')
results = json.loads(content)

# Fix undeterministic fields.
for case in results['cases']:
Expand Down
1 change: 1 addition & 0 deletions tests/render_test_results/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ py_test(
] + glob(["testdata/**"]),
python_version = "PY3",
srcs_version = "PY3",
deps = ["//third_party/runfiles"],
)
25 changes: 21 additions & 4 deletions tests/render_test_results/render_test_results_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import argparse
import os
import subprocess

from third_party.runfiles import runfiles

current_repository_root = os.environ['TEST_TARGET'].split('//')[0] + '//'


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--update', action='store_true')
options = parser.parse_args()

resolver = runfiles.Create()
bin_path = resolver.Rlocation('rules_contest/contest/render_test_results')
input_path = resolver.Rlocation(
'rules_contest/tests/render_test_results/testdata/build.jsonl')
golden_path = resolver.Rlocation(
'rules_contest/tests/render_test_results/golden.md')

output = subprocess.check_output(
['contest/render_test_results', 'tests/render_test_results/testdata/build.jsonl'])
[bin_path, input_path],
encoding='utf-8',
cwd=os.path.dirname(resolver.Rlocation('rules_contest/tests')),
)
output = output.replace(current_repository_root, '//')

if options.update:
with open('tests/render_test_results/golden.md', 'wb') as f:
with open(golden_path, 'w') as f:
f.write(output)
return

subprocess.run(
['diff', '-u', '/dev/stdin', 'tests/render_test_results/golden.md'],
input=output, check=True)
['diff', '-u', '/dev/stdin', golden_path],
input=output, encoding='utf-8', check=True)


if __name__ == '__main__':
Expand Down
6 changes: 5 additions & 1 deletion tests/simple_judge/simple_judge_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
solution_with_runfiles_path = resolver.Rlocation(
'rules_contest/tests/simple_judge/solution_with_runfiles')

current_repository_root = os.environ['TEST_TARGET'].split('//')[0] + '//'


class SimpleJudgeTest(unittest.TestCase):
maxDiff = None
Expand All @@ -43,7 +45,9 @@ def test_results(self):
self.assertEqual(sorted(os.listdir(out_dir)), sorted(output_files))

with open(os.path.join(out_dir, 'results.json')) as f:
results = json.load(f)
content = f.read()
content = content.replace(current_repository_root, '//')
results = json.loads(content)

# Fix undeterministic fields.
for case in results['cases']:
Expand Down

0 comments on commit 4318509

Please sign in to comment.