refactor: separate language engine from CLI and server hosts
Verify Aster engine / verify (push) Failing after 0s
Verify Aster engine / verify (push) Failing after 0s
This commit is contained in:
+8
-18
@@ -1,20 +1,10 @@
|
||||
# Cargo 工作区
|
||||
# 引擎工作区
|
||||
|
||||
包名与目录一致,路径依赖定义在根 Cargo.toml。
|
||||
| 目录 | 包及职责 |
|
||||
| --- | --- |
|
||||
| [objects](objects/README.md) | 零外部依赖;核心对象、私有堆、Number、代码和指令数据 |
|
||||
| [core](core/README.md) | 库名 endor_core;仅依赖 objects,提供 VM、执行帧和求值循环 |
|
||||
| [frontend](frontend/README.md) | Reader、PEG lexer/parser、ASDL AST 和编辑器接口 |
|
||||
| [compiler](compiler/README.md) | Frontend AST 到 wordcode;依赖 frontend 和 core |
|
||||
|
||||
| 目录 | 包名/产物 | 职责 |
|
||||
| --- | --- | --- |
|
||||
| [core](core/README.md) | core,库 endor_core | VM、执行帧与字节码求值循环,仅依赖本地 objects |
|
||||
| [objects](objects/README.md) | objects | 零依赖的核心对象、对象存储、代码对象与指令数据/静态验证 |
|
||||
| [frontend](frontend/README.md) | frontend | Endor 阅读器、字符 PEG lexer、token PEG parser、ASDL 生成的 AST/遍历/JSON 与编辑器接口,不执行源码 |
|
||||
| [compiler](compiler/README.md) | compiler | AST 名称/作用域分析、wordcode 模板生成和 VM 代码对象装载 |
|
||||
| [host](host/README.md) | host | UTF-8 JSON 文件基础信息,与数据库分离 |
|
||||
| [storage](storage/README.md) | storage | 为数据库特殊类保留的显式连接适配,不提供基础信息存储 |
|
||||
| [cli](cli/README.md) | cli,二进制 cli | 引导演示、源码解析/执行与文件信息命令,不使用数据库 |
|
||||
| [server](server/README.md) | server,二进制 server | HTTP 宿主与文件版本信息,不使用数据库 |
|
||||
|
||||
CLI 依赖 Compiler、Core、Frontend 与 Host,Server 依赖 Core 与 Host,二者不依赖 Storage;Compiler 依赖 Frontend 与 Core;Frontend 依赖 peg、serde/serde_json,不依赖 Core;Core 只依赖本地 Objects;Objects 不依赖 Core 或第三方包,二者均不依赖 Storage。未来特殊类的宿主实现按需依赖 Storage,见 [数据库对象](../docs/runtime/database-objects.md)。
|
||||
|
||||
Endor 的解释执行流程与平台资源版本方向见 [执行与版本模型](../docs/architecture/execution-and-versions.md)。Compiler 已连接 Frontend 与 Core 的最小源码执行链路,见 [编译契约](../docs/runtime/compiler.md);源码闭包、数值算术、版本锁、平台资源后端和持久化字节码缓存仍未实现。
|
||||
|
||||
core 的 Rust 库名为 endor_core,避免与标准库 core 重名,见 [Cargo 目标说明](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field)。网络、数据库、进程配置和信号不进入 Core。全局说明见 [架构](../docs/architecture/README.md),返回 [项目入口](../README.md)。
|
||||
依赖方向为 compiler → frontend + core、core → objects;frontend 依赖 peg、serde 和 serde_json。网络、数据库、进程配置和 GUI 能力由独立宿主提供。CLI、Server、Host、Storage 已移到独立宿主仓库。返回 [项目入口](../README.md)。
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
[package]
|
||||
name = "cli"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "cli"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
compiler.workspace = true
|
||||
core.workspace = true
|
||||
frontend.workspace = true
|
||||
host.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -1,11 +0,0 @@
|
||||
# cli
|
||||
|
||||
同步命令行宿主,提供 bootstrap、parse、run、eval、help 和文件 metadata 命令。bootstrap 演示两个运行时对象并释放,不读取数据库配置、不连接或写入数据库。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | cli 二进制与 compiler/core/frontend/host 依赖;无 storage 或 Tokio 依赖 |
|
||||
| [src](src/README.md) | 参数路由、引导演示、文件/stdin 阅读、源码解析/执行和退出码 |
|
||||
| [tests](tests/README.md) | parse 的 AST/JSON 与 run/eval 的文件/stdin 执行、错误和退出码 |
|
||||
|
||||
运行方式见 [运行指南](../../docs/operations/README.md),输入边界见 [配置](../../docs/operations/configuration.md)。parse 通过 EndorReader 严格读取 UTF-8 文件或 `-` 指定的 stdin 并输出 AstRoot;--mode 选择 module/expression/interactive,--json 输出带 schema 的 AST JSON,不执行源码,见 [前端](../../docs/runtime/frontend.md);run 调用模块的无参数入口(默认 main),eval 执行独立表达式,见 [编译链路](../../docs/runtime/compiler.md);metadata put/get 和只导入缺失项的 import 均使用文件,见 [宿主信息](../../docs/operations/host-information.md)。返回 [工作区](../README.md)。
|
||||
@@ -1,9 +0,0 @@
|
||||
# CLI 入口
|
||||
|
||||
[main.rs](main.rs) 是二进制入口:同步 main 将 run 的结果转成进程退出码;run 路由 bootstrap、parse、源码 run/eval、help、文件 metadata put/get/import,错误打印到 stderr 并返回失败。
|
||||
|
||||
bootstrap 创建并显式释放两个对象,不读取数据库环境、不打开数据库。parse_command 使用 EndorReader 和 parse_source_ast 输出 AstRoot;--mode 选择 module/expression/interactive(默认 module),--json 通过 AstDocument 输出 JSON,格式见 [AST 模型](../../../docs/runtime/ast.md);`-` 从 stdin 读到 EOF,来源为 `<stdin>`。词法/语法错误显示来源、行、列;不创建 VM 或执行源码。未知命令报错;metadata 通过 host::MetadataStore 读写文件,import 从标准输入接收旧记录且不覆盖已有文件。参数目前由 std::env::args 读取,非 Unicode 参数可能 panic,边界见 [配置](../../../docs/operations/configuration.md)。
|
||||
|
||||
[execute.rs](execute.rs) 负责 run/eval 的文件/stdin 阅读、调用 compiler、创建 VM/globals、模块初始化与入口调用、结果格式化、traceback 和成功/失败后的根释放;命名空间由 CLI 自己创建并清空,见 [编译契约](../../../docs/runtime/compiler.md)。
|
||||
|
||||
新增命令时更新用法、[运行指南](../../../docs/operations/README.md) 和 [smoke.py 的导航](../../../scripts/README.md)。返回 [cli](../README.md)。
|
||||
@@ -1,105 +0,0 @@
|
||||
use std::error::Error;
|
||||
|
||||
use compiler::{CompileOptions, compile_source};
|
||||
use endor_core::{Value, Vm};
|
||||
use frontend::{EndorReader, ParseMode, SourceUnit};
|
||||
|
||||
pub(super) fn run_command(arguments: &[&str]) -> Result<(), Box<dyn Error>> {
|
||||
let mut entry = "main";
|
||||
let mut path = None;
|
||||
let mut arguments = arguments.iter().copied();
|
||||
while let Some(argument) = arguments.next() {
|
||||
match argument {
|
||||
"--entry" => {
|
||||
entry = arguments
|
||||
.next()
|
||||
.filter(|name| !name.is_empty() && !name.starts_with("--"))
|
||||
.ok_or(super::USAGE)?;
|
||||
}
|
||||
value if !value.starts_with("--") && path.is_none() => path = Some(value),
|
||||
_ => return Err(super::USAGE.into()),
|
||||
}
|
||||
}
|
||||
let source = read(path.ok_or(super::USAGE)?)?;
|
||||
let output = execute(&source, ParseMode::Module, Some(entry))?;
|
||||
println!("{output}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn eval_command(path: &str) -> Result<(), Box<dyn Error>> {
|
||||
let source = read(path)?;
|
||||
let output = execute(&source, ParseMode::Expression, None)?;
|
||||
println!("{output}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read(path: &str) -> Result<SourceUnit, Box<dyn Error>> {
|
||||
Ok(if path == "-" {
|
||||
EndorReader::from_stream("<stdin>", std::io::stdin().lock())?
|
||||
} else {
|
||||
EndorReader::from_file(path)?
|
||||
})
|
||||
}
|
||||
|
||||
fn execute(
|
||||
source: &SourceUnit,
|
||||
mode: ParseMode,
|
||||
entry: Option<&str>,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let compiled = compile_source(source, mode, &CompileOptions::default())?;
|
||||
let mut vm = Vm::new()?;
|
||||
let code = compiled.load(&mut vm)?;
|
||||
let globals = match vm.new_dict() {
|
||||
Ok(globals) => globals,
|
||||
Err(error) => {
|
||||
vm.release(code)?;
|
||||
return Err(error.into());
|
||||
}
|
||||
};
|
||||
let result = (|| -> Result<String, Box<dyn Error>> {
|
||||
let mut value = vm.execute(code, globals)?;
|
||||
if let Some(entry) = entry {
|
||||
vm.release_value(value)?;
|
||||
let Some(Value::Object(function)) = vm.dict_get(globals, &Value::Text(entry.into()))?
|
||||
else {
|
||||
return Err(format!("entry function not found: {entry}").into());
|
||||
};
|
||||
value = vm.call(function, &[])?;
|
||||
}
|
||||
let output = format_value(&vm, &value);
|
||||
vm.release_value(value)?;
|
||||
output
|
||||
})();
|
||||
// Copy traceback metadata before cleanup. This is the entry location of
|
||||
// each code object plus bytecode offset, not a per-instruction source map.
|
||||
let result = result.map_err(|error| {
|
||||
let mut message = format!("{}: {error}", source.origin());
|
||||
for location in vm.last_traceback() {
|
||||
message.push_str(&format!(
|
||||
"\n at {} ({}:{}, code unit {})",
|
||||
location.function,
|
||||
location.filename,
|
||||
location.first_line,
|
||||
location.instruction_offset
|
||||
));
|
||||
}
|
||||
Box::<dyn Error>::from(message)
|
||||
});
|
||||
vm.release(code)?;
|
||||
// CLI owns this fresh namespace. Clearing it breaks function↔globals
|
||||
// cycles on both success and failure; embedders choose their own lifetime.
|
||||
vm.dict_clear(globals)?;
|
||||
vm.release(globals)?;
|
||||
vm.collect_cycles();
|
||||
result
|
||||
}
|
||||
|
||||
fn format_value(vm: &Vm, value: &Value) -> Result<String, Box<dyn Error>> {
|
||||
Ok(match value {
|
||||
Value::Nil => "nil".into(),
|
||||
Value::Bool(value) => value.to_string(),
|
||||
Value::Text(value) => value.clone(),
|
||||
Value::Number(value) => value.to_string(),
|
||||
Value::Object(reference) => vm.str(*reference)?,
|
||||
})
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
use std::error::Error;
|
||||
use std::process::ExitCode;
|
||||
|
||||
use endor_core::{Value, Vm};
|
||||
use frontend::ast::AstDocument;
|
||||
use frontend::{EndorReader, ParseMode, parse_source_ast};
|
||||
use host::MetadataStore;
|
||||
|
||||
mod execute;
|
||||
|
||||
const USAGE: &str = "Usage: cli [bootstrap | run [--entry name] <file|-> | eval <file|-> | parse [--mode module|expression|interactive] [--json] <file|-> | metadata put <key> <value> | metadata get <key> | metadata import | help]";
|
||||
|
||||
fn main() -> ExitCode {
|
||||
match run() {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("cli: {error}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<(), Box<dyn Error>> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
match args
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.collect::<Vec<_>>()
|
||||
.as_slice()
|
||||
{
|
||||
[] | ["bootstrap"] => {
|
||||
let mut vm = Vm::new()?;
|
||||
let first = vm.new_object()?;
|
||||
let second = vm.new_object()?;
|
||||
vm.set_attr(
|
||||
first,
|
||||
"message",
|
||||
Value::Text("Hello from EndorObject".into()),
|
||||
)?;
|
||||
println!("runtime=EndorObject type_system=disabled");
|
||||
println!(
|
||||
"objects_distinct={} heap_objects={}",
|
||||
first != second,
|
||||
vm.object_count()
|
||||
);
|
||||
vm.release(first)?;
|
||||
vm.release(second)?;
|
||||
println!("released heap_objects={}", vm.object_count());
|
||||
println!("bootstrap uses no database");
|
||||
}
|
||||
["help"] | ["--help"] | ["-h"] => println!("{USAGE}"),
|
||||
["parse", arguments @ ..] => parse_command(arguments)?,
|
||||
["run", arguments @ ..] => execute::run_command(arguments)?,
|
||||
["eval", path] => execute::eval_command(path)?,
|
||||
["metadata", "put", key, value] => {
|
||||
MetadataStore::from_env()?.put(key, value)?;
|
||||
println!("stored (file)");
|
||||
}
|
||||
["metadata", "get", key] => {
|
||||
println!(
|
||||
"{}",
|
||||
MetadataStore::from_env()?
|
||||
.get(key)?
|
||||
.as_deref()
|
||||
.unwrap_or("(missing)")
|
||||
);
|
||||
}
|
||||
["metadata", "import"] => {
|
||||
let count = MetadataStore::from_env()?.import_missing(std::io::stdin().lock())?;
|
||||
println!("imported {count} missing entries (file)");
|
||||
}
|
||||
_ => return Err(USAGE.into()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_command(arguments: &[&str]) -> Result<(), Box<dyn Error>> {
|
||||
let mut mode = ParseMode::Module;
|
||||
let mut json = false;
|
||||
let mut path = None;
|
||||
let mut arguments = arguments.iter().copied();
|
||||
while let Some(argument) = arguments.next() {
|
||||
match argument {
|
||||
"--json" => json = true,
|
||||
"--mode" => {
|
||||
mode = match arguments.next() {
|
||||
Some("module") => ParseMode::Module,
|
||||
Some("expression") => ParseMode::Expression,
|
||||
Some("interactive") => ParseMode::Interactive,
|
||||
_ => return Err(USAGE.into()),
|
||||
}
|
||||
}
|
||||
value if !value.starts_with("--") && path.is_none() => path = Some(value),
|
||||
_ => return Err(USAGE.into()),
|
||||
}
|
||||
}
|
||||
let path = path.ok_or(USAGE)?;
|
||||
let source = if path == "-" {
|
||||
EndorReader::from_stream("<stdin>", std::io::stdin().lock())?
|
||||
} else {
|
||||
EndorReader::from_file(path)?
|
||||
};
|
||||
let root = parse_source_ast(&source, mode)?;
|
||||
if json {
|
||||
println!("{}", AstDocument::new(root)?.to_json()?);
|
||||
} else {
|
||||
println!("{root:#?}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
# CLI 命令检查
|
||||
|
||||
[parse.rs](parse.rs) 使用 Cargo 提供的 cli 测试二进制检查 parse 命令:合法 UTF-8 源码输出 AST 且不执行名称调用;非法语法输出文件/行/列并返回失败;非法 UTF-8 不替换字符;`parse -` 检查 stdin 输入、EOF 和带流名的错误;--json/--mode 检查表达式与交互根、schema 标识以及非法模式退出。
|
||||
|
||||
[execute.rs](execute.rs) 检查 compiler/examples 中的真实 Endor 文件、run/eval、显式入口、stdin、运行时 traceback、名称/编译/语法/UTF-8 错误和无成功输出的失败退出。
|
||||
|
||||
测试仅在 ces-control 的工作区检查中运行,本地不执行。测试临时文件使用独立名称并在结束时清理。返回 [cli](../README.md),验证约定见 [验证](../../../docs/operations/validation.md)。
|
||||
@@ -1,136 +0,0 @@
|
||||
use std::io::Write;
|
||||
use std::process::{Command, Output, Stdio};
|
||||
|
||||
fn stdin_command(arguments: &[&str], source: &[u8]) -> Output {
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.args(arguments)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
child.stdin.take().unwrap().write_all(source).unwrap();
|
||||
child.wait_with_output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_executes_real_endor_files_and_prints_entry_results() {
|
||||
for (file, expected) in [
|
||||
("hello.endor", "你好,Endor 🌱\n"),
|
||||
("calls.endor", "函数调用已接通\n"),
|
||||
("shadowing.endor", "局部名称优先\n"),
|
||||
("number.endor", "-123456789012345678901234567890.00100\n"),
|
||||
] {
|
||||
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../compiler/examples")
|
||||
.join(file);
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.arg("run")
|
||||
.arg(path)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(String::from_utf8(output.stdout).unwrap(), expected);
|
||||
assert!(output.stderr.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_and_run_read_stdin_and_support_explicit_entry_names() {
|
||||
let output = stdin_command(&["eval", "-"], "\"表达式🌱\"".as_bytes());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(String::from_utf8(output.stdout).unwrap(), "表达式🌱\n");
|
||||
let output = stdin_command(
|
||||
&["run", "--entry", "greet", "-"],
|
||||
b"function greet(): String { return \"ok\" }",
|
||||
);
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(String::from_utf8(output.stdout).unwrap(), "ok\n");
|
||||
let path =
|
||||
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../compiler/examples/literal.endor");
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.arg("eval")
|
||||
.arg(path)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(output.status.success());
|
||||
assert_eq!(
|
||||
String::from_utf8(output.stdout).unwrap(),
|
||||
"表达式 → 字节码 → VM\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn execution_errors_fail_without_success_output_and_report_their_stage() {
|
||||
for (arguments, source, expected) in [
|
||||
(
|
||||
vec!["run", "-"],
|
||||
"function main(): T {\n return missing\n}",
|
||||
"<stdin>:2:10: undefined name: missing",
|
||||
),
|
||||
(
|
||||
vec!["run", "-"],
|
||||
"function main(): T { return f() } function f(x: T): T { return x }",
|
||||
"positional arguments",
|
||||
),
|
||||
(
|
||||
vec!["run", "-"],
|
||||
"function other(): T { return 1 }",
|
||||
"entry function not found: main",
|
||||
),
|
||||
(
|
||||
vec!["eval", "-"],
|
||||
"1 + 2",
|
||||
"arithmetic bytecode is not implemented",
|
||||
),
|
||||
(vec!["eval", "-"], "1()", "does not support call"),
|
||||
(
|
||||
vec!["run", "-"],
|
||||
"function main(): T { return ) }",
|
||||
"expected",
|
||||
),
|
||||
] {
|
||||
let output = stdin_command(&arguments, source.as_bytes());
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
let error = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(error.contains(expected), "{error}");
|
||||
if expected == "positional arguments" {
|
||||
assert!(error.contains("at main (<stdin>:1, code unit"));
|
||||
}
|
||||
}
|
||||
let output = stdin_command(&["eval", "-"], &[0xff]);
|
||||
assert!(!output.status.success());
|
||||
assert!(
|
||||
String::from_utf8(output.stderr)
|
||||
.unwrap()
|
||||
.contains("invalid UTF-8")
|
||||
);
|
||||
for arguments in [
|
||||
vec!["run"],
|
||||
vec!["run", "--entry"],
|
||||
vec!["run", "--unknown", "-"],
|
||||
vec!["eval"],
|
||||
vec!["eval", "-", "extra"],
|
||||
] {
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.args(arguments)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
assert!(String::from_utf8(output.stderr).unwrap().contains("Usage:"));
|
||||
}
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
static NEXT_FILE: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
struct SourceFile(PathBuf);
|
||||
|
||||
impl SourceFile {
|
||||
fn new(content: &[u8]) -> Self {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"aster-parse-{}-{}.endor",
|
||||
std::process::id(),
|
||||
NEXT_FILE.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
std::fs::write(&path, content).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for SourceFile {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_command_prints_ast_without_executing_source() {
|
||||
let source = SourceFile::new("function 表示(): String { return 未定義() }".as_bytes());
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.arg("parse")
|
||||
.arg(&source.0)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||
assert!(stdout.contains("表示"));
|
||||
assert!(stdout.contains("未定義"));
|
||||
assert!(stdout.contains("Call"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_command_reports_source_locations_and_rejects_invalid_utf8() {
|
||||
let source = SourceFile::new(b"function f(): Number {\n return )\n}");
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.arg("parse")
|
||||
.arg(&source.0)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
let stderr = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(stderr.contains(":2:10:"), "{stderr}");
|
||||
assert!(stderr.contains("expected"));
|
||||
let source = SourceFile::new(&[0xff]);
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.arg("parse")
|
||||
.arg(&source.0)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
}
|
||||
|
||||
fn parse_stdin(source: &[u8]) -> std::process::Output {
|
||||
let mut child = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.args(["parse", "-"])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let mut stdin = child.stdin.take().unwrap();
|
||||
stdin.write_all(source).unwrap();
|
||||
drop(stdin);
|
||||
child.wait_with_output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_command_reads_stdin_to_eof_and_reports_its_origin() {
|
||||
let output = parse_stdin("function 表示(): String { return \"日\" }".as_bytes());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert!(String::from_utf8(output.stdout).unwrap().contains("表示"));
|
||||
let output = parse_stdin(b"function f(): Number {\n return )\n}");
|
||||
assert!(!output.status.success());
|
||||
assert!(
|
||||
String::from_utf8(output.stderr)
|
||||
.unwrap()
|
||||
.contains("<stdin>:2:10:")
|
||||
);
|
||||
let output = parse_stdin(&[0xff]);
|
||||
assert!(!output.status.success());
|
||||
assert!(
|
||||
String::from_utf8(output.stderr)
|
||||
.unwrap()
|
||||
.contains("<stdin>: invalid UTF-8 at byte 0")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_command_exports_versioned_json_for_expression_and_interactive_inputs() {
|
||||
for (mode, source) in [("expression", "f(\"日\")"), ("interactive", "let x = 1")] {
|
||||
let source = SourceFile::new(source.as_bytes());
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.args(["parse", "--json", "--mode", mode])
|
||||
.arg(&source.0)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let document =
|
||||
frontend::ast::AstDocument::from_json(std::str::from_utf8(&output.stdout).unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(document.schema, frontend::ast::AST_SCHEMA_ID);
|
||||
match (mode, document.root) {
|
||||
("expression", frontend::ast::AstRoot::Expression(_))
|
||||
| ("interactive", frontend::ast::AstRoot::Interactive(_)) => {}
|
||||
_ => panic!("incorrect parse mode"),
|
||||
}
|
||||
}
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_cli"))
|
||||
.args(["parse", "--mode", "unknown", "-"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success());
|
||||
assert!(output.stdout.is_empty());
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
| --- | --- |
|
||||
| [Cargo.toml](Cargo.toml) | compiler 包、本地 frontend/core 依赖与工作区 lint |
|
||||
| [src](src/README.md) | 分析、字节码生成与显式代码对象装载 |
|
||||
| [examples](examples/README.md) | 可供 cli run/eval 的 Endor 源码文件,也是远端集成测试输入 |
|
||||
| [examples](examples/README.md) | 可供 lyra run/eval 的 Endor 源码文件,也是远端集成测试输入 |
|
||||
| [tests](tests/README.md) | 源码到 VM、错误路径、名称与对象生命周期检查 |
|
||||
|
||||
公开入口为 analyze、compile_ast、compile_source、CompileOptions 和 CompiledCode::load。完整规则与所有权见 [编译链路](../../docs/runtime/compiler.md);CLI 使用见 [运行指南](../../docs/operations/README.md)。修改语法仍进入 [frontend](../frontend/README.md),修改指令/验证进入 [objects](../objects/README.md),修改执行进入 [core](../core/README.md)。返回 [工作区](../README.md)。
|
||||
|
||||
@@ -12,13 +12,13 @@
|
||||
| [literal.endor](literal.endor) | eval;返回“表达式 → 字节码 → VM” |
|
||||
|
||||
```sh
|
||||
cli run crates/compiler/examples/hello.endor
|
||||
cli run crates/compiler/examples/calls.endor
|
||||
cli run crates/compiler/examples/shadowing.endor
|
||||
cli run crates/compiler/examples/number.endor
|
||||
cli eval crates/compiler/examples/literal.endor
|
||||
lyra run crates/compiler/examples/hello.endor
|
||||
lyra run crates/compiler/examples/calls.endor
|
||||
lyra run crates/compiler/examples/shadowing.endor
|
||||
lyra run crates/compiler/examples/number.endor
|
||||
lyra eval crates/compiler/examples/literal.endor
|
||||
```
|
||||
|
||||
run 默认调用无参数 main,可用 --entry name 选择另一个无参数入口;eval 解析一个完整表达式,二者都支持 - 读取 stdin 到 EOF。类型名称暂只保留语法,Callable 不要求预先注册语言类。例子不依赖 print 或其他默认标准库。编译器尚无算术运行语义;前端 basic.endor 含算术,只是解析示例,不能混作可执行示例。规则见 [编译契约](../../../docs/runtime/compiler.md),返回 [compiler](../README.md)。
|
||||
|
||||
CI 使用 [endor_examples.py](../../../scripts/endor_examples.py) 再次执行真实 CLI 并保存实际结果,不只显示 Rust 测试名称。对应 Actions run 的 endor-execution-output-<sha> artifact 包含源码、命令、预期/实际 stdout、stderr、退出码和状态;位置与失败规则见 [验证报告](../../../docs/operations/validation.md)。新增 .endor 示例时同步 cases.json,未覆盖文件会让报告步骤失败。
|
||||
本目录与 cases.json 继续作为引擎测试资料。独立 Lyra CLI 的 CI 使用固定 Aster 子模块中的同一份样例,实际执行 lyra run/eval 并保存 stdout/stderr、退出码及报告 artifact;报告脚本和进程入口检查位于 Lyra 仓库。Aster CI 只验证引擎,不构建 CLI。
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
[pipeline.rs](pipeline.rs) 将真实 Endor 示例交给 reader/parser/名称分析/代码生成/VM,检查函数值、前向引用、局部遮蔽、精确数字常量、语义诊断、宿主全局与属性回调、对象结果保活、跨 VM 模板装载、反复调用、错误帧清理、递归限额、共享交互命名空间及超过 255 的操作数。
|
||||
|
||||
同时检查重复名称、局部提前读取、未声明名称、不支持的执行特性和不合法 AST 原文范围。CLI 的文件/stdin 与退出码检查见 [cli/tests](../../cli/tests/README.md)。源码说明验证意图;仅由 ces-control 的 cargo test --workspace --locked 执行,本地不运行,结果边界见 [验证](../../../docs/operations/validation.md)。返回 [compiler](../README.md)。
|
||||
同时检查重复名称、局部提前读取、未声明名称、不支持的执行特性和不合法 AST 原文范围。CLI 的文件/stdin 与退出码检查见 [cli/tests](https://gitea.entroia.com/Zayden-Jung/endorphin-lyra-cli/src/branch/main/crates/cli/tests/README.md)。源码说明验证意图;仅由 ces-control 的 cargo test --workspace --locked 执行,本地不运行,结果边界见 [验证](../../../docs/operations/validation.md)。返回 [compiler](../README.md)。
|
||||
|
||||
@@ -9,6 +9,6 @@
|
||||
| [editor](editor/README.md) | 同源生成的 TypeScript 类型/遍历器和 JSON Schema |
|
||||
| [src](src/README.md) | 阅读器、SourceUnit、token、源码范围、诊断、AST、词法规则独立在 [lexer.peg](src/lexer.peg),语法规则在 [grammar.peg](src/grammar.peg) |
|
||||
| [tests](tests/README.md) | Unicode、优先级、函数、完整输入与错误位置验证 |
|
||||
| [examples](examples/README.md) | 可交给 cli parse 的源码样例 |
|
||||
| [examples](examples/README.md) | 可交给 lyra parse 的源码样例 |
|
||||
|
||||
宿主通过 EndorReader 读取字符串、流、文件或注入的 URI provider,取得 SourceUnit 后调用 lex_source、parse_source、parse_source_expression;统一入口 parse_ast/parse_source_ast 通过 ParseMode 返回 AstRoot;低层 lex、parse、parse_expression 仍保留。AST 定义、生成命令、Visitor 与 JSON 交换见 [AST 模型](../../docs/runtime/ast.md)。读取契约见 [Endor 阅读器](../../docs/runtime/reader.md)。Objects 保持零依赖,Core 仅依赖本地 Objects;后续把 AST 转成已有字节码并交给 VM 解释执行;这是平台内部准备步骤,尚未接通。资源修订、依赖锁和解释器统一升级的设计方向见 [执行与版本模型](../../docs/architecture/execution-and-versions.md)。返回 [工作区](../README.md)。
|
||||
宿主通过 EndorReader 读取字符串、流、文件或注入的 URI provider,取得 SourceUnit 后调用 lex_source、parse_source、parse_source_expression;统一入口 parse_ast/parse_source_ast 通过 ParseMode 返回 AstRoot;低层 lex、parse、parse_expression 仍保留。AST 定义、生成命令、Visitor 与 JSON 交换见 [AST 模型](../../docs/runtime/ast.md)。读取契约见 [Endor 阅读器](../../docs/runtime/reader.md)。Objects 保持零依赖,Core 仅依赖本地 Objects;独立 Compiler 已将 AST 转成 wordcode 并交给 VM 执行;这是平台内部准备步骤。资源修订、依赖锁和解释器统一升级的设计方向见 [执行与版本模型](../../docs/architecture/execution-and-versions.md)。返回 [工作区](../README.md)。
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
[basic.endor](basic.endor) 包含字符串函数、位置参数、let 绑定、return 与算术表达式。它展示当前能解析的源码,不表示已能编译和执行运算。
|
||||
|
||||
在指定远端环境使用 `cli parse crates/frontend/examples/basic.endor` 输出 AST;工作区运行方法见 [运行指南](../../../docs/operations/README.md)。syntax.rs 包含此样例的解析检查。返回 [frontend](../README.md)。
|
||||
在指定远端环境使用 `lyra parse crates/frontend/examples/basic.endor` 输出 AST;工作区运行方法见 [运行指南](../../../docs/operations/README.md)。syntax.rs 包含此样例的解析检查。返回 [frontend](../README.md)。
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
[package]
|
||||
name = "host"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -1,11 +0,0 @@
|
||||
# host
|
||||
|
||||
宿主基础信息的文件层,与 Core 和数据库 Storage 分开。CLI/Server 使用它保存 host.version、deployment.release 和其他命名信息;不打开数据库,不保存 VM 堆。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | host 包、Serde/JSON 依赖及远端检查依赖 |
|
||||
| [src](src/README.md) | MetadataStore、UTF-8 JSON 文件与迁移导入 |
|
||||
| [tests](tests/README.md) | 持久读取、替换、并发、坏记录、路径边界和幂等导入 |
|
||||
|
||||
ASTER_STATE_DIR 指定信息目录;默认 data/host,容器使用 /data/host。协议、生命周期及旧数据库一次性迁移见 [宿主信息](../../docs/operations/host-information.md)。返回 [工作区](../README.md)。
|
||||
@@ -1,8 +0,0 @@
|
||||
# Host 源码导航
|
||||
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | 导出 MetadataStore,明确文件层不依赖 VM 或 SQL |
|
||||
| [metadata.rs](metadata.rs) | 目录配置、键校验、逐键 JSON 读取/同步写入、原子发布、仅导入缺失项 |
|
||||
|
||||
文件数据以 UTF-8 JSON 保存,操作系统路径用 PathBuf;坏记录不自动覆盖。每个键独立发布,不提供多键事务。旧库导入不覆盖新文件,规则见 [宿主信息](../../../docs/operations/host-information.md)。返回 [host](../README.md)。
|
||||
@@ -1,4 +0,0 @@
|
||||
//! Host information stored in UTF-8 JSON files, independently of the VM and SQL.
|
||||
mod metadata;
|
||||
|
||||
pub use metadata::MetadataStore;
|
||||
@@ -1,172 +0,0 @@
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
static NEXT_TEMP: AtomicU64 = AtomicU64::new(0);
|
||||
const FORMAT_VERSION: u32 = 1;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LegacyRecord {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Record {
|
||||
format_version: u32,
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
/// One independently replaceable JSON file per ASCII metadata key. Clones
|
||||
/// share a directory, not a cached map; reads always observe the file system.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MetadataStore {
|
||||
directory: PathBuf,
|
||||
}
|
||||
|
||||
impl MetadataStore {
|
||||
/// Does not create the directory until the first write.
|
||||
pub fn new(directory: impl Into<PathBuf>) -> io::Result<Self> {
|
||||
let directory = directory.into();
|
||||
if directory.as_os_str().is_empty() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"metadata directory is empty",
|
||||
));
|
||||
}
|
||||
Ok(Self { directory })
|
||||
}
|
||||
|
||||
/// Preserve OS paths without requiring the environment value to be UTF-8.
|
||||
pub fn from_env() -> io::Result<Self> {
|
||||
Self::new(
|
||||
std::env::var_os("ASTER_STATE_DIR")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from("data/host")),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn directory(&self) -> &Path {
|
||||
&self.directory
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &str) -> io::Result<Option<String>> {
|
||||
let path = self.path(key)?;
|
||||
let bytes = match fs::read(path) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let record: Record = serde_json::from_slice(&bytes)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
if record.format_version != FORMAT_VERSION || record.key != key {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"metadata key or format version mismatch",
|
||||
));
|
||||
}
|
||||
Ok(Some(record.value))
|
||||
}
|
||||
|
||||
/// Writes a new sibling file, syncs it, then publishes it with rename.
|
||||
/// Different keys do not share a read/modify/write map. For the same key,
|
||||
/// the last successful rename wins. This is not a multi-key transaction.
|
||||
pub fn put(&self, key: &str, value: &str) -> io::Result<()> {
|
||||
self.store(key, value, true).map(|_| ())
|
||||
}
|
||||
|
||||
/// One-time migration input: a JSON array of key/value string records.
|
||||
/// Validate all keys first, and never overwrite an existing file record.
|
||||
pub fn import_missing(&self, reader: impl Read) -> io::Result<usize> {
|
||||
let records: Vec<LegacyRecord> = serde_json::from_reader(reader)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
for record in &records {
|
||||
self.path(&record.key)?;
|
||||
}
|
||||
let mut imported = 0;
|
||||
for record in records {
|
||||
if self.store(&record.key, &record.value, false)? {
|
||||
imported += 1;
|
||||
} else if self.get(&record.key)?.is_none() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"existing metadata disappeared during import",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(imported)
|
||||
}
|
||||
|
||||
fn store(&self, key: &str, value: &str, replace: bool) -> io::Result<bool> {
|
||||
let destination = self.path(key)?;
|
||||
let mut contents = serde_json::to_vec_pretty(&Record {
|
||||
format_version: FORMAT_VERSION,
|
||||
key: key.into(),
|
||||
value: value.into(),
|
||||
})
|
||||
.map_err(io::Error::other)?;
|
||||
contents.push(b'\n');
|
||||
fs::create_dir_all(&self.directory)?;
|
||||
for _ in 0..32 {
|
||||
let sequence = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
|
||||
let temporary = self
|
||||
.directory
|
||||
.join(format!(".{key}.{}.{sequence}.tmp", std::process::id()));
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(true).create_new(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600);
|
||||
}
|
||||
let mut file = match options.open(&temporary) {
|
||||
Ok(file) => file,
|
||||
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let result = (|| {
|
||||
file.write_all(&contents)?;
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
if replace {
|
||||
fs::rename(&temporary, &destination)?;
|
||||
Ok(true)
|
||||
} else {
|
||||
// Atomic create-if-absent: a concurrent writer's record
|
||||
// cannot be replaced by the legacy import.
|
||||
match fs::hard_link(&temporary, &destination) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => Ok(false),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
})();
|
||||
let _ = fs::remove_file(&temporary);
|
||||
return result;
|
||||
}
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::AlreadyExists,
|
||||
"cannot reserve a metadata temporary file",
|
||||
))
|
||||
}
|
||||
|
||||
fn path(&self, key: &str) -> io::Result<PathBuf> {
|
||||
if key.is_empty()
|
||||
|| key.len() > 128
|
||||
|| !key.as_bytes()[0].is_ascii_alphanumeric()
|
||||
|| !key
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
|
||||
{
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"metadata keys must start with an ASCII letter/digit and contain at most 128 ASCII letters, digits, dots, underscores or hyphens",
|
||||
));
|
||||
}
|
||||
Ok(self.directory.join(format!("{key}.json")))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
# Host 文件信息检查
|
||||
|
||||
[metadata.rs](metadata.rs) 检查缺失读取不建目录、日文/中文/emoji 值和日文路径、重开与同键替换、非法键不越界、坏 JSON/版本/键不匹配不改原文件、并发替换可见完整记录,以及旧记录幂等导入和保留更新值。
|
||||
|
||||
检查使用临时目录,仅在指定远端环境运行。本地不执行文件行为测试;当前是否通过应以对应提交的 CI 为准。命令见 [验证](../../../docs/operations/validation.md),返回 [host](../README.md)。
|
||||
@@ -1,131 +0,0 @@
|
||||
use host::MetadataStore;
|
||||
|
||||
#[test]
|
||||
fn missing_reads_do_not_create_state() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
assert_eq!(store.get("missing").unwrap(), None);
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utf8_values_survive_reopening_and_same_key_replacement() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("日本語");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
store.put("host.version", "0.1.0").unwrap();
|
||||
store.put("project.note", "日本語・中文・😀").unwrap();
|
||||
let reopened = MetadataStore::new(&directory).unwrap();
|
||||
assert_eq!(
|
||||
reopened.get("project.note").unwrap().as_deref(),
|
||||
Some("日本語・中文・😀")
|
||||
);
|
||||
reopened.put("host.version", "0.2.0").unwrap();
|
||||
assert_eq!(store.get("host.version").unwrap().as_deref(), Some("0.2.0"));
|
||||
assert_eq!(
|
||||
store.get("project.note").unwrap().as_deref(),
|
||||
Some("日本語・中文・😀")
|
||||
);
|
||||
let record: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(directory.join("project.note.json")).unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(record["format_version"], 1);
|
||||
assert_eq!(record["key"], "project.note");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_cannot_escape_the_state_directory() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
for key in [
|
||||
"",
|
||||
"../outside",
|
||||
"/outside",
|
||||
"nested/key",
|
||||
"nested\\key",
|
||||
".hidden",
|
||||
"日本語",
|
||||
] {
|
||||
assert!(store.put(key, "invalid").is_err(), "{key}");
|
||||
assert!(store.get(key).is_err(), "{key}");
|
||||
}
|
||||
assert!(store.put(&"x".repeat(129), "invalid").is_err());
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_or_mismatched_records_fail_without_being_rewritten() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
let path = temporary.path().join("host.version.json");
|
||||
for bytes in [
|
||||
b"not-json".as_slice(),
|
||||
b"\xff".as_slice(),
|
||||
br#"{"format_version":2,"key":"host.version","value":"old"}"#.as_slice(),
|
||||
br#"{"format_version":1,"key":"wrong","value":"old"}"#.as_slice(),
|
||||
] {
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
assert!(store.get("host.version").is_err());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), bytes);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_replacements_expose_complete_records_and_preserve_other_keys() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
store.put("independent", "keep").unwrap();
|
||||
let a = "a".repeat(8192);
|
||||
let b = "b".repeat(8192);
|
||||
store.put("shared", &a).unwrap();
|
||||
std::thread::scope(|scope| {
|
||||
for value in [&a, &b] {
|
||||
let store = &store;
|
||||
scope.spawn(move || {
|
||||
for _ in 0..8 {
|
||||
store.put("shared", value).unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
for _ in 0..64 {
|
||||
let value = store.get("shared").unwrap().unwrap();
|
||||
assert!(value == a || value == b);
|
||||
}
|
||||
});
|
||||
assert_eq!(store.get("independent").unwrap().as_deref(), Some("keep"));
|
||||
assert!(std::fs::read_dir(temporary.path()).unwrap().all(|entry| {
|
||||
!entry
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.ends_with(".tmp")
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_import_is_repeatable_and_preserves_newer_file_values() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
store.put("host.version", "new").unwrap();
|
||||
let input =
|
||||
br#"[{"key":"host.version","value":"old"},{"key":"project.note","value":"legacy"}]"#;
|
||||
assert_eq!(store.import_missing(input.as_slice()).unwrap(), 1);
|
||||
assert_eq!(store.import_missing(input.as_slice()).unwrap(), 0);
|
||||
assert_eq!(store.get("host.version").unwrap().as_deref(), Some("new"));
|
||||
assert_eq!(
|
||||
store.get("project.note").unwrap().as_deref(),
|
||||
Some("legacy")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_import_keys_are_rejected_before_any_record_is_written() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
let input = br#"[{"key":"valid","value":"keep"},{"key":"../outside","value":"invalid"}]"#;
|
||||
assert!(store.import_missing(input.as_slice()).is_err());
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
[package]
|
||||
name = "server"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
core.workspace = true
|
||||
host.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -1,10 +0,0 @@
|
||||
# server
|
||||
|
||||
最小常驻 HTTP 宿主,提供存活检查和只读 VM 信息。通过 Arc 共享 Vm,启动不读取数据库配置、不连接或写入数据库;通过 host 包把 host.version 写入文件。特殊类的数据库能力尚未接入 HTTP 宿主。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | core/host、Axum、Tokio、Serde 依赖;无 storage 依赖 |
|
||||
| [src](src/README.md) | AppState、启动、路由、信号与退出 |
|
||||
|
||||
HTTP 返回内容见 [运行指南](../../docs/operations/README.md),监听变量见 [配置](../../docs/operations/configuration.md),预置 PostgreSQL 资源见 [部署](../../docs/operations/deployment.md)。没有语言执行接口或可变 VM 请求上下文。返回 [工作区](../README.md)。
|
||||
@@ -1,13 +0,0 @@
|
||||
# Server 入口
|
||||
|
||||
[main.rs](main.rs) 包含所有宿主启动代码:
|
||||
|
||||
| 部分 | 职责 |
|
||||
| --- | --- |
|
||||
| AppState | 共享只读 Arc<Vm> |
|
||||
| main/run | 创建空 VM、绑定地址、设置退出信号、写文件 host.version、组装路由;不创建数据库连接 |
|
||||
| health | 返回 200 和 status=ok,仅检查 HTTP 宿主可响应 |
|
||||
| bootstrap | 返回对象模型、type_system=false 和堆对象数量,不分配对象 |
|
||||
| shutdown_signal | Ctrl-C;Unix 上同时接受 SIGTERM,触发 Axum 优雅退出 |
|
||||
|
||||
/health 不表示 PostgreSQL、特殊类查询或执行脚本正常;数据库检查应属于实际消费连接的对象。宿主独立于数据库的行为由远端 smoke.py 验证,见 [验证](../../../docs/operations/validation.md)。改变端点同步 [运行指南](../../../docs/operations/README.md) 和 [scripts](../../../scripts/README.md)。返回 [server](../README.md)。
|
||||
@@ -1,77 +0,0 @@
|
||||
use std::error::Error;
|
||||
use std::future::Future;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{Json, Router, extract::State, routing::get};
|
||||
use endor_core::Vm;
|
||||
use host::MetadataStore;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
vm: Arc<Vm>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Health {
|
||||
status: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct BootstrapInfo {
|
||||
object_model: &'static str,
|
||||
type_system: bool,
|
||||
heap_objects: usize,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ExitCode {
|
||||
match run().await {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("server: {error}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Box<dyn Error>> {
|
||||
let vm = Arc::new(Vm::new()?);
|
||||
let address = std::env::var("ASTER_LISTEN").unwrap_or_else(|_| "127.0.0.1:8080".into());
|
||||
let listener = tokio::net::TcpListener::bind(&address).await?;
|
||||
let shutdown = shutdown_signal()?;
|
||||
MetadataStore::from_env()?.put("host.version", env!("CARGO_PKG_VERSION"))?;
|
||||
let app = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/bootstrap", get(bootstrap))
|
||||
.with_state(AppState { vm });
|
||||
println!("Endor host listening on {}", listener.local_addr()?);
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn health() -> Json<Health> {
|
||||
Json(Health { status: "ok" })
|
||||
}
|
||||
|
||||
async fn bootstrap(State(state): State<AppState>) -> Json<BootstrapInfo> {
|
||||
Json(BootstrapInfo {
|
||||
object_model: "EndorObject",
|
||||
type_system: false,
|
||||
heap_objects: state.vm.object_count(),
|
||||
})
|
||||
}
|
||||
|
||||
fn shutdown_signal() -> std::io::Result<impl Future<Output = ()>> {
|
||||
#[cfg(unix)]
|
||||
let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
|
||||
Ok(async move {
|
||||
#[cfg(unix)]
|
||||
tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
|
||||
#[cfg(not(unix))]
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
[package]
|
||||
name = "storage"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
sqlx.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -1,11 +0,0 @@
|
||||
# storage
|
||||
|
||||
按需数据库连接适配层,供将来需要数据库的特殊类或宿主适配器使用。依赖 SQLx/Tokio,不依赖 VM 内核;普通对象、CLI 和 Server 不连接这层。
|
||||
|
||||
| 内容 | 入口 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | SQLx、Tokio 与远端检查依赖 |
|
||||
| [src](src/README.md) | 显式 DatabaseConfig、连接池与关闭 |
|
||||
| [tests](tests/README.md) | 连接不建表、调用方 SQL、隔离/持久化、真实 PostgreSQL |
|
||||
|
||||
没有默认数据库、自动环境变量读取、基础信息表或通用元数据 put/get。特殊类的名称、布局与查询接口尚未定义;当前只保留连接能力。设计边界见 [数据库对象](../../docs/runtime/database-objects.md),参数见 [配置](../../docs/operations/configuration.md)。返回 [工作区](../README.md)。
|
||||
@@ -1,7 +0,0 @@
|
||||
# 数据库连接源码导航
|
||||
|
||||
[lib.rs](lib.rs) 是唯一实现文件:DatabaseConfig 显式选择 SQLite 文件/内存或 PostgreSQL;DatabaseConnection 提供 connect、sqlite_pool/postgres_pool、backend_name、ping 和 close;DatabaseError 对公开错误脱敏。
|
||||
|
||||
connect 只建立连接池,不执行应用 DDL,不保存基础信息。没有 schema.sql、初始化表、put/get 或 from_env。业务表和查询由未来的数据库特殊类决定;本层不替它定义数据模型。
|
||||
|
||||
克隆共享连接池,close 会关闭所有克隆;具体生命周期及限制见 [数据库对象](../../../docs/runtime/database-objects.md)。配置变动同步 [配置](../../../docs/operations/configuration.md) 和 [tests](../tests/README.md)。返回 [storage](../README.md)。
|
||||
@@ -1,181 +0,0 @@
|
||||
//! Explicit database connections for database-backed objects.
|
||||
//! Connecting never creates application tables or stores VM/host metadata.
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
|
||||
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
|
||||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
|
||||
use sqlx::{PgPool, SqlitePool};
|
||||
|
||||
/// An explicit PostgreSQL configuration never falls back to SQLite.
|
||||
#[derive(Clone)]
|
||||
pub enum DatabaseConfig {
|
||||
SqliteFile(PathBuf),
|
||||
SqliteMemory,
|
||||
Postgres(String),
|
||||
}
|
||||
|
||||
impl fmt::Debug for DatabaseConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::SqliteFile(path) => f.debug_tuple("SqliteFile").field(path).finish(),
|
||||
Self::SqliteMemory => f.write_str("SqliteMemory"),
|
||||
Self::Postgres(_) => f.write_str("Postgres([REDACTED])"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DatabaseError {
|
||||
Config(&'static str),
|
||||
Directory(std::io::Error),
|
||||
Database(sqlx::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for DatabaseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Config(message) => f.write_str(message),
|
||||
Self::Directory(_) => f.write_str("cannot create SQLite parent directory"),
|
||||
// CLI/HTTP logs must never print a connection URL or credentials.
|
||||
Self::Database(_) => {
|
||||
f.write_str("database operation failed; no fallback was attempted")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for DatabaseError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Config(_) => None,
|
||||
Self::Directory(error) => Some(error),
|
||||
Self::Database(error) => Some(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for DatabaseError {
|
||||
fn from(error: sqlx::Error) -> Self {
|
||||
Self::Database(error)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Backend {
|
||||
Sqlite(SqlitePool),
|
||||
Postgres(PgPool),
|
||||
}
|
||||
|
||||
/// Created only on explicit request by an object or host adapter. Clones share
|
||||
/// a pool; close() closes it for all users. There is no automatic schema or data.
|
||||
/// In-memory SQLite uses one connection to preserve its database lifetime.
|
||||
#[derive(Clone)]
|
||||
pub struct DatabaseConnection {
|
||||
backend: Backend,
|
||||
}
|
||||
|
||||
impl DatabaseConnection {
|
||||
/// Connects eagerly to the explicit backend without executing application DDL.
|
||||
pub async fn connect(config: &DatabaseConfig) -> Result<Self, DatabaseError> {
|
||||
let backend = match config {
|
||||
DatabaseConfig::Postgres(url) => {
|
||||
if !(url.starts_with("postgres://") || url.starts_with("postgresql://")) {
|
||||
return Err(DatabaseError::Config(
|
||||
"expected a PostgreSQL connection URL",
|
||||
));
|
||||
}
|
||||
let options = PgConnectOptions::from_str(url)
|
||||
.map_err(|_| DatabaseError::Config("invalid PostgreSQL connection URL"))?;
|
||||
Backend::Postgres(
|
||||
PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.acquire_timeout(Duration::from_secs(5))
|
||||
.connect_with(options)
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
DatabaseConfig::SqliteFile(path) => {
|
||||
if path.as_os_str().is_empty() || path == std::path::Path::new(":memory:") {
|
||||
return Err(DatabaseError::Config(
|
||||
"expected a nonempty file path; use SqliteMemory for :memory:",
|
||||
));
|
||||
}
|
||||
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
|
||||
tokio::fs::create_dir_all(parent)
|
||||
.await
|
||||
.map_err(DatabaseError::Directory)?;
|
||||
}
|
||||
let options = SqliteConnectOptions::new()
|
||||
.filename(path)
|
||||
.create_if_missing(true)
|
||||
.journal_mode(SqliteJournalMode::Wal)
|
||||
.busy_timeout(Duration::from_secs(5));
|
||||
Backend::Sqlite(
|
||||
SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.acquire_timeout(Duration::from_secs(5))
|
||||
.connect_with(options)
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
DatabaseConfig::SqliteMemory => {
|
||||
let options = SqliteConnectOptions::new().in_memory(true);
|
||||
Backend::Sqlite(
|
||||
SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.min_connections(1)
|
||||
.idle_timeout(None)
|
||||
.max_lifetime(None)
|
||||
.connect_with(options)
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
};
|
||||
Ok(Self { backend })
|
||||
}
|
||||
|
||||
/// Backend-specific access for the database-backed object implementation.
|
||||
pub fn sqlite_pool(&self) -> Option<&SqlitePool> {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => Some(pool),
|
||||
Backend::Postgres(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn postgres_pool(&self) -> Option<&PgPool> {
|
||||
match &self.backend {
|
||||
Backend::Postgres(pool) => Some(pool),
|
||||
Backend::Sqlite(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend_name(&self) -> &'static str {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(_) => "sqlite",
|
||||
Backend::Postgres(_) => "postgresql",
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn ping(&self) -> Result<(), DatabaseError> {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => {
|
||||
sqlx::query("SELECT 1").execute(pool).await?;
|
||||
}
|
||||
Backend::Postgres(pool) => {
|
||||
sqlx::query("SELECT 1").execute(pool).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Explicitly closes the shared pool, including all DatabaseConnection clones.
|
||||
pub async fn close(&self) {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => pool.close().await,
|
||||
Backend::Postgres(pool) => pool.close().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
# 数据库连接检查
|
||||
|
||||
| 文件 | 覆盖与前提 |
|
||||
| --- | --- |
|
||||
| [storage.rs](storage.rs) | 显式错误配置、凭据脱敏、SQLite 连接后无应用表、克隆共享与独立内存隔离、调用方自建数据跨文件重连、目录错误、PostgreSQL 失败不降级 |
|
||||
| [postgres.rs](postgres.rs) | 真实连接后 public 无表;测试在事务中创建临时 class_rows、参数绑定和读取,提交自动删除临时表,再连接;默认 ignore,需全新专用 ASTER_TEST_POSTGRES_URL |
|
||||
|
||||
所有业务表都由测试自己创建,连接层不得自动初始化基础信息表。临时 SQLite 文件和 PostgreSQL 测试库由远端 CI 隔离,不使用生产数据。检查命令见 [验证](../../../docs/operations/validation.md),原生连接能力边界见 [数据库对象](../../../docs/runtime/database-objects.md)。本地不运行测试或下载依赖。返回 [storage](../README.md)。
|
||||
@@ -1,53 +0,0 @@
|
||||
use storage::{DatabaseConfig, DatabaseConnection};
|
||||
|
||||
/// Run against a fresh disposable database, never a production or shared schema.
|
||||
#[tokio::test]
|
||||
#[ignore = "requires ASTER_TEST_POSTGRES_URL pointing to a fresh disposable PostgreSQL database"]
|
||||
async fn postgres_connects_without_schema_and_executes_caller_owned_sql() {
|
||||
let url = std::env::var("ASTER_TEST_POSTGRES_URL")
|
||||
.expect("set ASTER_TEST_POSTGRES_URL to a fresh disposable PostgreSQL database");
|
||||
let config = DatabaseConfig::Postgres(url);
|
||||
let database = DatabaseConnection::connect(&config).await.unwrap();
|
||||
assert_eq!(database.backend_name(), "postgresql");
|
||||
assert!(database.sqlite_pool().is_none());
|
||||
database.ping().await.unwrap();
|
||||
let pool = database.postgres_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0, "connecting must not create application tables");
|
||||
let mut transaction = pool.begin().await.unwrap();
|
||||
sqlx::query(
|
||||
"CREATE TEMP TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL) ON COMMIT DROP",
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES ($1, $2)")
|
||||
.bind(1_i64)
|
||||
.bind("你好 ' ; $1")
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = $1")
|
||||
.bind(1_i64)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "你好 ' ; $1");
|
||||
transaction.commit().await.unwrap();
|
||||
database.close().await;
|
||||
let reopened = DatabaseConnection::connect(&config).await.unwrap();
|
||||
reopened.ping().await.unwrap();
|
||||
let tables: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'",
|
||||
)
|
||||
.fetch_one(reopened.postgres_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0);
|
||||
reopened.close().await;
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
use storage::{DatabaseConfig, DatabaseConnection};
|
||||
|
||||
#[test]
|
||||
fn postgres_configuration_debug_redacts_credentials() {
|
||||
let config = DatabaseConfig::Postgres("postgres://user:secret@localhost/db".into());
|
||||
assert!(!format!("{config:?}").contains("secret"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn explicit_invalid_backends_fail_without_fallback() {
|
||||
for config in [
|
||||
DatabaseConfig::Postgres(String::new()),
|
||||
DatabaseConfig::Postgres("mysql://localhost/db".into()),
|
||||
DatabaseConfig::SqliteFile("".into()),
|
||||
DatabaseConfig::SqliteFile(":memory:".into()),
|
||||
] {
|
||||
assert!(DatabaseConnection::connect(&config).await.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn memory_connections_start_without_tables_and_clones_share_the_pool() {
|
||||
let database = DatabaseConnection::connect(&DatabaseConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
let other = DatabaseConnection::connect(&DatabaseConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(database.postgres_pool().is_none());
|
||||
let pool = database.sqlite_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0, "connecting must not create application tables");
|
||||
// This schema belongs to the test's simulated database-backed object.
|
||||
sqlx::query("CREATE TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES (?, ?)")
|
||||
.bind(1_i64)
|
||||
.bind("你好 ' ? $1 ;")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let cloned = database.clone();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = ?")
|
||||
.bind(1_i64)
|
||||
.fetch_one(cloned.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "你好 ' ? $1 ;");
|
||||
let other_tables: i64 =
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(other.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(other_tables, 0);
|
||||
database.close().await;
|
||||
assert!(cloned.ping().await.is_err());
|
||||
other.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn explicit_sqlite_file_preserves_only_caller_created_data_across_reconnects() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("nested/data/class.db");
|
||||
let config = DatabaseConfig::SqliteFile(path.clone());
|
||||
let database = DatabaseConnection::connect(&config).await.unwrap();
|
||||
let pool = database.sqlite_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0);
|
||||
sqlx::query("CREATE TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES (?, ?)")
|
||||
.bind(1_i64)
|
||||
.bind("caller-owned data")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
database.close().await;
|
||||
assert!(path.is_file());
|
||||
let reopened = DatabaseConnection::connect(&config).await.unwrap();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = ?")
|
||||
.bind(1_i64)
|
||||
.fetch_one(reopened.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "caller-owned data");
|
||||
let tables: Vec<String> =
|
||||
sqlx::query_scalar("SELECT name FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_all(reopened.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, vec!["class_rows"]);
|
||||
reopened.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_sqlite_directory_reports_error() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let file = dir.path().join("file");
|
||||
std::fs::write(&file, "occupied").unwrap();
|
||||
assert!(
|
||||
DatabaseConnection::connect(&DatabaseConfig::SqliteFile(file.join("class.db")))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn postgres_connection_failure_is_an_error() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!(
|
||||
"postgres://aster:secret@{}/aster?sslmode=disable",
|
||||
listener.local_addr().unwrap()
|
||||
);
|
||||
let error = match DatabaseConnection::connect(&DatabaseConfig::Postgres(url)).await {
|
||||
Ok(_) => panic!("unreachable PostgreSQL must fail"),
|
||||
Err(error) => error,
|
||||
};
|
||||
assert!(!error.to_string().contains("secret"));
|
||||
assert!(error.to_string().contains("no fallback"));
|
||||
}
|
||||
Reference in New Issue
Block a user