From 11ac0cfad02f5fb611cfaecf74eb0d1669741ac8 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 8 Jun 2020 12:24:39 +0200 Subject: [PATCH] Test for nodejs --- tests/cases/simple.60 | 4 +++ tests/driver/build.rs | 10 ++++-- tests/driver/main.rs | 2 ++ tests/driver/nodejs.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 tests/driver/nodejs.rs diff --git a/tests/cases/simple.60 b/tests/cases/simple.60 index dc087d19fb0..1000d2c28f6 100644 --- a/tests/cases/simple.60 +++ b/tests/cases/simple.60 @@ -11,4 +11,8 @@ TestCase instance; TestCase::default(); ``` +```js +var x = new sixtyfps.TestCase({}); +``` + */ diff --git a/tests/driver/build.rs b/tests/driver/build.rs index 21eec363a95..6a0c0c2a222 100644 --- a/tests/driver/build.rs +++ b/tests/driver/build.rs @@ -33,8 +33,6 @@ fn main() -> std::io::Result<()> { let mut test_dirs = std::collections::HashSet::new(); for testcase in test_driver_lib::collect_test_cases()? { - println!("cargo:rerun-if-changed={}", testcase.absolute_path.to_string_lossy()); - test_dirs.insert({ let mut dir = testcase.absolute_path.clone(); dir.pop(); @@ -62,6 +60,14 @@ fn main() -> std::io::Result<()> { relative_path: std::path::PathBuf::from("{relative_path}"), }}).unwrap(); }} + + #[test] + fn test_nodejs_{function_name}() {{ + nodejs::test(&test_driver_lib::TestCase{{ + absolute_path: std::path::PathBuf::from("{absolute_path}"), + relative_path: std::path::PathBuf::from("{relative_path}"), + }}).unwrap(); + }} "#, function_name = test_function_name, absolute_path = testcase.absolute_path.to_string_lossy(), diff --git a/tests/driver/main.rs b/tests/driver/main.rs index e416cd24770..4d6f2ec731d 100644 --- a/tests/driver/main.rs +++ b/tests/driver/main.rs @@ -2,6 +2,8 @@ mod cpp; #[cfg(test)] mod interpreter; +#[cfg(test)] +mod nodejs; include!(env!("TEST_FUNCTIONS")); diff --git a/tests/driver/nodejs.rs b/tests/driver/nodejs.rs new file mode 100644 index 00000000000..dcb02131983 --- /dev/null +++ b/tests/driver/nodejs.rs @@ -0,0 +1,78 @@ +use std::error::Error; +use std::{fs::File, io::Write, path::PathBuf}; + +pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box> { + let dir = tempfile::tempdir()?; + + let mut sixtyfpsdir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + sixtyfpsdir.pop(); // driver + sixtyfpsdir.pop(); // tests + sixtyfpsdir.push("api/sixtyfps-node"); + + { + let mut package_json = File::create(dir.path().join("package.json"))?; + write!( + package_json, + r#" +{{ + "name": "sixtyfps_test_{testcase}", + "version": "0.1.0", + "main": "main.js", + "dependencies": {{ + "sixtyfps": "{sixtyfpsdir}" + }}, + "scripts": {{ + "start": "node ." + }} +}}"#, + testcase = testcase.relative_path.file_stem().unwrap().to_string_lossy(), + sixtyfpsdir = sixtyfpsdir.to_string_lossy(), + )?; + } + { + let mut main_js = File::create(dir.path().join("main.js"))?; + write!( + main_js, + r#" +const assert = require('assert').strict; +require("sixtyfps"); +let sixtyfps = require("{path}"); + +"#, + path = testcase.absolute_path.to_string_lossy() + )?; + let source = std::fs::read_to_string(&testcase.absolute_path)?; + for x in test_driver_lib::extract_test_functions(&source).filter(|x| x.language_id == "js") + { + write!(main_js, "{{\n {}\n}}\n", x.source.replace("\n", "\n "))?; + } + } + + let install_output = std::process::Command::new("npm") + .arg("install") + .current_dir(dir.path()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .output() + .map_err(|err| format!("Could not launch npm install: {}", err))?; + + if !install_output.status.success() { + print!("{}", String::from_utf8_lossy(install_output.stderr.as_ref())); + return Err("npm install failed!".to_owned().into()); + } + + let install_output = std::process::Command::new("npm") + .arg("start") + .current_dir(dir.path()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .output() + .map_err(|err| format!("Could not launch npm start: {}", err))?; + + if !install_output.status.success() { + print!("{}", String::from_utf8_lossy(install_output.stderr.as_ref())); + return Err(String::from_utf8_lossy(install_output.stderr.as_ref()).to_owned().into()); + } + + Ok(()) +}