Skip to content

Commit 94fe1d2

Browse files
committed
Convert wrapper test binary to Python.
This removes a dependency on the non-builtin Go rules.
1 parent db0ae2b commit 94fe1d2

File tree

5 files changed

+111
-142
lines changed

5 files changed

+111
-142
lines changed

elisp/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ py_library(
348348
name = "runfiles",
349349
srcs = ["runfiles.py"],
350350
srcs_version = "PY3",
351-
visibility = ["//emacs:__pkg__"],
351+
visibility = [
352+
"//emacs:__pkg__",
353+
"//tests/wrap:__pkg__",
354+
],
352355
deps = ["@bazel_tools//tools/python/runfiles"],
353356
)
354357

elisp/binary_test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ using ::testing::Eq;
3030
TEST(Executor, RunBinaryWrap) {
3131
const absl::StatusOr<Runfiles> runfiles = Runfiles::CreateForTest();
3232
ASSERT_TRUE(runfiles.ok()) << runfiles.status();
33+
NativeString wrapper =
34+
PHST_RULES_ELISP_NATIVE_LITERAL("phst_rules_elisp/tests/wrap/wrap");
35+
#ifdef PHST_RULES_ELISP_WINDOWS
36+
wrapper += L".exe";
37+
#endif
3338
const NativeString argv0 = PHST_RULES_ELISP_NATIVE_LITERAL("unused");
3439
const absl::StatusOr<NativeString> input_file =
3540
runfiles->Resolve("phst_rules_elisp/elisp/binary.cc");
3641
ASSERT_TRUE(input_file.ok()) << input_file.status();
3742
const std::vector<NativeString> args = {
38-
PHST_RULES_ELISP_NATIVE_LITERAL(
39-
"--wrapper=phst_rules_elisp/tests/wrap/wrap"),
43+
PHST_RULES_ELISP_NATIVE_LITERAL("--wrapper=") + wrapper,
4044
PHST_RULES_ELISP_NATIVE_LITERAL("--mode=wrap"),
4145
PHST_RULES_ELISP_NATIVE_LITERAL("--rule-tag=local"),
4246
PHST_RULES_ELISP_NATIVE_LITERAL("--rule-tag=mytag"),

tests/wrap/BUILD

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
16-
17-
go_library(
18-
name = "go_default_library",
19-
testonly = 1,
20-
srcs = ["main.go"],
21-
importpath = "github.com/phst/rules_elisp/tests/wrap",
22-
visibility = ["//visibility:private"],
23-
deps = [
24-
"@com_github_google_go_cmp//cmp:go_default_library",
25-
"@com_github_phst_runfiles//:go_default_library",
26-
],
27-
)
28-
29-
go_binary(
15+
py_binary(
3016
name = "wrap",
3117
testonly = 1,
32-
out = "wrap",
33-
embed = [":go_default_library"],
18+
srcs = ["wrap.py"],
19+
python_version = "PY3",
20+
srcs_version = "PY3",
3421
visibility = ["//elisp:__pkg__"],
22+
deps = ["//elisp:runfiles"],
3523
)

tests/wrap/main.go

Lines changed: 0 additions & 122 deletions
This file was deleted.

tests/wrap/wrap.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright 2020, 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Binary wrap is a test helper program for //elisp:binary_test, which see."""
16+
17+
import argparse
18+
import json
19+
import os
20+
import pathlib
21+
import unittest
22+
import sys
23+
from typing import Any, Dict, List
24+
25+
from phst_rules_elisp.elisp import runfiles
26+
27+
def _main() -> None:
28+
print('Args:', sys.argv)
29+
print('Environment:', os.environ)
30+
parser = argparse.ArgumentParser(allow_abbrev=False)
31+
parser.add_argument('--manifest', type=pathlib.Path, required=True)
32+
parser.add_argument('rest', nargs='+')
33+
args = parser.parse_args()
34+
run_files = runfiles.Runfiles()
35+
output_file = pathlib.PurePath(
36+
r'C:\Temp\output.dat' if os.name == 'nt' else '/tmp/output.dat')
37+
38+
class Test(unittest.TestCase):
39+
"""Unit tests for the command line and manifest."""
40+
41+
maxDiff = 5000
42+
43+
def test_args(self) -> None:
44+
"""Test that the Emacs command line is as expected."""
45+
got = args.rest
46+
want = ['--quick', '--batch']
47+
# The load path setup depends on whether we use manifest-based or
48+
# directory-based runfiles.
49+
try:
50+
directory = run_files.resolve(
51+
pathlib.PurePosixPath('phst_rules_elisp'))
52+
except FileNotFoundError:
53+
# Manifest-based runfiles.
54+
want += [
55+
'--load=' + str(run_files.resolve(pathlib.PurePosixPath(
56+
'phst_rules_elisp/elisp/runfiles/runfiles.elc'))),
57+
'--funcall=elisp/runfiles/install-handler',
58+
'--directory=/bazel-runfile:phst_rules_elisp',
59+
]
60+
else:
61+
# Directory-based runfiles.
62+
want.append('--directory=' + str(directory))
63+
want += [
64+
'--option',
65+
str(run_files.resolve(pathlib.PurePosixPath(
66+
'phst_rules_elisp/elisp/binary.cc'))),
67+
' \t\n\r\f äα𝐴🐈\'\\"',
68+
'/:' + str(output_file),
69+
]
70+
self.assertListEqual(got, want)
71+
72+
def test_manifest(self) -> None:
73+
"""Test the manifest."""
74+
got = json.loads(args.manifest.read_text(encoding='utf-8'))
75+
want = {
76+
'root': 'RUNFILES_ROOT',
77+
'tags': ['local', 'mytag'],
78+
'loadPath': ['phst_rules_elisp'],
79+
'inputFiles': ['phst_rules_elisp/elisp/binary.cc',
80+
'phst_rules_elisp/elisp/binary.h'],
81+
'outputFiles': [str(output_file)],
82+
} # type: Dict[str, Any]
83+
for var in (got, want):
84+
files = var.get('inputFiles', []) # type: List[str]
85+
for i, file in enumerate(files):
86+
file = pathlib.PurePosixPath(file)
87+
if not file.is_absolute():
88+
files[i] = str(run_files.resolve(file))
89+
self.assertDictEqual(got, want)
90+
91+
tests = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
92+
if not unittest.TextTestRunner().run(tests).wasSuccessful():
93+
raise ValueError('incorrect arguments')
94+
95+
if __name__ == '__main__':
96+
_main()

0 commit comments

Comments
 (0)