From 46921a8c283718402322d4d09c0bd1d9194278b1 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 3 Nov 2022 16:13:35 +0100 Subject: [PATCH] Separate test crate This removes the not-really-cyclic dependency that confuses rust-analyzer. See also: https://github.com/rust-lang/rust-analyzer/issues/2414 --- .github/workflows/rust.yml | 11 +----- .gitignore | 1 + Cargo.lock | 20 ++++++++-- Cargo.toml | 20 +++------- cli/Cargo.toml | 3 ++ library/Cargo.toml | 5 +++ macros/Cargo.toml | 3 ++ macros/src/lib.rs | 2 + tests/Cargo.toml | 27 +++++++++++++ tests/README.md | 45 ++++++++++++++-------- benches/oneshot.rs => tests/src/benches.rs | 5 ++- tests/{typeset.rs => src/tests.rs} | 16 ++++---- {benches => tests/typ/benches}/bench.typ | 0 13 files changed, 104 insertions(+), 54 deletions(-) create mode 100644 tests/Cargo.toml rename benches/oneshot.rs => tests/src/benches.rs (96%) rename tests/{typeset.rs => src/tests.rs} (98%) rename {benches => tests/typ/benches}/bench.typ (100%) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c7dc91bd7..db77d1549 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,13 +20,6 @@ jobs: - name: Checkout source code uses: actions/checkout@v2 - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: ${{ matrix.rust }} - override: true - - name: Dependency cache uses: Swatinem/rust-cache@v1 @@ -34,10 +27,10 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --all-features + args: --all - name: Test uses: actions-rs/cargo@v1 with: command: test - args: --all-features + args: --all diff --git a/.gitignore b/.gitignore index 2cb6e97df..72f0aea0f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ desktop.ini # Tests and benchmarks tests/png tests/pdf +tests/target tarpaulin-report.html # Rust diff --git a/Cargo.lock b/Cargo.lock index aabc95e1a..23e40dfeb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1111,9 +1111,7 @@ dependencies = [ "bitflags", "bytemuck", "comemo", - "elsa", "flate2", - "iai", "image", "miniz_oxide", "once_cell", @@ -1131,13 +1129,11 @@ dependencies = [ "syntect", "tiny-skia", "ttf-parser 0.17.1", - "typst-library", "typst-macros", "unicode-segmentation", "unicode-xid", "unscanny", "usvg", - "walkdir", ] [[package]] @@ -1194,6 +1190,22 @@ dependencies = [ "syn", ] +[[package]] +name = "typst-tests" +version = "0.1.0" +dependencies = [ + "comemo", + "elsa", + "iai", + "once_cell", + "tiny-skia", + "ttf-parser 0.17.1", + "typst", + "typst-library", + "unscanny", + "walkdir", +] + [[package]] name = "unicode-bidi" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 0e9648771..fba8f5b3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,11 @@ authors = ["The Typst Project Developers"] edition = "2021" [workspace] -members = ["cli", "library", "macros"] +members = ["cli", "library", "macros", "tests"] + +[lib] +doctest = false +bench = false [dependencies] typst-macros = { path = "macros" } @@ -35,22 +39,8 @@ unicode-xid = "0.2" unscanny = "0.1" usvg = { version = "0.22", default-features = false } -[dev-dependencies] -typst-library = { path = "library" } -iai = { git = "https://github.com/reknih/iai" } -elsa = "1.7" -walkdir = "2" - [profile.dev] debug = 0 [profile.dev.package."*"] opt-level = 2 - -[[test]] -name = "typeset" -harness = false - -[[bench]] -name = "oneshot" -harness = false diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5e3f227ad..8dd45bd6c 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" [[bin]] name = "typst" path = "src/main.rs" +test = false +doctest = false +bench = false doc = false [dependencies] diff --git a/library/Cargo.toml b/library/Cargo.toml index 18f4779c4..75cb8efc7 100644 --- a/library/Cargo.toml +++ b/library/Cargo.toml @@ -4,6 +4,11 @@ version = "0.1.0" authors = ["The Typst Project Developers"] edition = "2021" +[lib] +test = false +doctest = false +bench = false + [dependencies] typst = { path = ".." } comemo = "0.1" diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 6a7f55224..20ca02629 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [lib] proc-macro = true +test = false +doctest = false +bench = false [dependencies] proc-macro2 = "1" diff --git a/macros/src/lib.rs b/macros/src/lib.rs index d3b3b30ad..e57d758e9 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,3 +1,5 @@ +//! Procedural macros for Typst. + extern crate proc_macro; use proc_macro::TokenStream; diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 000000000..cf77ca152 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "typst-tests" +version = "0.1.0" +authors = ["The Typst Project Developers"] +edition = "2021" + +[dev-dependencies] +typst = { path = ".." } +typst-library = { path = "../library" } +comemo = "0.1" +elsa = "1.7" +iai = { git = "https://github.com/reknih/iai" } +once_cell = "1" +tiny-skia = "0.6.2" +ttf-parser = "0.17" +unscanny = "0.1" +walkdir = "2" + +[[test]] +name = "tests" +path = "src/tests.rs" +harness = false + +[[bench]] +name = "benches" +path = "src/benches.rs" +harness = false diff --git a/tests/README.md b/tests/README.md index 7343d8680..baca91489 100644 --- a/tests/README.md +++ b/tests/README.md @@ -2,6 +2,7 @@ ## Directory structure Top level directory structure: +- `src`: Testing code. - `typ`: Input files. - `res`: Resource files used by tests. - `ref`: Reference images which the output is compared with to determine whether @@ -10,24 +11,34 @@ Top level directory structure: - `pdf`: PDF files produced by tests. ## Running the tests -Running the integration tests (the tests in this directory). +Running all tests (including unit tests): ```bash -cargo test --test typeset +cargo test --all +``` + +Running just the integration tests (the tests in this directory): +```bash +cargo test --all --test tests +``` + +You may want to [make yourself an alias](#making-an-alias) like: +```bash +testit ``` Running all tests whose paths contain the string `page` or `stack`. ```bash -cargo test --test typeset page stack +testit page stack ``` Running a test with the exact filename `page.typ`. ```bash -cargo test --test typeset -- --exact page.typ +testit --exact page.typ ``` Debug-printing the layout trees for all executed tests. ```bash -cargo test --test typeset -- --debug empty.typ +testit --debug empty.typ ``` To make the integration tests go faster they don't generate PDFs by default. @@ -35,7 +46,7 @@ Pass the `--pdf` flag to generate those. Mind that PDFs are not tested automatically at the moment, so you should always check the output manually when making changes. ```bash -cargo test --test typeset -- --pdf +testit --pdf ``` ## Creating new tests @@ -50,23 +61,23 @@ oxipng -o max path/to/image.png oxipng -r -o max tests/ref ``` -## Shorthand for running tests +## Making an alias If you want to have a quicker way to run the tests, consider adding a shortcut to your shell profile so that you can simply write something like: ```bash -tests --debug empty.typ -``` - -### PowerShell -Open your PowerShell profile by executing `notepad $profile`. -```ps -function tests { - cargo test --test typeset -- $args -} +testit empty.typ ``` ### Bash Open your Bash configuration by executing `nano ~/.bashrc`. ```bash -alias tests="cargo test --test typeset --" +alias testit="cargo test --all --test tests --" +``` + +### PowerShell +Open your PowerShell profile by executing `notepad $profile`. +```ps +function testit { + cargo test --all --test tests -- $args +} ``` diff --git a/benches/oneshot.rs b/tests/src/benches.rs similarity index 96% rename from benches/oneshot.rs rename to tests/src/benches.rs index 965d0c788..3af2db6eb 100644 --- a/benches/oneshot.rs +++ b/tests/src/benches.rs @@ -10,8 +10,8 @@ use typst::syntax::{Source, SourceId, TokenMode, Tokens}; use typst::util::Buffer; use typst::{Config, World}; -const TEXT: &str = include_str!("bench.typ"); -const FONT: &[u8] = include_bytes!("../fonts/IBMPlexSans-Regular.ttf"); +const TEXT: &str = include_str!("../typ/benches/bench.typ"); +const FONT: &[u8] = include_bytes!("../../fonts/IBMPlexSans-Regular.ttf"); main!( bench_decode, @@ -113,6 +113,7 @@ impl BenchWorld { let book = FontBook::from_fonts([&font]); let id = SourceId::from_u16(0); let source = Source::new(id, Path::new("bench.typ"), TEXT.into()); + Self { config: Prehashed::new(config), book: Prehashed::new(book), diff --git a/tests/typeset.rs b/tests/src/tests.rs similarity index 98% rename from tests/typeset.rs rename to tests/src/tests.rs index 7c0a6e698..620c1e7b2 100644 --- a/tests/typeset.rs +++ b/tests/src/tests.rs @@ -25,26 +25,28 @@ use typst::{Config, World}; use typst_library::layout::PageNode; use typst_library::text::{TextNode, TextSize}; -const TYP_DIR: &str = "./typ"; -const REF_DIR: &str = "./ref"; -const PNG_DIR: &str = "./png"; -const PDF_DIR: &str = "./pdf"; +const TYP_DIR: &str = "typ"; +const REF_DIR: &str = "ref"; +const PNG_DIR: &str = "png"; +const PDF_DIR: &str = "pdf"; const FONT_DIR: &str = "../fonts"; fn main() { - env::set_current_dir(env::current_dir().unwrap().join("tests")).unwrap(); - let args = Args::new(env::args().skip(1)); let mut filtered = Vec::new(); // Since differents tests can affect each other through the memoization // cache, a deterministic order is important for reproducibility. - for entry in WalkDir::new(".").sort_by_file_name() { + for entry in WalkDir::new("typ").sort_by_file_name() { let entry = entry.unwrap(); if entry.depth() <= 1 { continue; } + if entry.path().starts_with("typ/benches") { + continue; + } + let src_path = entry.into_path(); if src_path.extension() != Some(OsStr::new("typ")) { continue; diff --git a/benches/bench.typ b/tests/typ/benches/bench.typ similarity index 100% rename from benches/bench.typ rename to tests/typ/benches/bench.typ