Skip to content

Commit

Permalink
Convert wrapper test binary to Python.
Browse files Browse the repository at this point in the history
This removes a dependency on the non-builtin Go rules.
  • Loading branch information
phst committed Nov 30, 2021
1 parent 3af60ba commit bfa3716
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 143 deletions.
5 changes: 4 additions & 1 deletion elisp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ py_library(
name = "runfiles",
srcs = ["runfiles.py"],
srcs_version = "PY3",
visibility = ["//emacs:__pkg__"],
visibility = [
"//emacs:__pkg__",
"//tests/wrap:__pkg__",
],
deps = ["@bazel_tools//tools/python/runfiles"],
)

Expand Down
25 changes: 5 additions & 20 deletions tests/wrap/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "go_default_library",
testonly = 1,
srcs = ["main.go"],
importpath = "github.com/phst/rules_elisp/tests/wrap",
visibility = ["//visibility:private"],
deps = [
"@com_github_google_go_cmp//cmp:go_default_library",
"@com_github_phst_runfiles//:go_default_library",
],
)

go_binary(
py_binary(
name = "wrap",
testonly = 1,
out = select({
"@platforms//os:windows": "wrap.exe",
"//conditions:default": "wrap",
}),
embed = [":go_default_library"],
srcs = ["wrap.py"],
python_version = "PY3",
srcs_version = "PY3",
visibility = ["//elisp:__pkg__"],
deps = ["//elisp:runfiles"],
)
122 changes: 0 additions & 122 deletions tests/wrap/main.go

This file was deleted.

96 changes: 96 additions & 0 deletions tests/wrap/wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2020, 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Binary wrap is a test helper program for //elisp:binary_test, which see."""

import argparse
import json
import os
import pathlib
import unittest
import sys
from typing import Any, Dict, List

from phst_rules_elisp.elisp import runfiles

def _main() -> None:
print('Args:', sys.argv)
print('Environment:', os.environ)
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument('--manifest', type=pathlib.Path, required=True)
parser.add_argument('rest', nargs='+')
args = parser.parse_args()
run_files = runfiles.Runfiles()
output_file = pathlib.PurePath(
r'C:\Temp\output.dat' if os.name == 'nt' else '/tmp/output.dat')

class Test(unittest.TestCase):
"""Unit tests for the command line and manifest."""

maxDiff = 5000

def test_args(self) -> None:
"""Test that the Emacs command line is as expected."""
got = args.rest
want = ['--quick', '--batch']
# The load path setup depends on whether we use manifest-based or
# directory-based runfiles.
try:
directory = run_files.resolve(
pathlib.PurePosixPath('phst_rules_elisp'))
except FileNotFoundError:
# Manifest-based runfiles.
want += [
'--load=' + str(run_files.resolve(pathlib.PurePosixPath(
'phst_rules_elisp/elisp/runfiles/runfiles.elc'))),
'--funcall=elisp/runfiles/install-handler',
'--directory=/bazel-runfile:phst_rules_elisp',
]
else:
# Directory-based runfiles.
want.append('--directory=' + str(directory))
want += [
'--option',
str(run_files.resolve(pathlib.PurePosixPath(
'phst_rules_elisp/elisp/binary.cc'))),
' \t\n\r\f äα𝐴🐈\'\\"',
'/:' + str(output_file),
]
self.assertListEqual(got, want)

def test_manifest(self) -> None:
"""Test the manifest."""
got = json.loads(args.manifest.read_text(encoding='utf-8'))
want = {
'root': 'RUNFILES_ROOT',
'tags': ['local', 'mytag'],
'loadPath': ['phst_rules_elisp'],
'inputFiles': ['phst_rules_elisp/elisp/binary.cc',
'phst_rules_elisp/elisp/binary.h'],
'outputFiles': [str(output_file)],
} # type: Dict[str, Any]
for var in (got, want):
files = var.get('inputFiles', []) # type: List[str]
for i, file in enumerate(files):
file = pathlib.PurePosixPath(file)
if not file.is_absolute():
files[i] = str(run_files.resolve(file))
self.assertDictEqual(got, want)

tests = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
if not unittest.TextTestRunner().run(tests).wasSuccessful():
raise ValueError('incorrect arguments')

if __name__ == '__main__':
_main()

0 comments on commit bfa3716

Please sign in to comment.