forked from cheahjs/palworld-save-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli_scripts.py
105 lines (102 loc) · 4.14 KB
/
test_cli_scripts.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
94
95
96
97
98
99
100
101
102
103
104
105
import contextlib
import os
import subprocess
import unittest
from parameterized import parameterized
class TestCliScripts(unittest.TestCase):
@parameterized.expand(
[
("Level.sav"),
("Level-tricky-unicode-player-name.sav"),
("LevelMeta.sav"),
("LocalData.sav"),
("WorldOption.sav"),
("00000000000000000000000000000001.sav"),
("unicode-saves/Level.sav"),
("unicode-saves/LevelMeta.sav"),
("unicode-saves/LocalData.sav"),
("unicode-saves/WorldOption.sav"),
("unicode-saves/00000000000000000000000000000001.sav"),
]
)
def test_sav_roundtrip(self, file_name):
try:
base_name = os.path.basename(file_name)
dir_name = os.path.dirname(file_name)
# Convert sav to JSON
run = subprocess.run(
[
"python3",
"convert.py",
f"tests/testdata/{dir_name}/{base_name}",
]
)
self.assertEqual(run.returncode, 0)
self.assertTrue(
os.path.exists(f"tests/testdata/{dir_name}/{base_name}.json")
)
# Convert JSON back to sav
os.rename(
f"tests/testdata/{dir_name}/{base_name}.json",
f"tests/testdata/{dir_name}/1-{base_name}.json",
)
run = subprocess.run(
[
"python3",
"convert.py",
f"tests/testdata/{dir_name}/1-{base_name}.json",
]
)
self.assertEqual(run.returncode, 0)
self.assertTrue(os.path.exists(f"tests/testdata/{dir_name}/1-{base_name}"))
# Reconvert sav back to JSON
os.rename(
f"tests/testdata/{dir_name}/1-{base_name}",
f"tests/testdata/{dir_name}/2-{base_name}",
)
run = subprocess.run(
[
"python3",
"convert.py",
f"tests/testdata/{dir_name}/2-{base_name}",
]
)
self.assertEqual(run.returncode, 0)
self.assertTrue(
os.path.exists(f"tests/testdata/{dir_name}/2-{base_name}.json")
)
# Reconvert JSON back to sav
os.rename(
f"tests/testdata/{dir_name}/2-{base_name}.json",
f"tests/testdata/{dir_name}/3-{base_name}.json",
)
run = subprocess.run(
[
"python3",
"convert.py",
f"tests/testdata/{dir_name}/3-{base_name}.json",
]
)
self.assertEqual(run.returncode, 0)
self.assertTrue(os.path.exists(f"tests/testdata/{dir_name}/3-{base_name}"))
# Compare the final sav to the intermediate save
with open(f"tests/testdata/{dir_name}/2-{base_name}", "rb") as f:
intermediate_data = f.read()
with open(f"tests/testdata/{dir_name}/3-{base_name}", "rb") as f:
final_data = f.read()
self.assertEqual(intermediate_data, final_data)
finally:
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/{base_name}.json")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/1-{base_name}")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/1-{base_name}.json")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/2-{base_name}")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/2-{base_name}.json")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/3-{base_name}")
with contextlib.suppress(FileNotFoundError):
os.remove(f"tests/testdata/{dir_name}/3-{base_name}.json")