Repository navigation
Expand file tree
/
Copy pathtest_import_selected_using_hardlink.py
More file actions
250 lines (219 loc) · 8.24 KB
/
Copy pathtest_import_selected_using_hardlink.py
File metadata and controls
250 lines (219 loc) · 8.24 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""Track 2 module pins: selected imports, using-import reject, hardlink identity."""
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
def run_cli(args: list[str]) -> subprocess.CompletedProcess[str]:
env = dict(os.environ, PYTHONPATH=str(ROOT))
return subprocess.run(
[sys.executable, str(ROOT / "main.py"), *args],
cwd=ROOT,
capture_output=True,
text=True,
env=env,
)
def test_selected_import_file_module_compiles(tmp_path):
(tmp_path / "vector.a7").write_text(
"Vec3 :: struct { x: f32, y: f32, z: f32 }\n"
"dot :: fn(a: Vec3, b: Vec3) f32 {\n"
" ret a.x * b.x + a.y * b.y + a.z * b.z\n"
"}\n",
encoding="utf-8",
)
main = tmp_path / "main.a7"
main.write_text(
'io :: import "std/io"\n'
'import "vector" { Vec3, dot }\n'
"main :: fn() {\n"
" a := Vec3{x: 1.0, y: 2.0, z: 3.0}\n"
" b := Vec3{x: 1.0, y: 0.0, z: 0.0}\n"
' io.println("{}", dot(a, b))\n'
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
compiled = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert compiled.returncode == 0, compiled.stdout + compiled.stderr
emitted = out.read_text()
assert "module_vector__dot" in emitted or "dot" in emitted
assert "module_vector__Vec3" in emitted or "Vec3" in emitted
def test_selected_import_stdlib_println(tmp_path):
main = tmp_path / "main.a7"
main.write_text(
'import "std/io" { println }\n'
"main :: fn() {\n"
' println("hi")\n'
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
result = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert result.returncode == 0, result.stdout + result.stderr
assert "hi" in out.read_text() or "__a7_stdout_print" in out.read_text()
def test_selected_import_missing_name_exits_6(tmp_path):
(tmp_path / "helper.a7").write_text("value :: fn() i32 { ret 1 }\n", encoding="utf-8")
main = tmp_path / "main.a7"
main.write_text(
'import "helper" { missing }\n'
"main :: fn() {}\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
assert "missing" in (result.stdout + result.stderr).lower()
def test_selected_import_private_name_exits_6(tmp_path):
(tmp_path / "helper.a7").write_text("_secret :: fn() i32 { ret 1 }\n", encoding="utf-8")
main = tmp_path / "main.a7"
main.write_text(
'import "helper" { _secret }\n'
"main :: fn() {}\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
assert "private" in (result.stdout + result.stderr).lower()
def test_using_import_exits_6_with_alias_guidance(tmp_path):
main = tmp_path / "main.a7"
main.write_text(
'using import "std/io"\n'
"main :: fn() {}\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
text = result.stdout + result.stderr
assert "using import is not supported" in text
assert 'alias :: import' in text
def test_selected_import_stdlib_ioerr_compiles(tmp_path):
main = tmp_path / "main.a7"
main.write_text(
'import "std/io" { IoErr, println_ok }\n'
"main :: fn() {\n"
" r: Result(usize, IoErr) = println_ok(\"ok\")\n"
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
result = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert result.returncode == 0, result.stdout + result.stderr
emitted = out.read_text()
assert "IoErr" in emitted
def test_selected_import_module_alias_exits_6(tmp_path):
(tmp_path / "leaf.a7").write_text("foo :: fn() i32 { ret 1 }\n", encoding="utf-8")
(tmp_path / "mid.a7").write_text('l :: import "leaf"\n', encoding="utf-8")
main = tmp_path / "main.a7"
main.write_text(
'import "mid" { l }\n'
"main :: fn() { x := l.foo() }\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
text = (result.stdout + result.stderr).lower()
assert "import alias" in text or "cannot be selected" in text
def test_selected_stdlib_op_as_value_exits_6(tmp_path):
main = tmp_path / "main.a7"
main.write_text(
'import "std/io" { println }\n'
"main :: fn() {\n"
" f := println\n"
' f("hello")\n'
"}\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
assert "function values are unavailable" in (result.stdout + result.stderr)
def test_selected_read_line_uses_fixed_signature(tmp_path):
main = tmp_path / "main.a7"
main.write_text(
'import "std/io" { read_line }\n'
"main :: fn() {\n"
" buf: [8]u8\n"
" _ := read_line(buf[0..])\n"
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
ok = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert ok.returncode == 0, ok.stdout + ok.stderr
bad = tmp_path / "bad.a7"
bad.write_text(
'import "std/io" { read_line }\n'
'main :: fn() { _ := read_line("hello") }\n',
encoding="utf-8",
)
rejected = run_cli(["--mode", "semantic", str(bad)])
assert rejected.returncode == 6, rejected.stdout + rejected.stderr
assert "[]u8" in (rejected.stdout + rejected.stderr)
def test_selected_constant_in_generic_type_arg_lowers(tmp_path):
(tmp_path / "lib.a7").write_text("SIZE :: 3\n", encoding="utf-8")
main = tmp_path / "main.a7"
main.write_text(
'import "lib" { SIZE }\n'
"Buf($N: usize) :: struct { data: [$N]u8 }\n"
"main :: fn() {\n"
" b: Buf(SIZE)\n"
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
result = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert result.returncode == 0, result.stdout + result.stderr
emitted = out.read_text()
assert "Buf(3)" in emitted
assert "Buf(SIZE)" not in emitted
def test_hardlink_duplicate_import_exits_6(tmp_path):
helper = tmp_path / "helper.a7"
helper.write_text("pub value :: fn() i32 { ret 7 }\n", encoding="utf-8")
link = tmp_path / "link.a7"
try:
os.link(helper, link)
except OSError:
pytest.skip("cannot create hardlink")
main = tmp_path / "main.a7"
main.write_text(
'h :: import "helper"\n'
'l :: import "link"\n'
"main :: fn() {}\n",
encoding="utf-8",
)
result = run_cli(["--mode", "semantic", str(main)])
assert result.returncode == 6, result.stdout + result.stderr
assert "uplicate import" in result.stdout + result.stderr
def test_hardlink_diamond_shares_one_module(tmp_path):
shared = tmp_path / "shared.a7"
shared.write_text("pub value :: fn() i32 { ret 3 }\n", encoding="utf-8")
(tmp_path / "left").mkdir()
(tmp_path / "right").mkdir()
(tmp_path / "left" / "part.a7").write_text(
'shared :: import "../shared"\n'
"pub left_value :: fn() i32 { ret shared.value() }\n",
encoding="utf-8",
)
link = tmp_path / "right" / "alias.a7"
try:
os.link(shared, link)
except OSError:
pytest.skip("cannot create hardlink")
(tmp_path / "right" / "part.a7").write_text(
'shared :: import "alias"\n'
"pub right_value :: fn() i32 { ret shared.value() * 2 }\n",
encoding="utf-8",
)
main = tmp_path / "main.a7"
main.write_text(
'l :: import "left/part"\n'
'r :: import "right/part"\n'
"main :: fn() {\n"
" x := l.left_value()\n"
" y := r.right_value()\n"
"}\n",
encoding="utf-8",
)
out = tmp_path / "main.zig"
result = run_cli(["--mode", "compile", "--output", str(out), str(main)])
assert result.returncode == 0, result.stdout + result.stderr