Skip to content

Commit 1e3e2c7

Browse files
authored
Write files for large test expectations; Move data to _test_data (#217)
1 parent 6c420fb commit 1e3e2c7

File tree

3 files changed

+102
-32
lines changed

3 files changed

+102
-32
lines changed

partiql-conformance-test-generator/src/generator.rs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub enum TestTree {
4747
#[derive(Debug)]
4848
pub enum Node {
4949
Test(TestNode),
50-
Env(EnvNode),
50+
Value(TestValueNode),
5151
}
5252

5353
#[derive(Debug)]
@@ -56,8 +56,8 @@ pub struct TestNode {
5656
}
5757

5858
#[derive(Debug)]
59-
pub struct EnvNode {
60-
pub env: String,
59+
pub struct TestValueNode {
60+
pub value: String,
6161
}
6262

6363
#[derive(Debug, Default)]
@@ -98,6 +98,10 @@ pub struct Generator {
9898
seen_fns: Vec<HashSet<String>>,
9999
}
100100

101+
const TEST_DATA_DIR: &'static str = "_test_data";
102+
const ENV_INLINE_LOWER_BOUND_LINE_COUNT: usize = 10;
103+
const EXPECTED_INLINE_LOWER_BOUND_LINE_COUNT: usize = 25;
104+
101105
impl Generator {
102106
pub fn new(config: GeneratorConfig) -> Generator {
103107
Self {
@@ -251,6 +255,29 @@ impl Generator {
251255
}
252256

253257
fn gen_envs(&mut self, scope: &mut Scope, envs: &Environments) {
258+
let envs = struct_to_string(&envs.envs);
259+
260+
if envs.lines().count() < ENV_INLINE_LOWER_BOUND_LINE_COUNT {
261+
self.gen_envs_inline(scope, envs);
262+
} else {
263+
self.gen_envs_external(scope, envs);
264+
}
265+
}
266+
267+
fn gen_envs_inline(&mut self, scope: &mut Scope, envs: String) {
268+
scope.raw(
269+
quote! {
270+
const ENV_ION_TEXT : &'static str = #envs;
271+
fn environment() -> Option<TestValue<'static>> {
272+
Some(ENV_ION_TEXT.into())
273+
}
274+
}
275+
.to_string()
276+
.replace("\\n", "\n"),
277+
);
278+
}
279+
280+
fn gen_envs_external(&mut self, scope: &mut Scope, envs: String) {
254281
let env_file = self
255282
.curr_mod_path
256283
.iter()
@@ -259,9 +286,10 @@ impl Generator {
259286
.join("___")
260287
+ ".env.ion";
261288

289+
let data_file = format!("{}/{}", TEST_DATA_DIR, env_file);
262290
scope.raw(
263291
quote! {
264-
const ENV_ION_TEXT : &'static str = include_str!(#env_file);
292+
const ENV_ION_TEXT : &'static str = include_str!(#data_file);
265293
fn environment() -> Option<TestValue<'static>> {
266294
Some(ENV_ION_TEXT.into())
267295
}
@@ -270,15 +298,18 @@ impl Generator {
270298
.replace("\\n", "\n"),
271299
);
272300

301+
let td_dir = TEST_DATA_DIR.to_string();
273302
let env_path: Vec<_> = self
274303
.curr_path
275304
.iter()
305+
.chain(std::iter::once(&td_dir))
276306
.chain(std::iter::once(&env_file))
277307
.collect();
278-
let env = struct_to_string(&envs.envs);
279308

280-
self.result
281-
.insert(env_path.as_slice(), Node::Env(EnvNode { env }));
309+
self.result.insert(
310+
env_path.as_slice(),
311+
Node::Value(TestValueNode { value: envs }),
312+
);
282313
}
283314

284315
fn gen_equivs(&mut self, _scope: &mut Scope, equivs: &EquivalenceClass) {
@@ -378,6 +409,36 @@ impl Generator {
378409
test_fn.line("\n//**** evaluation success test case(s) ****//");
379410

380411
let expected = elt_to_string(output);
412+
let expected =
413+
if expected.lines().count() > EXPECTED_INLINE_LOWER_BOUND_LINE_COUNT {
414+
let expected_file = self
415+
.curr_mod_path
416+
.iter()
417+
.map(|s| s.escape_path())
418+
.chain(std::iter::once(test_case.name.escape_path()))
419+
.collect::<Vec<_>>()
420+
.join("___")
421+
+ ".expected.ion";
422+
423+
let td_dir = TEST_DATA_DIR.to_string();
424+
let expected_path: Vec<_> = self
425+
.curr_path
426+
.iter()
427+
.chain(std::iter::once(&td_dir))
428+
.chain(std::iter::once(&expected_file))
429+
.collect();
430+
431+
self.result.insert(
432+
expected_path.as_slice(),
433+
Node::Value(TestValueNode { value: expected }),
434+
);
435+
436+
let data_file = format!("{}/{}", TEST_DATA_DIR, expected_file);
437+
quote! {include_str!(#data_file)}
438+
} else {
439+
quote! {#expected}
440+
};
441+
381442
let modes: Vec<_> = eval_mode
382443
.into_iter()
383444
.map(|mode| match mode {

partiql-conformance-test-generator/src/reader.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ impl TryFrom<&Struct> for TestCase {
199199
}
200200
};
201201

202-
//let statement = expect_str!(value.get("statement"), "TestCase statement").into();
203202
let env = if let Some(v) = value.get("env") {
204203
Some(expect_struct!(Some(v), "TestCase envs").clone())
205204
} else {

partiql-conformance-test-generator/src/writer.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::util::Escaper;
44
use codegen::Scope;
55
use miette::IntoDiagnostic;
66

7-
use std::fs::File;
7+
use std::fs::{DirBuilder, File};
88
use std::io::Write;
99
use std::path::{Path, PathBuf};
1010

@@ -14,6 +14,8 @@ const FILE_HEADER: &str = "\
1414
// ********************************************************************************************** //
1515
1616
#[allow(unused_imports)]
17+
use super::*;
18+
1719
";
1820

1921
#[derive(Debug)]
@@ -24,50 +26,55 @@ impl Writer {
2426
Self {}
2527
}
2628

27-
pub fn write(&self, path: impl AsRef<Path>, root: NamespaceNode) -> miette::Result<()> {
29+
pub fn write(&self, path: impl AsRef<Path>, mut root: NamespaceNode) -> miette::Result<()> {
2830
let path: PathBuf = path
2931
.as_ref()
3032
.components()
3133
.map(|c| c.as_os_str().to_string_lossy().to_string().escape_path())
3234
.collect();
3335
std::fs::create_dir_all(&path).into_diagnostic()?;
3436

35-
write_module(&path, root)?;
37+
write_module(&path, &mut root)?;
3638

3739
Ok(())
3840
}
3941
}
4042

41-
fn write_module(path: impl AsRef<Path>, module: NamespaceNode) -> miette::Result<()> {
42-
let sub_mods = module.children.iter().filter_map(|(name, tree)| {
43-
if let TestTree::Node(Node::Env(_)) = tree {
44-
None
45-
} else {
46-
Some(name)
47-
}
48-
});
49-
write_dir_mod(&path, sub_mods)?;
43+
fn write_module(path: impl AsRef<Path>, module: &mut NamespaceNode) -> miette::Result<bool> {
44+
DirBuilder::new()
45+
.recursive(true)
46+
.create(&path)
47+
.into_diagnostic()?;
48+
49+
let mut sub_mods = vec![];
5050

51-
for (name, child) in module.children {
51+
for (name, child) in &mut module.children {
5252
let mut child_path: PathBuf = path.as_ref().into();
5353
child_path.push(&name);
54-
match child {
55-
TestTree::Node(Node::Test(mut s)) => write_scope(child_path, s.module.scope())?,
56-
TestTree::Node(Node::Env(e)) => write_file(child_path, &e.env)?,
54+
let is_sub_mod = match child {
55+
TestTree::Node(Node::Test(s)) => write_scope(child_path, s.module.scope())?,
56+
TestTree::Node(Node::Value(e)) => write_file(child_path, &e.value)?,
5757
TestTree::Namespace(m) => write_module(child_path, m)?,
58+
};
59+
if is_sub_mod {
60+
sub_mods.push(name.clone());
5861
}
5962
}
60-
Ok(())
63+
64+
if !sub_mods.is_empty() {
65+
write_dir_mod(&path, sub_mods.iter())?;
66+
}
67+
68+
Ok(!sub_mods.is_empty())
6169
}
6270

6371
fn write_dir_mod<'a>(
6472
path: impl AsRef<Path>,
6573
sub_mods: impl Iterator<Item = &'a String>,
66-
) -> miette::Result<()> {
74+
) -> miette::Result<bool> {
6775
std::fs::create_dir_all(&path).into_diagnostic()?;
6876

6977
let mut contents = FILE_HEADER.to_string();
70-
contents.push_str("use super::*;");
7178
for sub_mod in sub_mods {
7279
contents.push_str(&format!("mod {};\n", sub_mod.replace(".rs", "")))
7380
}
@@ -76,16 +83,19 @@ fn write_dir_mod<'a>(
7683
File::create(file_path)
7784
.into_diagnostic()?
7885
.write_all(contents.as_bytes())
79-
.into_diagnostic()
86+
.into_diagnostic()?;
87+
Ok(true)
8088
}
8189

82-
fn write_scope(path: impl AsRef<Path>, scope: &Scope) -> miette::Result<()> {
90+
fn write_scope(path: impl AsRef<Path>, scope: &Scope) -> miette::Result<bool> {
8391
let contents = format!("{}{}", FILE_HEADER, scope.to_string());
84-
write_file(path, &contents)
92+
write_file(path, &contents)?;
93+
Ok(true)
8594
}
8695

87-
fn write_file(path: impl AsRef<Path>, contents: &str) -> miette::Result<()> {
96+
fn write_file(path: impl AsRef<Path>, contents: &str) -> miette::Result<bool> {
8897
let mut file = File::create(path).into_diagnostic()?;
8998
file.write_all(contents.as_bytes()).into_diagnostic()?;
90-
file.sync_all().into_diagnostic()
99+
file.sync_all().into_diagnostic()?;
100+
Ok(false)
91101
}

0 commit comments

Comments
 (0)