Repository navigation
Expand file tree
/
Copy pathmodules.rs
More file actions
376 lines (358 loc) · 12.7 KB
/
Copy pathmodules.rs
File metadata and controls
376 lines (358 loc) · 12.7 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use morphir_extension_sdk::prelude::*;
use morphir_python_binding::PythonExtension;
use serde_json::{Value, json};
fn request(sources: &[(&str, &str)]) -> CompileRequest {
CompileRequest {
language_id: "python".into(),
sources: SourceSet {
root: None,
documents: sources
.iter()
.map(|(uri, text)| SourceDocument {
uri: (*uri).into(),
language_id: "python".into(),
version: 1,
text: (*text).into(),
})
.collect(),
},
package: CompilePackage {
name: "acme/example".into(),
exposed_modules: None,
},
dependencies: vec![],
options: CompileOptions {
ir_version: "4".into(),
types_only: false,
..Default::default()
},
baseline: None,
}
}
fn compile(request: CompileRequest) -> Value {
let result = PythonExtension.compile(request).unwrap();
assert!(result.success, "{:?}", result.diagnostics);
result.ir.unwrap()
}
fn roundtrip(request: CompileRequest) -> (Value, Vec<Artifact>) {
let ir = compile(request.clone());
let mut reversed = request;
reversed.sources.documents.reverse();
assert_eq!(
ir,
compile(reversed),
"document order must not affect resolution"
);
let generated = PythonExtension
.generate(GenerateRequest {
ir: ir.clone(),
target: "python".into(),
options: Default::default(),
})
.unwrap();
assert!(generated.success, "{:?}", generated.diagnostics);
let sources: Vec<_> = generated
.artifacts
.iter()
.map(|a| (a.path.as_str(), a.content.as_str()))
.collect();
assert_eq!(ir, compile(self::request(&sources)));
(ir, generated.artifacts)
}
const TYPES: &str = "from dataclasses import dataclass\n@dataclass(frozen=True)\nclass Address:\n zip_code: int\ntype Pair = tuple[int, str]\n";
#[test]
fn private_modules_are_available_inside_the_package_and_survive_generation() {
let mut input = request(&[
("internal/models.py", TYPES),
(
"api.py",
"from internal.models import Pair\ndef origin() -> Pair:\n return (1, 'one')\n",
),
]);
input.package.exposed_modules = Some(vec!["api".into()]);
let ir = compile(input.clone());
assert!(
ir["distribution"]["Library"]["def"]["modules"]["internal/models"]["Private"].is_object()
);
assert!(ir["distribution"]["Library"]["def"]["modules"]["api"]["Public"].is_object());
let generated = PythonExtension
.generate(GenerateRequest {
ir: ir.clone(),
target: "python".into(),
options: Default::default(),
})
.unwrap();
assert!(generated.success, "{:?}", generated.diagnostics);
input.sources.documents = generated
.artifacts
.iter()
.map(|artifact| SourceDocument {
uri: artifact.path.clone(),
text: artifact.content.clone(),
language_id: "python".into(),
version: 1,
})
.collect();
assert_eq!(ir, compile(input));
}
#[test]
fn explicitly_empty_exposure_makes_every_module_private() {
let mut input = request(&[("models.py", TYPES)]);
input.package.exposed_modules = Some(vec![]);
let ir = compile(input);
assert!(ir["distribution"]["Library"]["def"]["modules"]["models"]["Private"].is_object());
}
#[test]
fn imported_types_and_tuple_aliases_roundtrip_across_modules() {
let (ir, artifacts) = roundtrip(request(&[
("models.py", TYPES),
(
"rules.py",
"from models import Address as Home, Pair\ndef choose(flag: bool, first: Home, second: Home) -> Home:\n return first if flag else second\ndef make_pair() -> Pair:\n return (1, 'one')\n",
),
]));
assert_eq!(
artifacts
.iter()
.map(|a| a.path.as_str())
.collect::<Vec<_>>(),
["models.py", "rules.py"]
);
assert_eq!(
ir["distribution"]["Library"]["def"]["modules"]["rules"]["Public"]["values"]["choose"]["Public"]
["ExpressionBody"]["outputType"],
json!("acme/example:models#address")
);
}
#[test]
fn nested_modules_resolve_absolute_relative_and_qualified_imports() {
let mut input = request(&[
("file:///project/src/domain/models.py", TYPES),
(
"file:///project/src/domain/rules.py",
"from .models import Pair\nimport domain.models as model\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass Holder:\n home: model.Address\ndef make_pair() -> Pair:\n return (1, 'one')\n",
),
(
"file:///project/src/app.py",
"import domain.models\nfrom domain import rules as rule\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass App:\n home: domain.models.Address\n holder: rule.Holder\n",
),
]);
input.sources.root = Some("file:///project/src".into());
input.package.exposed_modules = Some(vec![
"App".into(),
"Domain.Models".into(),
"Domain.Rules".into(),
]);
let (_, artifacts) = roundtrip(input);
assert_eq!(
artifacts
.iter()
.map(|a| a.path.as_str())
.collect::<Vec<_>>(),
["app.py", "domain/models.py", "domain/rules.py"]
);
}
#[test]
fn mutually_referencing_records_and_same_named_types_roundtrip() {
roundtrip(request(&[
(
"first.py",
"from dataclasses import dataclass\nimport second\n@dataclass(frozen=True)\nclass Item:\n other: second.Item\n",
),
(
"second.py",
"from dataclasses import dataclass\nimport first\n@dataclass(frozen=True)\nclass Item:\n other: first.Item\n",
),
]));
}
#[test]
fn rejects_invalid_module_graphs_without_partial_ir() {
for sources in [
vec![],
vec![("models.py", TYPES), ("models.py", TYPES)],
vec![("foo_bar.py", ""), ("fooBar.py", "")],
vec![
("models.py", TYPES),
("rules.py", "from missing import Address\n"),
],
vec![
("models.py", TYPES),
("rules.py", "from models import Missing\n"),
],
vec![("models.py", TYPES), ("rules.py", "from models import *\n")],
vec![
("models.py", TYPES),
("rules.py", "from models import Address as int\n"),
],
vec![
("models.py", TYPES),
(
"rules.py",
"from models import Address\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass Address:\n x: int\n",
),
],
vec![
("models.py", TYPES),
("rules.py", "from .models import Address\n"),
],
vec![
(
"a.py",
"from b import Pair as Other\ntype Pair = tuple[Other, int]\n",
),
(
"b.py",
"from a import Pair as Other\ntype Pair = tuple[Other, int]\n",
),
],
vec![("../models.py", TYPES)],
vec![("models/__init__.py", TYPES)],
vec![("models.py", TYPES), ("models/child.py", TYPES)],
] {
let result = PythonExtension.compile(request(&sources)).unwrap();
assert!(!result.success, "accepted {sources:?}");
assert!(result.ir.is_none());
assert!(result.modules.is_empty());
}
}
#[test]
fn rejects_sources_outside_root_and_unknown_exposed_modules() {
let mut input = request(&[("file:///other/models.py", TYPES)]);
input.sources.root = Some("file:///project".into());
assert!(!PythonExtension.compile(input).unwrap().success);
let mut input = request(&[("models.py", TYPES), ("rules.py", "")]);
input.package.exposed_modules = Some(vec!["Missing".into()]);
assert!(!PythonExtension.compile(input).unwrap().success);
}
#[test]
fn imports_sharing_a_package_prefix_and_parent_relative_imports_roundtrip() {
roundtrip(request(&[
("domain/models.py", TYPES),
("domain/other.py", TYPES),
(
"domain/rules/select.py",
"from ..models import Address\nfrom .. import other\nimport domain.models\nimport domain.other\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass Choice:\n first: domain.models.Address\n second: domain.other.Address\n third: other.Address\n fourth: Address\n",
),
]));
}
#[test]
fn generated_module_imports_avoid_declarations_and_constructor_names() {
let (_, artifacts) = roundtrip(request(&[
("models.py", TYPES),
(
"rules.py",
"from models import Address\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass MorphirModule1:\n home: Address\n@dataclass(frozen=True)\nclass MorphirModule2:\n pass\ntype Choice = MorphirModule2\ndef morphir_module_1(value: Address) -> Address:\n return value\ndef morphir_module_2(value: Address) -> Address:\n return value\n",
),
]));
assert!(
artifacts[1]
.content
.contains("import models as morphir_module_3")
);
}
#[test]
fn reports_every_module_and_locates_bad_imports() {
let result = PythonExtension
.compile(request(&[("b.py", ""), ("a.py", "")]))
.unwrap();
assert_eq!(result.modules, ["a", "b"]);
let result = PythonExtension
.compile(request(&[
("models.py", TYPES),
("bad.py", "from models import Missing\n"),
]))
.unwrap();
assert!(!result.success);
assert_eq!(
result.diagnostics[0].location.as_ref().unwrap().uri,
"bad.py"
);
}
#[test]
fn backend_rejects_unresolved_cross_module_references_atomically() {
let mut ir = compile(request(&[
("models.py", TYPES),
(
"rules.py",
"from models import Address\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass Holder:\n home: Address\n",
),
]));
ir["distribution"]["Library"]["def"]["modules"]["rules"]["Public"]["types"]["holder"]["Public"]
["TypeAliasDefinition"]["typeExp"]["Record"]["fields"]["home"] =
json!("acme/example:models#missing");
let result = PythonExtension
.generate(GenerateRequest {
ir,
target: "python".into(),
options: Default::default(),
})
.unwrap();
assert!(!result.success);
assert!(result.artifacts.is_empty());
}
#[test]
fn types_only_applies_to_every_module() {
let mut input = request(&[
("models.py", TYPES),
("rules.py", "def count() -> int:\n return 1\n"),
]);
input.options.types_only = true;
assert!(!PythonExtension.compile(input).unwrap().success);
}
#[test]
fn rejects_module_paths_that_collide_on_case_insensitive_filesystems() {
for sources in [
vec![("API.py", ""), ("api.py", "")],
vec![("API.py", ""), ("api/models.py", "")],
vec![("domain/API.py", ""), ("domain/api.py", "")],
] {
assert!(
!PythonExtension.compile(request(&sources)).unwrap().success,
"accepted {sources:?}"
);
}
let ir = json!({"formatVersion": 4, "distribution": {"Library": {
"packageName": "acme/example", "dependencies": {}, "def": {"modules": {
"API": {"Public": {"types": {}, "values": {}}},
"api": {"Public": {"types": {}, "values": {}}}
}}
}}});
let result = PythonExtension
.generate(GenerateRequest {
ir,
target: "python".into(),
options: Default::default(),
})
.unwrap();
assert!(!result.success);
assert!(result.artifacts.is_empty());
}
#[test]
fn document_uri_metadata_and_percent_encoding_do_not_change_module_identity() {
let mut input = request(&[
(
"file:///my%20project/src/domain/%6dodels.py?rev=1#selection",
TYPES,
),
(
"file:///my%20project/src/domain/rules.py?rev=2",
"from .models import Pair\ndef origin() -> Pair:\n return (0, '')\n",
),
]);
input.sources.root = Some("file:///my%20project/src/".into());
roundtrip(input);
for uri in [
"file://other/project/src/models.py",
"file:///project/src/../models.py",
"file:///project/src/%2e%2e/models.py",
"file:///project/src/domain%2Fmodels.py",
"file:///project/src/domain%5Cmodels.py",
] {
let mut input = request(&[(uri, TYPES)]);
input.sources.root = Some("file:///project/src".into());
assert!(
!PythonExtension.compile(input).unwrap().success,
"accepted {uri}"
);
}
}