Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate invariant-enforcing tests in stubgen adapters. #389

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions trustfall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub mod provider {

// Helpers for common operations when building adapters.
pub use trustfall_core::interpreter::helpers::{
resolve_coercion_using_schema, resolve_coercion_with, resolve_neighbors_with,
resolve_property_with, resolve_typename,
check_adapter_invariants, resolve_coercion_using_schema, resolve_coercion_with,
resolve_neighbors_with, resolve_property_with, resolve_typename,
};
pub use trustfall_core::{accessor_property, field_property};

Expand Down
1 change: 1 addition & 0 deletions trustfall_stubgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The generated Trustfall adapter stub has the following structure:
| adapter/entrypoints.rs | contains the entry points where all queries must start |
| adapter/properties.rs | contains the property implementations |
| adapter/edges.rs | contains the edge implementations |
| adapter/tests.rs | contains test code |

See an example of
[a generated adapter stub](https://github.com/obi1kenobi/trustfall/tree/main/trustfall_stubgen/test_data/expected_outputs/hackernews/adapter)
Expand Down
5 changes: 5 additions & 0 deletions trustfall_stubgen/src/adapter_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(super) fn make_adapter_file(
};

let adapter_defn = quote! {
#[non_exhaustive]
#[derive(Debug)]
pub struct Adapter {}
};
Expand All @@ -45,6 +46,10 @@ pub(super) fn make_adapter_file(
pub fn schema() -> &'static Schema {
SCHEMA.get_or_init(|| Schema::parse(Self::SCHEMA_TEXT).expect("not a valid schema"))
}

pub fn new() -> Self {
Self {}
}
}
};

Expand Down
35 changes: 34 additions & 1 deletion trustfall_stubgen/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use quote::quote;
use regex::Regex;
use trustfall::{Schema, SchemaAdapter, TryIntoStruct};

use crate::util::parse_import;

use super::{
adapter_creator::make_adapter_file, edges_creator::make_edges_file,
entrypoints_creator::make_entrypoints_file, properties_creator::make_properties_file,
Expand All @@ -25,6 +27,7 @@ use super::{
/// - adapter/entrypoints.rs contains the entry points where all queries must start
/// - adapter/properties.rs contains the property implementations
/// - adapter/edges.rs contains the edge implementations
/// - adapter/tests.rs contains test code
///
/// # Example
/// ```no_run
Expand Down Expand Up @@ -62,13 +65,13 @@ pub fn generate_rust_stub(schema: &str, target: &Path) -> anyhow::Result<()> {
&mut stub.properties,
);
make_edges_file(&querying_schema, schema_adapter.clone(), &mut stub.edges);

make_adapter_file(
&querying_schema,
schema_adapter.clone(),
&mut stub.adapter_impl,
entrypoint_match_arms,
);
make_tests_file(&mut stub.tests);

stub.write_to_directory(target)
}
Expand Down Expand Up @@ -266,6 +269,7 @@ struct AdapterStub<'a> {
entrypoints: RustFile,
properties: RustFile,
edges: RustFile,
tests: RustFile,
}

impl<'a> AdapterStub<'a> {
Expand All @@ -279,6 +283,10 @@ impl<'a> AdapterStub<'a> {
mod properties;
mod edges;
});
mod_.top_level_items.push(quote! {
#[cfg(test)]
mod tests;
});
mod_.top_level_items.push(quote! {
pub use adapter_impl::Adapter;
pub use vertex::Vertex;
Expand All @@ -292,6 +300,7 @@ impl<'a> AdapterStub<'a> {
entrypoints: Default::default(),
properties: Default::default(),
edges: Default::default(),
tests: Default::default(),
}
}

Expand Down Expand Up @@ -328,10 +337,33 @@ impl<'a> AdapterStub<'a> {
self.edges.write_to_file(path_buf.as_path())?;
path_buf.pop();

path_buf.push("tests.rs");
self.tests.write_to_file(path_buf.as_path())?;
path_buf.pop();

Ok(())
}
}

fn make_tests_file(tests_file: &mut RustFile) {
tests_file.external_imports.insert(parse_import(
"trustfall::provider::check_adapter_invariants",
));

tests_file
.internal_imports
.insert(parse_import("super::Adapter"));

tests_file.top_level_items.push(quote! {
#[test]
fn adapter_satisfies_trustfall_invariants() {
let adapter = Adapter::new();
let schema = Adapter::schema();
check_adapter_invariants(schema, adapter);
}
});
}

fn make_vertex_file(
querying_schema: &Schema,
adapter: Arc<SchemaAdapter<'_>>,
Expand Down Expand Up @@ -368,6 +400,7 @@ fn make_vertex_file(
}

let vertex = quote! {
#[non_exhaustive]
#[derive(Debug, Clone, trustfall::provider::TrustfallEnumVertex)]
pub enum Vertex {
#variants
Expand Down
3 changes: 1 addition & 2 deletions trustfall_stubgen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ fn get_relevant_files_from_dir(path: &Path) -> BTreeMap<PathBuf, PathBuf> {
let mut dir_glob = path.to_path_buf();
dir_glob.push("**");
dir_glob.push("*");
dbg!(&dir_glob);

glob::glob(dir_glob.to_str().expect("failed to convert path to &str"))
.expect("failed to list dir")
.filter_map(|res| {
let pathbuf = dbg!(res.expect("failed to check file"));
let pathbuf = res.expect("failed to check file");
if !pathbuf.is_file() {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::vertex::Vertex;

static SCHEMA: OnceLock<Schema> = OnceLock::new();

#[non_exhaustive]
#[derive(Debug)]
pub struct Adapter {}

Expand All @@ -18,6 +19,10 @@ impl Adapter {
Schema::parse(Self::SCHEMA_TEXT).expect("not a valid schema")
})
}

pub fn new() -> Self {
Self {}
}
}

impl<'a> trustfall::provider::Adapter<'a> for Adapter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ mod entrypoints;
mod properties;
mod edges;

#[cfg(test)]
mod tests;

pub use adapter_impl::Adapter;
pub use vertex::Vertex;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use trustfall::provider::check_adapter_invariants;

use super::Adapter;

#[test]
fn adapter_satisfies_trustfall_invariants() {
let adapter = Adapter::new();
let schema = Adapter::schema();
check_adapter_invariants(schema, adapter);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[non_exhaustive]
#[derive(Debug, Clone, trustfall::provider::TrustfallEnumVertex)]
pub enum Vertex {
Comment(()),
Expand Down