Skip to content

Commit

Permalink
remove more calls to tokio_util::block_on (#3059)
Browse files Browse the repository at this point in the history
towards #2960
  • Loading branch information
bartlomieju authored and ry committed Oct 6, 2019
1 parent 3e02d7d commit e1d49fe
Show file tree
Hide file tree
Showing 4 changed files with 568 additions and 520 deletions.
92 changes: 48 additions & 44 deletions cli/compilers/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,50 +656,46 @@ mod tests {
use crate::fs as deno_fs;
use crate::tokio_util;
use deno::ModuleSpecifier;
use futures::future::lazy;
use std::path::PathBuf;
use tempfile::TempDir;

impl TsCompiler {
fn compile_sync(
self: &Self,
state: ThreadSafeState,
source_file: &SourceFile,
) -> Result<CompiledModule, ErrBox> {
tokio_util::block_on(self.compile_async(state, source_file))
}
}

#[test]
fn test_compile_sync() {
tokio_util::init(|| {
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tests/002_hello.ts")
.to_owned();
let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();

let out = SourceFile {
url: specifier.as_url().clone(),
filename: PathBuf::from(p.to_str().unwrap().to_string()),
media_type: msg::MediaType::TypeScript,
source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
};
fn test_compile_async() {
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tests/002_hello.ts")
.to_owned();
let specifier =
ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();

let out = SourceFile {
url: specifier.as_url().clone(),
filename: PathBuf::from(p.to_str().unwrap().to_string()),
media_type: msg::MediaType::TypeScript,
source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
};

let mock_state = ThreadSafeState::mock(vec![
String::from("deno"),
String::from("hello.js"),
]);
let compiled = mock_state
let mock_state = ThreadSafeState::mock(vec![
String::from("deno"),
String::from("hello.js"),
]);

tokio_util::run(lazy(move || {
mock_state
.ts_compiler
.compile_sync(mock_state.clone(), &out)
.unwrap();
assert!(compiled
.code
.as_bytes()
.starts_with("console.log(\"Hello World\");".as_bytes()));
})
.compile_async(mock_state.clone(), &out)
.then(|result| {
assert!(result.is_ok());
assert!(result
.unwrap()
.code
.as_bytes()
.starts_with("console.log(\"Hello World\");".as_bytes()));
Ok(())
})
}))
}

#[test]
Expand All @@ -719,12 +715,20 @@ mod tests {
p.to_string_lossy().into(),
String::from("$deno$/bundle.js"),
]);
let out = state.ts_compiler.bundle_async(
state.clone(),
module_name,
String::from("$deno$/bundle.js"),
);
assert!(tokio_util::block_on(out).is_ok());

tokio_util::run(lazy(move || {
state
.ts_compiler
.bundle_async(
state.clone(),
module_name,
String::from("$deno$/bundle.js"),
)
.then(|result| {
assert!(result.is_ok());
Ok(())
})
}))
}

#[test]
Expand Down
Loading

0 comments on commit e1d49fe

Please sign in to comment.