Skip to content

Commit

Permalink
Test for nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Jun 8, 2020
1 parent 11a0e89 commit 11ac0cf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/cases/simple.60
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ TestCase instance;
TestCase::default();
```

```js
var x = new sixtyfps.TestCase({});
```

*/
10 changes: 8 additions & 2 deletions tests/driver/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions tests/driver/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
mod cpp;
#[cfg(test)]
mod interpreter;
#[cfg(test)]
mod nodejs;

include!(env!("TEST_FUNCTIONS"));

Expand Down
78 changes: 78 additions & 0 deletions tests/driver/nodejs.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
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(())
}

0 comments on commit 11ac0cf

Please sign in to comment.