From 67c46e7159c1cda89e8862165c2c30e6e8e222d1 Mon Sep 17 00:00:00 2001 From: Filippo Neysofu Costa Date: Tue, 12 Sep 2023 19:32:03 +0200 Subject: [PATCH] Support for `trybuild` and similar macro testing tools (#168) Signed-off-by: Filippo Costa --- dist/restore/index.js | 19 ++++++ dist/save/index.js | 19 ++++++ src/cleanup.ts | 20 +++++++ tests/Cargo.lock | 64 ++++++++++++++++++++- tests/Cargo.toml | 3 + tests/tests/trybuild.rs | 6 ++ tests/tests/trybuild/empty_main.rs | 1 + tests/tests/trybuild/fail_to_compile.rs | 3 + tests/tests/trybuild/fail_to_compile.stderr | 5 ++ 9 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 tests/tests/trybuild.rs create mode 100644 tests/tests/trybuild/empty_main.rs create mode 100644 tests/tests/trybuild/fail_to_compile.rs create mode 100644 tests/tests/trybuild/fail_to_compile.stderr diff --git a/dist/restore/index.js b/dist/restore/index.js index ba15d54..d4e032a 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -64354,6 +64354,25 @@ async function cleanTargetDir(targetDir, packages, checkTimestamp = false) { } async function cleanProfileTarget(profileDir, packages, checkTimestamp = false) { lib_core.debug(`cleaning profile directory "${profileDir}"`); + // Quite a few testing utility crates store compilation artifacts as nested + // workspaces under `target/tests`. Notably, `target/tests/target` and + // `target/tests/trybuild`. + if (external_path_default().basename(profileDir) === "tests") { + try { + // https://github.com/vertexclique/kaos/blob/9876f6c890339741cc5be4b7cb9df72baa5a6d79/src/cargo.rs#L25 + // https://github.com/eupn/macrotest/blob/c4151a5f9f545942f4971980b5d264ebcd0b1d11/src/cargo.rs#L27 + cleanTargetDir(external_path_default().join(profileDir, "target"), packages, checkTimestamp); + } + catch { } + try { + // https://github.com/dtolnay/trybuild/blob/eec8ca6cb9b8f53d0caf1aa499d99df52cae8b40/src/cargo.rs#L50 + cleanTargetDir(external_path_default().join(profileDir, "trybuild"), packages, checkTimestamp); + } + catch { } + // Delete everything else. + await rmExcept(profileDir, new Set(["target", "trybuild"]), checkTimestamp); + return; + } let keepProfile = new Set(["build", ".fingerprint", "deps"]); await rmExcept(profileDir, keepProfile); const keepPkg = new Set(packages.map((p) => p.name)); diff --git a/dist/save/index.js b/dist/save/index.js index 49e4b49..f22cb50 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -64354,6 +64354,25 @@ async function cleanTargetDir(targetDir, packages, checkTimestamp = false) { } async function cleanProfileTarget(profileDir, packages, checkTimestamp = false) { core.debug(`cleaning profile directory "${profileDir}"`); + // Quite a few testing utility crates store compilation artifacts as nested + // workspaces under `target/tests`. Notably, `target/tests/target` and + // `target/tests/trybuild`. + if (external_path_default().basename(profileDir) === "tests") { + try { + // https://github.com/vertexclique/kaos/blob/9876f6c890339741cc5be4b7cb9df72baa5a6d79/src/cargo.rs#L25 + // https://github.com/eupn/macrotest/blob/c4151a5f9f545942f4971980b5d264ebcd0b1d11/src/cargo.rs#L27 + cleanTargetDir(external_path_default().join(profileDir, "target"), packages, checkTimestamp); + } + catch { } + try { + // https://github.com/dtolnay/trybuild/blob/eec8ca6cb9b8f53d0caf1aa499d99df52cae8b40/src/cargo.rs#L50 + cleanTargetDir(external_path_default().join(profileDir, "trybuild"), packages, checkTimestamp); + } + catch { } + // Delete everything else. + await rmExcept(profileDir, new Set(["target", "trybuild"]), checkTimestamp); + return; + } let keepProfile = new Set(["build", ".fingerprint", "deps"]); await rmExcept(profileDir, keepProfile); const keepPkg = new Set(packages.map((p) => p.name)); diff --git a/src/cleanup.ts b/src/cleanup.ts index 6b401f3..854f404 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -34,6 +34,26 @@ export async function cleanTargetDir(targetDir: string, packages: Packages, chec async function cleanProfileTarget(profileDir: string, packages: Packages, checkTimestamp = false) { core.debug(`cleaning profile directory "${profileDir}"`); + // Quite a few testing utility crates store compilation artifacts as nested + // workspaces under `target/tests`. Notably, `target/tests/target` and + // `target/tests/trybuild`. + if (path.basename(profileDir) === "tests") { + try { + // https://github.com/vertexclique/kaos/blob/9876f6c890339741cc5be4b7cb9df72baa5a6d79/src/cargo.rs#L25 + // https://github.com/eupn/macrotest/blob/c4151a5f9f545942f4971980b5d264ebcd0b1d11/src/cargo.rs#L27 + cleanTargetDir(path.join(profileDir, "target"), packages, checkTimestamp); + } catch { } + try { + // https://github.com/dtolnay/trybuild/blob/eec8ca6cb9b8f53d0caf1aa499d99df52cae8b40/src/cargo.rs#L50 + cleanTargetDir(path.join(profileDir, "trybuild"), packages, checkTimestamp); + } catch { } + + // Delete everything else. + await rmExcept(profileDir, new Set(["target", "trybuild"]), checkTimestamp); + + return + } + let keepProfile = new Set(["build", ".fingerprint", "deps"]); await rmExcept(profileDir, keepProfile); diff --git a/tests/Cargo.lock b/tests/Cargo.lock index fcde97f..51ac318 100644 --- a/tests/Cargo.lock +++ b/tests/Cargo.lock @@ -14,6 +14,15 @@ version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" +[[package]] +name = "basic-toml" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bfc506e7a2370ec239e1d072507b2a80c833083699d3c6fa176fbb4de8448c6" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -148,6 +157,12 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "h2" version = "0.3.14" @@ -524,6 +539,7 @@ version = "0.1.0" dependencies = [ "reqwest", "tikv-jemallocator", + "trybuild", "watto", ] @@ -568,9 +584,20 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.144" +version = "1.0.185" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +checksum = "be9b6f69f1dfd54c3b568ffa45c310d6973a5e5148fd40cf515acaf38cf5bc31" + +[[package]] +name = "serde_derive" +version = "1.0.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" @@ -639,6 +666,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.33" @@ -767,6 +803,21 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "trybuild" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501dbdbb99861e4ab6b60eb6a7493956a9defb644fd034bc4a5ef27c693c8a3a" +dependencies = [ + "basic-toml", + "glob", + "once_cell", + "serde", + "serde_derive", + "serde_json", + "termcolor", +] + [[package]] name = "unicode-bidi" version = "0.3.8" @@ -923,6 +974,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/tests/Cargo.toml b/tests/Cargo.toml index c189021..ba14017 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -9,5 +9,8 @@ edition = "2021" reqwest = "0.11.18" watto = { git = "https://github.com/getsentry/watto", rev = "d71c8218506bddba102a124a460d64da25e303dc", features = ["strings"] } +[dev-dependencies] +trybuild = "1" + [target.'cfg(not(target_env = "msvc"))'.dependencies] tikv-jemallocator = "0.5.4" diff --git a/tests/tests/trybuild.rs b/tests/tests/trybuild.rs new file mode 100644 index 0000000..8a871f1 --- /dev/null +++ b/tests/tests/trybuild.rs @@ -0,0 +1,6 @@ +#[test] +fn test_trybuild() { + let t = trybuild::TestCases::new(); + t.pass("tests/trybuild/empty_main.rs"); + t.compile_fail("tests/trybuild/fail_to_compile.rs"); +} diff --git a/tests/tests/trybuild/empty_main.rs b/tests/tests/trybuild/empty_main.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/tests/tests/trybuild/empty_main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/tests/tests/trybuild/fail_to_compile.rs b/tests/tests/trybuild/fail_to_compile.rs new file mode 100644 index 0000000..4de098c --- /dev/null +++ b/tests/tests/trybuild/fail_to_compile.rs @@ -0,0 +1,3 @@ +fn main() { + "foobar".foobar(); +} diff --git a/tests/tests/trybuild/fail_to_compile.stderr b/tests/tests/trybuild/fail_to_compile.stderr new file mode 100644 index 0000000..fac6619 --- /dev/null +++ b/tests/tests/trybuild/fail_to_compile.stderr @@ -0,0 +1,5 @@ +error[E0599]: no method named `foobar` found for reference `&'static str` in the current scope + --> tests/trybuild/fail_to_compile.rs:2:14 + | +2 | "foobar".foobar(); + | ^^^^^^ method not found in `&str`