forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-emoji-name-scripts
executable file
·86 lines (74 loc) · 3.46 KB
/
test-emoji-name-scripts
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
#!/usr/bin/env python3
import argparse
import difflib
import filecmp
import os
import subprocess
import shutil
import tempfile
TOOLS_DIR = os.path.abspath(os.path.dirname(__file__))
def generate_files(source_file: str, tmp_dir: str) -> None:
# Copy the source CSV file to out temporary test directory.
input_file_path = source_file
output_file_path = os.path.join(tmp_dir, "CSV_A")
shutil.copyfile(input_file_path, output_file_path)
# Generate the name map file in the temporary directory.
input_file_path = output_file_path
output_file_path = os.path.join(tmp_dir, 'NAME_MAP_A')
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'import_emoji_names_from_csv'),
'--input-file', input_file_path, '--output-file', output_file_path],
stdout=subprocess.DEVNULL)
# Regenerate the CSV file from name map.
input_file_path = output_file_path
output_file_path = os.path.join(tmp_dir, 'CSV_B')
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'export_emoji_names_to_csv'),
'--input-file', input_file_path, '--output-file', output_file_path],
stdout=subprocess.DEVNULL)
# Regenerate the name map file from the regenerated CSV file.
input_file_path = output_file_path
output_file_path = os.path.join(tmp_dir, 'NAME_MAP_B')
subprocess.check_call([os.path.join(TOOLS_DIR, 'setup', 'emoji', 'import_emoji_names_from_csv'),
'--input-file', input_file_path, '--output-file', output_file_path],
stdout=subprocess.DEVNULL)
def print_diff(path_file1, path_file2):
# type: (str, str) -> None
with open(path_file1) as file1:
with open(path_file2) as file2:
diff = difflib.unified_diff(
file1.readlines(),
file2.readlines(),
fromfile=path_file1,
tofile=path_file2,
)
for line in diff:
print(line)
def compare_files(first_file: str, second_file: str) -> None:
same = True
same = same and filecmp.cmp(first_file, second_file, shallow=False)
if not same:
print_diff(first_file, second_file)
raise Exception("Round trip conversion failed!!")
def check_files(tmp_dir: str) -> None:
# Compare the original and regenerated CSV files.
first_file = os.path.join(tmp_dir, 'CSV_A')
second_file = os.path.join(tmp_dir, 'CSV_B')
compare_files(first_file, second_file)
# Compare the name map files.
first_file = os.path.join(tmp_dir, 'NAME_MAP_A')
second_file = os.path.join(tmp_dir, 'NAME_MAP_B')
compare_files(first_file, second_file)
def main() -> None:
description = ("This tool is used test the emoji tooling that we use to import/export "
"naming related data. This works by doing a round-trip conversion of "
"and then verifying that no changes were made in the process.")
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"--input-file", dest="input_file_path", type=str, metavar="<path>",
default=os.path.join(TOOLS_DIR, "setup", "emoji", "emoji_names.csv"),
help="Path to the CSV file to be used for round-trip conversion.")
args = parser.parse_args()
with tempfile.TemporaryDirectory() as tmp_dir:
generate_files(args.input_file_path, tmp_dir)
check_files(tmp_dir)
if __name__ == "__main__":
main()