mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-13 09:49:11 +03:00
Update glue tests
This commit is contained in:
parent
cc790a2773
commit
ed69a73786
@ -65,22 +65,18 @@ where
|
||||
run_with_stdin(&roc_binary_path, args, stdin_vals)
|
||||
}
|
||||
|
||||
pub fn run_bindgen<I, S>(args: I) -> Out
|
||||
pub fn run_glue<I, S>(args: I) -> Out
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: AsRef<OsStr>,
|
||||
{
|
||||
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
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -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,
|
||||
|
@ -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)]
|
||||
|
2
crates/glue/tests/fixtures/.gitignore
vendored
2
crates/glue/tests/fixtures/.gitignore
vendored
@ -2,7 +2,7 @@ Cargo.lock
|
||||
Cargo.toml
|
||||
build.rs
|
||||
host.c
|
||||
bindings.rs
|
||||
test_glue.rs
|
||||
roc_externs.rs
|
||||
main.rs
|
||||
app
|
||||
|
@ -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"]
|
||||
|
@ -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<bindings::MyRcd> = core::mem::MaybeUninit::uninit();
|
||||
let mut ret: core::mem::MaybeUninit<test_glue::MyRcd> = core::mem::MaybeUninit::uninit();
|
||||
|
||||
roc_main(ret.as_mut_ptr());
|
||||
|
||||
|
@ -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"]
|
||||
|
@ -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<bindings::MyEnum> = core::mem::MaybeUninit::uninit();
|
||||
let mut ret: core::mem::MaybeUninit<test_glue::MyEnum> = 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
|
||||
|
@ -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"]
|
||||
|
@ -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<bindings::Outer> = core::mem::MaybeUninit::uninit();
|
||||
let mut ret: core::mem::MaybeUninit<test_glue::Outer> = core::mem::MaybeUninit::uninit();
|
||||
|
||||
roc_main(ret.as_mut_ptr());
|
||||
|
||||
|
@ -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"]
|
||||
|
@ -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"]
|
||||
|
@ -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
|
||||
|
@ -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<bindings::NonRecursive> =
|
||||
let mut ret: core::mem::MaybeUninit<test_glue::NonRecursive> =
|
||||
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();
|
||||
|
@ -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);
|
||||
|
@ -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<String>) {
|
||||
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<Item = &'a str>>(
|
||||
fn generate_glue_for<'a, I: IntoIterator<Item = &'a str>>(
|
||||
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<Item = &'a str>>(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()
|
@ -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)]
|
||||
|
Loading…
Reference in New Issue
Block a user