mirror of
https://github.com/telemt/telemt.git
synced 2026-06-18 17:08:29 +03:00
Use tokio::fs for I/O in config API tests
The save and patch paths under test are async, so the tests now use tokio::fs instead of blocking std::fs. The config_store tests also switch to tempfile::tempdir() for panic-safe cleanup instead of manual remove_dir_all.
This commit is contained in:
@@ -333,7 +333,7 @@ mod tests {
|
|||||||
let patch: Json = serde_json::json!({"general": {"links": {"show": ["alice"]}}});
|
let patch: Json = serde_json::json!({"general": {"links": {"show": ["alice"]}}});
|
||||||
let resp = apply_patch_to_path(&path, &patch, None).await.unwrap();
|
let resp = apply_patch_to_path(&path, &patch, None).await.unwrap();
|
||||||
assert!(resp.changed.iter().any(|c| c == "general"));
|
assert!(resp.changed.iter().any(|c| c == "general"));
|
||||||
let written = std::fs::read_to_string(&path).unwrap();
|
let written = tokio::fs::read_to_string(&path).await.unwrap();
|
||||||
let parsed: toml::Value = toml::from_str(&written).unwrap();
|
let parsed: toml::Value = toml::from_str(&written).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parsed["general"]["links"]["show"][0].as_str(),
|
parsed["general"]["links"]["show"][0].as_str(),
|
||||||
@@ -353,7 +353,7 @@ mod tests {
|
|||||||
let patch: Json = serde_json::json!({"general": {"links": {"public_port": 443}}});
|
let patch: Json = serde_json::json!({"general": {"links": {"public_port": 443}}});
|
||||||
apply_patch_to_path(&path, &patch, None).await.unwrap();
|
apply_patch_to_path(&path, &patch, None).await.unwrap();
|
||||||
|
|
||||||
let written = std::fs::read_to_string(&path).unwrap();
|
let written = tokio::fs::read_to_string(&path).await.unwrap();
|
||||||
assert!(written.contains("public_port = 443"), "{written}");
|
assert!(written.contains("public_port = 443"), "{written}");
|
||||||
assert!(!written.contains("443.0"), "must not be a float:\n{written}");
|
assert!(!written.contains("443.0"), "must not be a float:\n{written}");
|
||||||
assert!(!written.contains("\"443\""), "must not be a string:\n{written}");
|
assert!(!written.contains("\"443\""), "must not be a string:\n{written}");
|
||||||
|
|||||||
@@ -520,14 +520,14 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn save_general_section_keeps_subtables_dotted_without_duplicates() {
|
async fn save_general_section_keeps_subtables_dotted_without_duplicates() {
|
||||||
let dir = std::env::temp_dir().join(format!("cfgtest-{}", rand::random::<u64>()));
|
let dir = tempfile::tempdir().unwrap();
|
||||||
std::fs::create_dir_all(&dir).unwrap();
|
let path = dir.path().join("config.toml");
|
||||||
let path = dir.join("config.toml");
|
tokio::fs::write(
|
||||||
std::fs::write(
|
|
||||||
&path,
|
&path,
|
||||||
"[general]\nprefer_ipv6 = false\n\n[general.modes]\ntls = true\n\n\
|
"[general]\nprefer_ipv6 = false\n\n[general.modes]\ntls = true\n\n\
|
||||||
[general.links]\npublic_host = \"old.example\"\n\n[server]\nport = 443\n",
|
[general.links]\npublic_host = \"old.example\"\n\n[server]\nport = 443\n",
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut cfg = ProxyConfig::default();
|
let mut cfg = ProxyConfig::default();
|
||||||
@@ -537,7 +537,7 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let written = std::fs::read_to_string(&path).unwrap();
|
let written = tokio::fs::read_to_string(&path).await.unwrap();
|
||||||
|
|
||||||
// No bare top-level [modes] / [links] headers leaked.
|
// No bare top-level [modes] / [links] headers leaked.
|
||||||
for line in written.lines() {
|
for line in written.lines() {
|
||||||
@@ -563,19 +563,18 @@ mod tests {
|
|||||||
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
||||||
|
|
||||||
assert!(written.contains("[server]\nport = 443")); // untouched table kept
|
assert!(written.contains("[server]\nport = 443")); // untouched table kept
|
||||||
std::fs::remove_dir_all(&dir).ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn save_general_section_is_idempotent_across_repeated_saves() {
|
async fn save_general_section_is_idempotent_across_repeated_saves() {
|
||||||
let dir = std::env::temp_dir().join(format!("cfgtest-{}", rand::random::<u64>()));
|
let dir = tempfile::tempdir().unwrap();
|
||||||
std::fs::create_dir_all(&dir).unwrap();
|
let path = dir.path().join("config.toml");
|
||||||
let path = dir.join("config.toml");
|
tokio::fs::write(
|
||||||
std::fs::write(
|
|
||||||
&path,
|
&path,
|
||||||
"[general]\nprefer_ipv6 = false\n\n[general.modes]\ntls = true\n\n\
|
"[general]\nprefer_ipv6 = false\n\n[general.modes]\ntls = true\n\n\
|
||||||
[general.links]\npublic_host = \"old.example\"\n",
|
[general.links]\npublic_host = \"old.example\"\n",
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut cfg = ProxyConfig::default();
|
let mut cfg = ProxyConfig::default();
|
||||||
@@ -588,13 +587,12 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let written = std::fs::read_to_string(&path).unwrap();
|
let written = tokio::fs::read_to_string(&path).await.unwrap();
|
||||||
assert_eq!(written.matches("[general.modes]").count(), 1, "{written}");
|
assert_eq!(written.matches("[general.modes]").count(), 1, "{written}");
|
||||||
assert_eq!(written.matches("[general.links]").count(), 1, "{written}");
|
assert_eq!(written.matches("[general.links]").count(), 1, "{written}");
|
||||||
assert_eq!(written.matches("[general]").count(), 1, "{written}");
|
assert_eq!(written.matches("[general]").count(), 1, "{written}");
|
||||||
toml::from_str::<toml::Value>(&written)
|
toml::from_str::<toml::Value>(&written)
|
||||||
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
||||||
std::fs::remove_dir_all(&dir).ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -623,15 +621,15 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn save_general_handles_non_contiguous_subtables() {
|
async fn save_general_handles_non_contiguous_subtables() {
|
||||||
let dir = std::env::temp_dir().join(format!("cfgtest-{}", rand::random::<u64>()));
|
let dir = tempfile::tempdir().unwrap();
|
||||||
std::fs::create_dir_all(&dir).unwrap();
|
let path = dir.path().join("config.toml");
|
||||||
let path = dir.join("config.toml");
|
|
||||||
// Hand-edited layout: [general.modes] sits AFTER an unrelated [server].
|
// Hand-edited layout: [general.modes] sits AFTER an unrelated [server].
|
||||||
std::fs::write(
|
tokio::fs::write(
|
||||||
&path,
|
&path,
|
||||||
"[general]\nprefer_ipv6 = false\n\n[server]\nport = 443\n\n\
|
"[general]\nprefer_ipv6 = false\n\n[server]\nport = 443\n\n\
|
||||||
[general.modes]\ntls = true\n",
|
[general.modes]\ntls = true\n",
|
||||||
)
|
)
|
||||||
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut cfg = ProxyConfig::default();
|
let mut cfg = ProxyConfig::default();
|
||||||
@@ -641,7 +639,7 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let written = std::fs::read_to_string(&path).unwrap();
|
let written = tokio::fs::read_to_string(&path).await.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
written.matches("[general.modes]").count(),
|
written.matches("[general.modes]").count(),
|
||||||
1,
|
1,
|
||||||
@@ -650,7 +648,6 @@ mod tests {
|
|||||||
toml::from_str::<toml::Value>(&written)
|
toml::from_str::<toml::Value>(&written)
|
||||||
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
.unwrap_or_else(|e| panic!("written config must parse: {e}\n{written}"));
|
||||||
assert!(written.contains("[server]")); // unrelated section preserved
|
assert!(written.contains("[server]")); // unrelated section preserved
|
||||||
std::fs::remove_dir_all(&dir).ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user