-
Notifications
You must be signed in to change notification settings - Fork 13
/
integration_test.py
executable file
·93 lines (72 loc) · 2.51 KB
/
integration_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/local/env python
#
# Copyright 2017 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
#
# http://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.
from os import path
import difflib
import os
import subprocess
import sys
def check_proto_file(filename):
assert isinstance(filename, str) and filename.endswith(".proto"), filename
return filename
def check_rules_file(filename):
assert isinstance(filename, str) and filename.endswith(".rules"), filename
return filename
def read_file(filename):
with open(filename, "r") as f:
return f.read()
def check_rules_output(test_file, expected, actual):
if expected != actual:
for line in difflib.unified_diff(
expected.splitlines(True),
actual.splitlines(True),
fromfile=test_file,
tofile="firestore.rules"):
sys.stderr.write(line)
sys.exit(1)
output_dir = os.environ["TEST_UNDECLARED_OUTPUTS_DIR"]
rules_plugin, protoc = sys.argv[1], sys.argv[2]
firebase_protos = path.dirname(sys.argv[3])
google_protos = path.dirname(sys.argv[4])
example_rules = sys.argv[5]
example_golden = sys.argv[6]
testdata = sys.argv[7:]
def run_testcase(proto_file, output):
test_protos = path.dirname(proto_file)
test_file = path.basename(proto_file)
subprocess.check_call(
[
protoc,
"--plugin=protoc-gen-firebase_rules=" + rules_plugin,
"--firebase_rules_out=" + output_dir,
"--proto_path=" + google_protos,
"--proto_path=" + firebase_protos,
"--proto_path=" + test_protos,
test_file,
],
stderr=sys.stderr)
actual = read_file(output_dir + "/firestore.rules")
expected = read_file(output)
check_rules_output(path.basename(output), expected, actual)
# Testcases should be <name>.proto as the input and <name>.rules as the output.
testdata.sort()
for i in xrange(0, len(testdata), 2):
proto_file = check_proto_file(testdata[i])
rules_out = check_rules_file(testdata[i + 1])
run_testcase(proto_file, rules_out)
check_rules_output(
'example.rules',
read_file(example_golden),
read_file(example_rules))