-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpython_engine_test.rs
More file actions
122 lines (106 loc) · 3.83 KB
/
Copy pathpython_engine_test.rs
File metadata and controls
122 lines (106 loc) · 3.83 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
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::print_stderr,
clippy::needless_raw_string_hashes
)]
#[tokio::test]
async fn python_roundtrip() {
// Skip if python3 not available or harmont deps missing
if which::which("python3").is_err() {
eprintln!("skipping: python3 not on PATH");
return;
}
let dir = tempfile::tempdir().unwrap();
let harmont = dir.path().join(".hm");
std::fs::create_dir_all(&harmont).unwrap();
std::fs::write(
harmont.join("ci.py"),
r#"import harmont as hm
@hm.pipeline('ci')
def ci() -> hm.Step:
return hm.scratch().sh('echo test', label='test')
"#,
)
.unwrap();
let lang = hm_dsl_engine::detect::detect_language(dir.path()).unwrap();
assert_eq!(lang, hm_dsl_engine::DslLanguage::Python);
let engine = hm_dsl_engine::engine_for(lang).unwrap();
let metas = engine.list_pipelines(dir.path()).await.unwrap();
assert_eq!(metas.len(), 1);
assert_eq!(metas[0].slug, "ci");
let json_str = engine.render_pipeline_json(dir.path(), "ci").await.unwrap();
let v: serde_json::Value = serde_json::from_str(&json_str).unwrap();
assert_eq!(v["version"], "0");
}
#[tokio::test]
async fn python_load_error_is_denoised() {
// A pipeline file that raises at import time should produce an error that
// points at the user's file and the exception — not the importlib /
// harness bootstrap frames the user can't act on.
if which::which("python3").is_err() {
eprintln!("skipping: python3 not on PATH");
return;
}
let dir = tempfile::tempdir().unwrap();
let harmont = dir.path().join(".hm");
std::fs::create_dir_all(&harmont).unwrap();
std::fs::write(
harmont.join("ci.py"),
r#"import harmont as hm
raise RuntimeError("boom from user code")
"#,
)
.unwrap();
let engine = hm_dsl_engine::engine_for(hm_dsl_engine::DslLanguage::Python).unwrap();
let err = engine
.render_pipeline_json(dir.path(), "ci")
.await
.expect_err("loading a raising pipeline file must fail");
let msg = format!("{err:#}");
// Points precisely: which file, and the actual exception.
assert!(msg.contains("ci.py"), "should name the pipeline file: {msg}");
assert!(
msg.contains("RuntimeError: boom from user code"),
"should surface the exception: {msg}"
);
// Points at the offending line in the user's file.
assert!(
msg.contains("ci.py:3"),
"should point at the failing line: {msg}"
);
// De-noised: the harness/importlib bootstrap frames are gone.
assert!(
!msg.contains("_bootstrap") && !msg.contains("exec_module"),
"should drop importlib/harness frames: {msg}"
);
}
#[tokio::test]
async fn python_registry_json_carries_triggers_and_allow_manual() {
if which::which("python3").is_err() {
eprintln!("skipping: python3 not on PATH");
return;
}
let dir = tempfile::tempdir().unwrap();
let harmont = dir.path().join(".hm");
std::fs::create_dir_all(&harmont).unwrap();
std::fs::write(
harmont.join("ci.py"),
r#"import harmont as hm
@hm.pipeline('ci', name='CI', triggers=[hm.push(branch='main')], allow_manual=False)
def ci() -> hm.Step:
return hm.scratch().sh('echo test', label='test')
"#,
)
.unwrap();
let engine = hm_dsl_engine::engine_for(hm_dsl_engine::DslLanguage::Python).unwrap();
let json = engine.registry_json(dir.path()).await.unwrap();
let v: serde_json::Value = serde_json::from_str(&json).unwrap();
let p = &v["pipelines"][0];
assert_eq!(p["slug"], "ci");
assert_eq!(p["name"], "CI");
assert_eq!(p["allow_manual"], false);
assert_eq!(p["triggers"][0]["event"], "push");
assert_eq!(p["triggers"][0]["branches"][0], "main");
assert_eq!(p["definition"]["version"], "0");
}