From ed69a7378690d70fba8aede8a3b67144114aa9f8 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 24 Jul 2022 11:57:01 -0400 Subject: [PATCH] Update glue tests --- crates/cli_utils/src/helpers.rs | 8 +- crates/glue/src/load.rs | 2 +- crates/glue/src/rust_glue.rs | 8 +- crates/glue/src/types.rs | 4 +- crates/glue/templates/header.rs | 2 +- crates/glue/tests/fixtures/.gitignore | 2 +- .../advanced-recursive-union/src/lib.rs | 4 +- .../tests/fixtures/basic-record/src/lib.rs | 6 +- .../fixtures/basic-recursive-union/src/lib.rs | 4 +- .../tests/fixtures/enumeration/src/lib.rs | 10 +-- .../fixtures/list-recursive-union/src/lib.rs | 4 +- .../tests/fixtures/nested-record/src/lib.rs | 6 +- .../fixtures/nullable-unwrapped/src/lib.rs | 4 +- .../fixtures/union-with-padding/src/lib.rs | 4 +- .../union-without-padding/platform.roc | 2 +- .../fixtures/union-without-padding/src/lib.rs | 14 +-- crates/glue/tests/helpers/mod.rs | 8 +- .../{test_bindgen_cli.rs => test_glue_cli.rs} | 85 +++++++------------ examples/interactive/cli-platform/src/glue.rs | 2 +- 19 files changed, 74 insertions(+), 105 deletions(-) rename crates/glue/tests/{test_bindgen_cli.rs => test_glue_cli.rs} (74%) diff --git a/crates/cli_utils/src/helpers.rs b/crates/cli_utils/src/helpers.rs index 2598504ae3..fb8c63b370 100644 --- a/crates/cli_utils/src/helpers.rs +++ b/crates/cli_utils/src/helpers.rs @@ -65,22 +65,18 @@ where run_with_stdin(&roc_binary_path, args, stdin_vals) } -pub fn run_bindgen(args: I) -> Out +pub fn run_glue(args: I) -> Out where I: IntoIterator, S: AsRef, { - run_with_stdin(&path_to_bindgen_binary(), args, &[]) + run_with_stdin(&path_to_roc_binary(), args, &[]) } pub fn path_to_roc_binary() -> PathBuf { path_to_binary("roc") } -pub fn path_to_bindgen_binary() -> PathBuf { - path_to_binary("roc-bindgen") -} - pub fn path_to_binary(binary_name: &str) -> PathBuf { // Adapted from https://github.com/volta-cli/volta/blob/cefdf7436a15af3ce3a38b8fe53bb0cfdb37d3dd/tests/acceptance/support/sandbox.rs#L680 // by the Volta Contributors - license information can be found in diff --git a/crates/glue/src/load.rs b/crates/glue/src/load.rs index 26604ad28c..9390dd79b3 100644 --- a/crates/glue/src/load.rs +++ b/crates/glue/src/load.rs @@ -106,7 +106,7 @@ pub fn load_types( if !can_problems.is_empty() || !type_problems.is_empty() { todo!( - "Gracefully report compilation problems during bindgen: {:?}, {:?}", + "Gracefully report compilation problems during glue generation: {:?}, {:?}", can_problems, type_problems ); diff --git a/crates/glue/src/rust_glue.rs b/crates/glue/src/rust_glue.rs index eedb58040e..f40896b929 100644 --- a/crates/glue/src/rust_glue.rs +++ b/crates/glue/src/rust_glue.rs @@ -254,7 +254,7 @@ fn add_type(target_info: TargetInfo, id: TypeId, types: &Types, impls: &mut Impl // so no extra work needs to happen. } RocType::Function { .. } => { - // TODO actually bindgen functions! + // TODO actually generate glue functions! } } } @@ -312,8 +312,8 @@ fn add_tag_union( types: &Types, impls: &mut Impls, ) { - // We should never be attempting to bindgen empty tag unions; RocType should not - // have let this happen. + // We should never be attempting to generate glue for empty tag unions; + // RocType should not have let this happen. debug_assert_ne!(tags.len(), 0); let tag_names = tags.iter().map(|(name, _)| name).cloned().collect(); @@ -471,7 +471,7 @@ pub struct {name} {{ ); } else { todo!( - "Support {} tags in a recursive tag union on target_info {:?}. (This is too many tags for pointer tagging to work, so we need to bindgen something different.)", + "Support {} tags in a recursive tag union on target_info {:?}. (This is too many tags for pointer tagging to work, so we need to generate different glue.)", tags.len(), target_info ); diff --git a/crates/glue/src/types.rs b/crates/glue/src/types.rs index 1184b0e49f..9530f33f5d 100644 --- a/crates/glue/src/types.rs +++ b/crates/glue/src/types.rs @@ -1111,11 +1111,11 @@ fn add_tag_union<'a>( | Layout::Boxed(_) | Layout::LambdaSet(_) | Layout::RecursivePointer => { - // These must be single-tag unions. Bindgen ordinary nonrecursive + // These must be single-tag unions. Generate ordinary nonrecursive // tag unions for them, and let Rust do the unwrapping. // // This should be a very rare use case, and it's not worth overcomplicating - // the rest of bindgen to make it do something different. + // the rest of glue to make it do something different. RocType::TagUnion(RocTagUnion::NonRecursive { name: name.clone(), tags, diff --git a/crates/glue/templates/header.rs b/crates/glue/templates/header.rs index 66613dfa09..b4935b9097 100644 --- a/crates/glue/templates/header.rs +++ b/crates/glue/templates/header.rs @@ -1,4 +1,4 @@ -// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc-bindgen` CLI +// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc glue` CLI command #![allow(dead_code)] #![allow(unused_mut)] diff --git a/crates/glue/tests/fixtures/.gitignore b/crates/glue/tests/fixtures/.gitignore index cc22f3ac1f..a71685876a 100644 --- a/crates/glue/tests/fixtures/.gitignore +++ b/crates/glue/tests/fixtures/.gitignore @@ -2,7 +2,7 @@ Cargo.lock Cargo.toml build.rs host.c -bindings.rs +test_glue.rs roc_externs.rs main.rs app diff --git a/crates/glue/tests/fixtures/advanced-recursive-union/src/lib.rs b/crates/glue/tests/fixtures/advanced-recursive-union/src/lib.rs index 7df74a4368..8199720954 100644 --- a/crates/glue/tests/fixtures/advanced-recursive-union/src/lib.rs +++ b/crates/glue/tests/fixtures/advanced-recursive-union/src/lib.rs @@ -1,7 +1,7 @@ -mod bindings; +mod test_glue; -use bindings::Rbt; use indoc::indoc; +use test_glue::Rbt; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] diff --git a/crates/glue/tests/fixtures/basic-record/src/lib.rs b/crates/glue/tests/fixtures/basic-record/src/lib.rs index 15e3ecd7e5..d467649ad7 100644 --- a/crates/glue/tests/fixtures/basic-record/src/lib.rs +++ b/crates/glue/tests/fixtures/basic-record/src/lib.rs @@ -1,8 +1,8 @@ -mod bindings; +mod test_glue; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] - fn roc_main(_: *mut bindings::MyRcd); + fn roc_main(_: *mut test_glue::MyRcd); } #[no_mangle] @@ -11,7 +11,7 @@ pub extern "C" fn rust_main() -> i32 { use std::collections::hash_set::HashSet; let record = unsafe { - let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); + let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); roc_main(ret.as_mut_ptr()); diff --git a/crates/glue/tests/fixtures/basic-recursive-union/src/lib.rs b/crates/glue/tests/fixtures/basic-recursive-union/src/lib.rs index 1cc2d76e9d..8a82758288 100644 --- a/crates/glue/tests/fixtures/basic-recursive-union/src/lib.rs +++ b/crates/glue/tests/fixtures/basic-recursive-union/src/lib.rs @@ -1,7 +1,7 @@ -mod bindings; +mod test_glue; -use bindings::Expr; use indoc::indoc; +use test_glue::Expr; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] diff --git a/crates/glue/tests/fixtures/enumeration/src/lib.rs b/crates/glue/tests/fixtures/enumeration/src/lib.rs index b63cee5ce8..9bf76923e2 100644 --- a/crates/glue/tests/fixtures/enumeration/src/lib.rs +++ b/crates/glue/tests/fixtures/enumeration/src/lib.rs @@ -1,8 +1,8 @@ -mod bindings; +mod test_glue; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] - fn roc_main(_: *mut bindings::MyEnum); + fn roc_main(_: *mut test_glue::MyEnum); } #[no_mangle] @@ -11,7 +11,7 @@ pub extern "C" fn rust_main() -> i32 { use std::collections::hash_set::HashSet; let tag_union = unsafe { - let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); + let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); roc_main(ret.as_mut_ptr()); @@ -39,8 +39,8 @@ pub extern "C" fn rust_main() -> i32 { println!( "tag_union was: {:?}, Bar is: {:?}, Baz is: {:?}", tag_union, - bindings::MyEnum::Bar, - bindings::MyEnum::Baz, + test_glue::MyEnum::Bar, + test_glue::MyEnum::Baz, ); // Debug // Exit code diff --git a/crates/glue/tests/fixtures/list-recursive-union/src/lib.rs b/crates/glue/tests/fixtures/list-recursive-union/src/lib.rs index 7df74a4368..8199720954 100644 --- a/crates/glue/tests/fixtures/list-recursive-union/src/lib.rs +++ b/crates/glue/tests/fixtures/list-recursive-union/src/lib.rs @@ -1,7 +1,7 @@ -mod bindings; +mod test_glue; -use bindings::Rbt; use indoc::indoc; +use test_glue::Rbt; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] diff --git a/crates/glue/tests/fixtures/nested-record/src/lib.rs b/crates/glue/tests/fixtures/nested-record/src/lib.rs index a660d079a9..52b9445876 100644 --- a/crates/glue/tests/fixtures/nested-record/src/lib.rs +++ b/crates/glue/tests/fixtures/nested-record/src/lib.rs @@ -1,8 +1,8 @@ -mod bindings; +mod test_glue; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] - fn roc_main(_: *mut bindings::Outer); + fn roc_main(_: *mut test_glue::Outer); } #[no_mangle] @@ -10,7 +10,7 @@ pub extern "C" fn rust_main() -> i32 { use std::cmp::Ordering; let outer = unsafe { - let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); + let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); roc_main(ret.as_mut_ptr()); diff --git a/crates/glue/tests/fixtures/nullable-unwrapped/src/lib.rs b/crates/glue/tests/fixtures/nullable-unwrapped/src/lib.rs index 8b025e07dd..58a5516bde 100644 --- a/crates/glue/tests/fixtures/nullable-unwrapped/src/lib.rs +++ b/crates/glue/tests/fixtures/nullable-unwrapped/src/lib.rs @@ -1,7 +1,7 @@ -mod bindings; +mod test_glue; -use bindings::StrConsList; use indoc::indoc; +use test_glue::StrConsList; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] diff --git a/crates/glue/tests/fixtures/union-with-padding/src/lib.rs b/crates/glue/tests/fixtures/union-with-padding/src/lib.rs index 408f574a4c..f8b9a05e20 100644 --- a/crates/glue/tests/fixtures/union-with-padding/src/lib.rs +++ b/crates/glue/tests/fixtures/union-with-padding/src/lib.rs @@ -1,6 +1,6 @@ -mod bindings; +mod test_glue; -use bindings::NonRecursive; +use test_glue::NonRecursive; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] diff --git a/crates/glue/tests/fixtures/union-without-padding/platform.roc b/crates/glue/tests/fixtures/union-without-padding/platform.roc index 881b30b330..68a1c236fc 100644 --- a/crates/glue/tests/fixtures/union-without-padding/platform.roc +++ b/crates/glue/tests/fixtures/union-without-padding/platform.roc @@ -8,7 +8,7 @@ platform "test-platform" # This case is important to test because there's no padding # after the largest variant, so the compiler adds an extra u8 # (rounded up to alignment, so an an extra 8 bytes) in which -# to store the discriminant. We have to bindgen accordingly! +# to store the discriminant. We have to generate glue code accordingly! NonRecursive : [Foo Str, Bar I64, Blah I32, Baz] mainForHost : NonRecursive diff --git a/crates/glue/tests/fixtures/union-without-padding/src/lib.rs b/crates/glue/tests/fixtures/union-without-padding/src/lib.rs index ba4df484bf..c65c5c76e3 100644 --- a/crates/glue/tests/fixtures/union-without-padding/src/lib.rs +++ b/crates/glue/tests/fixtures/union-without-padding/src/lib.rs @@ -1,8 +1,8 @@ -mod bindings; +mod test_glue; extern "C" { #[link_name = "roc__mainForHost_1_exposed_generic"] - fn roc_main(_: *mut bindings::NonRecursive); + fn roc_main(_: *mut test_glue::NonRecursive); } #[no_mangle] @@ -11,7 +11,7 @@ pub extern "C" fn rust_main() -> i32 { use std::collections::hash_set::HashSet; let tag_union = unsafe { - let mut ret: core::mem::MaybeUninit = + let mut ret: core::mem::MaybeUninit = core::mem::MaybeUninit::uninit(); roc_main(ret.as_mut_ptr()); @@ -30,10 +30,10 @@ pub extern "C" fn rust_main() -> i32 { println!( "tag_union was: {:?}\n`Foo \"small str\"` is: {:?}\n`Bar 123` is: {:?}\n`Baz` is: {:?}\n`Blah 456` is: {:?}", tag_union, - bindings::NonRecursive::Foo("small str".into()), - bindings::NonRecursive::Bar(123), - bindings::NonRecursive::Baz, - bindings::NonRecursive::Blah(456), + test_glue::NonRecursive::Foo("small str".into()), + test_glue::NonRecursive::Bar(123), + test_glue::NonRecursive::Baz, + test_glue::NonRecursive::Blah(456), ); // Debug let mut set = HashSet::new(); diff --git a/crates/glue/tests/helpers/mod.rs b/crates/glue/tests/helpers/mod.rs index b58612531e..127940603e 100644 --- a/crates/glue/tests/helpers/mod.rs +++ b/crates/glue/tests/helpers/mod.rs @@ -1,5 +1,5 @@ -use roc_bindgen::bindgen_rs; -use roc_bindgen::load::load_types; +use roc_glue::load::load_types; +use roc_glue::rust_glue; use roc_load::Threading; use std::env; use std::fs::File; @@ -40,7 +40,7 @@ pub fn generate_bindings(decl_src: &str) -> String { result.expect("had problems loading") }; - bindgen_rs::emit(&pairs) + rust_glue::emit(&pairs) } #[allow(dead_code)] @@ -49,7 +49,7 @@ pub fn fixtures_dir(dir_name: &str) -> PathBuf { // Descend into cli/tests/fixtures/{dir_name} path.push("crates"); - path.push("bindgen"); + path.push("glue"); path.push("tests"); path.push("fixtures"); path.push(dir_name); diff --git a/crates/glue/tests/test_bindgen_cli.rs b/crates/glue/tests/test_glue_cli.rs similarity index 74% rename from crates/glue/tests/test_bindgen_cli.rs rename to crates/glue/tests/test_glue_cli.rs index a9ce346e10..4d506621c2 100644 --- a/crates/glue/tests/test_bindgen_cli.rs +++ b/crates/glue/tests/test_glue_cli.rs @@ -10,41 +10,11 @@ extern crate roc_collections; mod helpers; #[cfg(test)] -mod bindgen_cli_run { - use crate::helpers::{fixtures_dir, root_dir}; - use cli_utils::helpers::{run_bindgen, run_roc, Out}; +mod glue_cli_run { + use crate::helpers::fixtures_dir; + use cli_utils::helpers::{run_glue, run_roc, Out}; use std::fs; use std::path::Path; - use std::process::Command; - - // All of these tests rely on `target/` for the `cli` crate being up-to-date, - // so do a `cargo build` on it first! - #[ctor::ctor] - fn init() { - let args = if cfg!(debug_assertions) { - vec!["build"] - } else { - vec!["build", "--release"] - }; - - println!( - "Running `cargo {}` on the `cli` crate before running the tests. This may take a bit!", - args.join(" ") - ); - - let output = Command::new("cargo") - .args(args) - .current_dir(root_dir().join("crates").join("cli")) - .output() - .unwrap_or_else(|err| { - panic!( - "Failed to `cargo build` roc CLI for bindgen CLI tests - error was: {:?}", - err - ) - }); - - assert!(output.status.success()); - } /// This macro does two things. /// @@ -68,7 +38,7 @@ mod bindgen_cli_run { fn $test_name() { let dir = fixtures_dir($fixture_dir); - generate_bindings_for(&dir, std::iter::empty()); + generate_glue_for(&dir, std::iter::empty()); let out = run_app(&dir.join("app.roc"), std::iter::empty()); assert!(out.status.success()); @@ -137,8 +107,6 @@ mod bindgen_cli_run { fn check_for_tests(all_fixtures: &mut roc_collections::VecSet) { use roc_collections::VecSet; - // todo!("Remove a bunch of duplication - don't have a ton of files in there."); - let fixtures = fixtures_dir(""); let entries = std::fs::read_dir(fixtures.as_path()).unwrap_or_else(|err| { panic!( @@ -156,7 +124,7 @@ mod bindgen_cli_run { if !all_fixtures.remove(&fixture_dir_name) { panic!( - "The bindgen fixture directory {} does not have any corresponding tests in cli_run. Please add one, so if it ever stops working, we'll know about it right away!", + "The glue fixture directory {} does not have any corresponding tests in test_glue_cli. Please add one, so if it ever stops working, we'll know about it right away!", entry.path().to_string_lossy() ); } @@ -166,12 +134,12 @@ mod bindgen_cli_run { assert_eq!(all_fixtures, &mut VecSet::default()); } - fn generate_bindings_for<'a, I: IntoIterator>( + fn generate_glue_for<'a, I: IntoIterator>( platform_dir: &'a Path, args: I, ) -> Out { let platform_module_path = platform_dir.join("platform.roc"); - let bindings_file = platform_dir.join("src").join("bindings.rs"); + let glue_file = platform_dir.join("src").join("test_glue.rs"); let fixture_templates_dir = platform_dir .parent() .unwrap() @@ -179,44 +147,49 @@ mod bindgen_cli_run { .unwrap() .join("fixture-templates"); + dbg!(&platform_module_path); + dbg!(&glue_file); + // Copy the rust template from the templates directory into the fixture dir. dircpy::CopyBuilder::new(fixture_templates_dir.join("rust"), platform_dir) .overwrite(true) // overwrite any files that were already present .run() .unwrap(); - // Delete the bindings file to make sure we're actually regenerating it! - if bindings_file.exists() { - fs::remove_file(&bindings_file) - .expect("Unable to remove bindings.rs in order to regenerate it in the test"); + // Delete the glue file to make sure we're actually regenerating it! + if glue_file.exists() { + fs::remove_file(&glue_file) + .expect("Unable to remove test_glue.rs in order to regenerate it in the test"); } - // Generate a fresh bindings.rs for this platform - let bindgen_out = run_bindgen( + // Generate a fresh test_glue.rs for this platform + let glue_out = run_glue( // converting these all to String avoids lifetime issues - args.into_iter().map(|arg| arg.to_string()).chain([ - platform_module_path.to_str().unwrap().to_string(), - bindings_file.to_str().unwrap().to_string(), - ]), + std::iter::once("glue".to_string()).chain( + args.into_iter().map(|arg| arg.to_string()).chain([ + platform_module_path.to_str().unwrap().to_string(), + glue_file.to_str().unwrap().to_string(), + ]), + ), ); // If there is any stderr, it should be reporting the runtime and that's it! - if !(bindgen_out.stderr.is_empty() - || bindgen_out.stderr.starts_with("runtime: ") && bindgen_out.stderr.ends_with("ms\n")) + if !(glue_out.stderr.is_empty() + || glue_out.stderr.starts_with("runtime: ") && glue_out.stderr.ends_with("ms\n")) { panic!( - "`roc-bindgen` command had unexpected stderr: {}", - bindgen_out.stderr + "`roc glue` command had unexpected stderr: {}", + glue_out.stderr ); } - assert!(bindgen_out.status.success(), "bad status {:?}", bindgen_out); + assert!(glue_out.status.success(), "bad status {:?}", glue_out); - bindgen_out + glue_out } fn run_app<'a, I: IntoIterator>(app_file: &'a Path, args: I) -> Out { - // Generate bindings.rs for this platform + // Generate test_glue.rs for this platform let compile_out = run_roc( // converting these all to String avoids lifetime issues args.into_iter() diff --git a/examples/interactive/cli-platform/src/glue.rs b/examples/interactive/cli-platform/src/glue.rs index c09aeca0e8..779b0bf874 100644 --- a/examples/interactive/cli-platform/src/glue.rs +++ b/examples/interactive/cli-platform/src/glue.rs @@ -1,4 +1,4 @@ -// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc-bindgen` CLI +// ⚠️ GENERATED CODE ⚠️ - this entire file was generated by the `roc glue` CLI command #![allow(dead_code)] #![allow(unused_mut)]