mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-11-25 05:22:52 +00:00
dev: split test functions for multiple crates (#1695)
* dev: split test functions for multiple crates * build: update cargo.lock * fix: compile error
This commit is contained in:
parent
1108b39e3f
commit
647cda29a0
18 changed files with 212 additions and 165 deletions
|
|
@ -57,10 +57,10 @@ walkdir.workspace = true
|
|||
yaml-rust2.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
insta.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
typst-assets = { workspace = true, features = ["fonts"] }
|
||||
tinymist-tests = { workspace = true }
|
||||
sha2 = { version = "0.10" }
|
||||
hex = { version = "0.4" }
|
||||
|
||||
|
|
|
|||
|
|
@ -33,19 +33,16 @@ mod prelude;
|
|||
mod global;
|
||||
pub use global::*;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ecow::{eco_format, EcoVec};
|
||||
use ecow::eco_format;
|
||||
use lsp_types::Url;
|
||||
use tinymist_project::LspComputeGraph;
|
||||
use tinymist_std::{bail, ImmutPath, Result};
|
||||
use tinymist_world::vfs::WorkspaceResolver;
|
||||
use tinymist_world::{EntryReader, TaskInputs, WorldDeps};
|
||||
use tinymist_std::{bail, Result};
|
||||
use tinymist_world::{EntryReader, TaskInputs};
|
||||
use typst::diag::{FileError, FileResult};
|
||||
use typst::foundations::{Func, Value};
|
||||
use typst::syntax::{FileId, Source};
|
||||
use typst::World;
|
||||
use typst::syntax::FileId;
|
||||
|
||||
use crate::{path_res_to_url, CompilerQueryResponse, SemanticRequest, StatefulRequest};
|
||||
|
||||
|
|
@ -65,65 +62,17 @@ impl ToFunc for Value {
|
|||
|
||||
/// Extension trait for `typst::World`.
|
||||
pub trait LspWorldExt {
|
||||
/// Get file's id by its path
|
||||
fn file_id_by_path(&self, path: &Path) -> FileResult<FileId>;
|
||||
|
||||
/// Get the source of a file by file path.
|
||||
fn source_by_path(&self, path: &Path) -> FileResult<Source>;
|
||||
|
||||
/// Resolve the uri for a file id.
|
||||
fn uri_for_id(&self, fid: FileId) -> FileResult<Url>;
|
||||
|
||||
/// Get all depended file ids of a compilation, inclusively.
|
||||
/// Note: must be called after compilation.
|
||||
fn depended_files(&self) -> EcoVec<FileId>;
|
||||
|
||||
/// Get all depended paths in file system of a compilation, inclusively.
|
||||
/// Note: must be called after compilation.
|
||||
fn depended_fs_paths(&self) -> EcoVec<ImmutPath>;
|
||||
}
|
||||
|
||||
impl LspWorldExt for tinymist_project::LspWorld {
|
||||
fn file_id_by_path(&self, path: &Path) -> FileResult<FileId> {
|
||||
// todo: source in packages
|
||||
match self.id_for_path(path) {
|
||||
Some(id) => Ok(id),
|
||||
None => WorkspaceResolver::file_with_parent_root(path).ok_or_else(|| {
|
||||
let reason = eco_format!("invalid path: {path:?}");
|
||||
FileError::Other(Some(reason))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn source_by_path(&self, path: &Path) -> FileResult<Source> {
|
||||
// todo: source cache
|
||||
self.source(self.file_id_by_path(path)?)
|
||||
}
|
||||
|
||||
fn uri_for_id(&self, fid: FileId) -> Result<Url, FileError> {
|
||||
let res = path_res_to_url(self.path_for_id(fid)?);
|
||||
|
||||
crate::log_debug_ct!("uri_for_id: {fid:?} -> {res:?}");
|
||||
res.map_err(|err| FileError::Other(Some(eco_format!("convert to url: {err:?}"))))
|
||||
}
|
||||
|
||||
fn depended_files(&self) -> EcoVec<FileId> {
|
||||
let mut deps = EcoVec::new();
|
||||
self.iter_dependencies(&mut |file_id| {
|
||||
deps.push(file_id);
|
||||
});
|
||||
deps
|
||||
}
|
||||
|
||||
fn depended_fs_paths(&self) -> EcoVec<ImmutPath> {
|
||||
let mut deps = EcoVec::new();
|
||||
self.iter_dependencies(&mut |file_id| {
|
||||
if let Ok(path) = self.path_for_id(file_id) {
|
||||
deps.push(path.as_path().into());
|
||||
}
|
||||
});
|
||||
deps
|
||||
}
|
||||
}
|
||||
|
||||
/// A snapshot for LSP queries.
|
||||
|
|
@ -448,7 +397,6 @@ mod type_check_tests {
|
|||
#[cfg(test)]
|
||||
mod post_type_check_tests {
|
||||
|
||||
use insta::with_settings;
|
||||
use typst::syntax::LinkedNode;
|
||||
use typst_shim::syntax::LinkedNodeExt;
|
||||
|
||||
|
|
@ -484,7 +432,6 @@ mod post_type_check_tests {
|
|||
#[cfg(test)]
|
||||
mod type_describe_tests {
|
||||
|
||||
use insta::with_settings;
|
||||
use typst::syntax::LinkedNode;
|
||||
use typst_shim::syntax::LinkedNodeExt;
|
||||
|
||||
|
|
|
|||
|
|
@ -213,6 +213,8 @@ mod tests {
|
|||
|
||||
use typst::syntax::{FileId, VirtualPath};
|
||||
|
||||
use crate::tests::*;
|
||||
|
||||
// This is a workaround for slashes in the path on Windows and Linux
|
||||
// are different
|
||||
fn bib_snap(snap: &impl fmt::Debug) -> String {
|
||||
|
|
@ -234,8 +236,8 @@ Euclid2:
|
|||
FileId::new_fake(VirtualPath::new(Path::new("test.yml"))),
|
||||
);
|
||||
assert_eq!(bib.entries.len(), 2);
|
||||
insta::assert_snapshot!(bib_snap(&bib.entries[0]), @r###"("Euclid", BibEntry { file_id: /test.yml, name_range: 1..7, range: 1..63, raw_entry: None })"###);
|
||||
insta::assert_snapshot!(bib_snap(&bib.entries[1]), @r###"("Euclid2", BibEntry { file_id: /test.yml, name_range: 63..70, range: 63..126, raw_entry: None })"###);
|
||||
assert_snapshot!(bib_snap(&bib.entries[0]), @r###"("Euclid", BibEntry { file_id: /test.yml, name_range: 1..7, range: 1..63, raw_entry: None })"###);
|
||||
assert_snapshot!(bib_snap(&bib.entries[1]), @r###"("Euclid2", BibEntry { file_id: /test.yml, name_range: 63..70, range: 63..126, raw_entry: None })"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ mod tests {
|
|||
|
||||
let result = request.request(ctx);
|
||||
|
||||
insta::with_settings!({
|
||||
with_settings!({
|
||||
description => format!("Code Action on {})", make_range_annoation(&source)),
|
||||
}, {
|
||||
assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
|
||||
|
|
|
|||
|
|
@ -100,8 +100,6 @@ impl StatefulRequest for CompletionRequest {
|
|||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use insta::with_settings;
|
||||
|
||||
use super::*;
|
||||
use crate::{completion::proto::CompletionItem, syntax::find_module_level_docs, tests::*};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use tinymist_project::LspWorld;
|
|||
use tinymist_world::vfs::WorkspaceResolver;
|
||||
use typst::syntax::Span;
|
||||
|
||||
use crate::{analysis::Analysis, prelude::*, LspWorldExt};
|
||||
use crate::{analysis::Analysis, prelude::*};
|
||||
|
||||
use regex::RegexSet;
|
||||
|
||||
|
|
|
|||
|
|
@ -249,7 +249,7 @@ mod tests {
|
|||
})
|
||||
.join("\n");
|
||||
|
||||
insta::with_settings!({
|
||||
with_settings!({
|
||||
description => format!("Jump cursor on {})", make_range_annoation(&source)),
|
||||
}, {
|
||||
assert_snapshot!(results);
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ use tinymist_world::vfs::PathResolution;
|
|||
use crate::prelude::*;
|
||||
|
||||
/// An LSP Position encoded by [`PositionEncoding`].
|
||||
pub type LspPosition = lsp_types::Position;
|
||||
pub use tinymist_analysis::location::LspPosition;
|
||||
/// An LSP range encoded by [`PositionEncoding`].
|
||||
pub type LspRange = lsp_types::Range;
|
||||
pub use tinymist_analysis::location::LspRange;
|
||||
|
||||
pub use tinymist_analysis::location::*;
|
||||
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ mod tests {
|
|||
format!("{window_before}|{window_line}|{window_after}")
|
||||
};
|
||||
|
||||
insta::with_settings!({
|
||||
with_settings!({
|
||||
description => format!("On Enter on {annotated})"),
|
||||
}, {
|
||||
assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
|
||||
|
|
|
|||
|
|
@ -9,45 +9,30 @@ use std::{
|
|||
};
|
||||
|
||||
use serde_json::{ser::PrettyFormatter, Serializer, Value};
|
||||
use tinymist_project::{CompileFontArgs, ExportTarget, LspCompileSnapshot, LspComputeGraph};
|
||||
use tinymist_project::{LspCompileSnapshot, LspComputeGraph};
|
||||
use tinymist_std::path::unix_slash;
|
||||
use tinymist_std::typst::TypstDocument;
|
||||
use tinymist_world::debug_loc::LspRange;
|
||||
use tinymist_world::package::PackageSpec;
|
||||
use tinymist_world::vfs::WorkspaceResolver;
|
||||
use tinymist_world::{EntryManager, EntryReader, EntryState, ShadowApi, TaskInputs};
|
||||
use typst::foundations::Bytes;
|
||||
use tinymist_world::{EntryReader, ShadowApi, TaskInputs};
|
||||
use typst::syntax::ast::{self, AstNode};
|
||||
use typst::syntax::{LinkedNode, Source, SyntaxKind, VirtualPath};
|
||||
use typst_shim::syntax::LinkedNodeExt;
|
||||
|
||||
pub use crate::syntax::find_module_level_docs;
|
||||
pub use insta::assert_snapshot;
|
||||
pub use serde::Serialize;
|
||||
pub use serde_json::json;
|
||||
pub use tinymist_project::{LspUniverse, LspUniverseBuilder};
|
||||
pub use tinymist_project::LspUniverse;
|
||||
pub use tinymist_tests::{assert_snapshot, run_with_sources, with_settings};
|
||||
pub use tinymist_world::WorldComputeGraph;
|
||||
|
||||
pub use crate::syntax::find_module_level_docs;
|
||||
use crate::{analysis::Analysis, prelude::LocalContext, LspPosition, PositionEncoding};
|
||||
use crate::{to_lsp_position, CompletionFeat, LspWorldExt};
|
||||
use crate::{to_lsp_position, CompletionFeat};
|
||||
|
||||
pub fn snapshot_testing(name: &str, f: &impl Fn(&mut LocalContext, PathBuf)) {
|
||||
let name = if name.is_empty() { "playground" } else { name };
|
||||
|
||||
let mut settings = insta::Settings::new();
|
||||
settings.set_prepend_module_to_snapshot(false);
|
||||
settings.set_snapshot_path(format!("fixtures/{name}/snaps"));
|
||||
settings.bind(|| {
|
||||
let glob_path = format!("fixtures/{name}/*.typ");
|
||||
insta::glob!(&glob_path, |path| {
|
||||
let contents = std::fs::read_to_string(path).unwrap();
|
||||
#[cfg(windows)]
|
||||
let contents = contents.replace("\r\n", "\n");
|
||||
|
||||
run_with_sources(&contents, |verse, path| {
|
||||
run_with_ctx(verse, path, f);
|
||||
});
|
||||
});
|
||||
tinymist_tests::snapshot_testing!(name, |verse, path| {
|
||||
run_with_ctx(verse, path, f);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -143,64 +128,6 @@ pub fn compile_doc_for_test(
|
|||
WorldComputeGraph::new(snap)
|
||||
}
|
||||
|
||||
pub fn run_with_sources<T>(source: &str, f: impl FnOnce(&mut LspUniverse, PathBuf) -> T) -> T {
|
||||
let root = if cfg!(windows) {
|
||||
PathBuf::from("C:\\root")
|
||||
} else {
|
||||
PathBuf::from("/root")
|
||||
};
|
||||
let mut verse = LspUniverseBuilder::build(
|
||||
EntryState::new_rooted(root.as_path().into(), None),
|
||||
ExportTarget::Paged,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
LspUniverseBuilder::resolve_package(None, None),
|
||||
Arc::new(
|
||||
LspUniverseBuilder::resolve_fonts(CompileFontArgs {
|
||||
ignore_system_fonts: true,
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap(),
|
||||
),
|
||||
);
|
||||
let sources = source.split("-----");
|
||||
|
||||
let mut last_pw = None;
|
||||
for (idx, source) in sources.enumerate() {
|
||||
// find prelude
|
||||
let mut source = source.trim_start();
|
||||
let mut path = None;
|
||||
|
||||
if source.starts_with("//") {
|
||||
let first_line = source.lines().next().unwrap();
|
||||
let content = first_line.trim_start_matches("/").trim();
|
||||
|
||||
if let Some(path_attr) = content.strip_prefix("path:") {
|
||||
source = source.strip_prefix(first_line).unwrap().trim();
|
||||
path = Some(path_attr.trim().to_owned())
|
||||
}
|
||||
};
|
||||
|
||||
let path = path.unwrap_or_else(|| format!("/s{idx}.typ"));
|
||||
let path = path.strip_prefix("/").unwrap_or(path.as_str());
|
||||
|
||||
let pw = root.join(Path::new(&path));
|
||||
verse
|
||||
.map_shadow(&pw, Bytes::from_string(source.to_owned()))
|
||||
.unwrap();
|
||||
last_pw = Some(pw);
|
||||
}
|
||||
|
||||
let pw = last_pw.unwrap();
|
||||
verse
|
||||
.mutate_entry(EntryState::new_rooted(
|
||||
root.as_path().into(),
|
||||
Some(VirtualPath::new(pw.strip_prefix(root).unwrap())),
|
||||
))
|
||||
.unwrap();
|
||||
f(&mut verse, pw)
|
||||
}
|
||||
|
||||
pub fn find_test_range(s: &Source) -> LspRange {
|
||||
let range = find_test_range_(s);
|
||||
crate::to_lsp_range(range, s, PositionEncoding::Utf16)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue