2019-09-19 14:48:05 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// TODO(ry) Make this file test-only. Somehow it's very difficult to export
|
2019-10-29 17:52:57 -04:00
|
|
|
// methods to tests/integration_tests.rs if this is enabled...
|
2019-09-19 14:48:05 -04:00
|
|
|
// #![cfg(test)]
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Child;
|
|
|
|
use std::process::Command;
|
2019-10-29 16:06:14 -04:00
|
|
|
use std::process::Stdio;
|
2019-09-19 14:48:05 -04:00
|
|
|
use std::sync::Mutex;
|
2019-11-13 20:06:25 -05:00
|
|
|
use std::sync::MutexGuard;
|
2019-09-19 14:48:05 -04:00
|
|
|
|
|
|
|
lazy_static! {
|
2019-11-13 20:06:25 -05:00
|
|
|
static ref GUARD: Mutex<()> = Mutex::new(());
|
2019-09-19 14:48:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn root_path() -> PathBuf {
|
|
|
|
PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/.."))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn target_dir() -> PathBuf {
|
|
|
|
let current_exe = std::env::current_exe().unwrap();
|
|
|
|
let target_dir = current_exe.parent().unwrap().parent().unwrap();
|
|
|
|
println!("target_dir {}", target_dir.display());
|
|
|
|
target_dir.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deno_exe_path() -> PathBuf {
|
|
|
|
// Something like /Users/rld/src/deno/target/debug/deps/deno
|
|
|
|
let mut p = target_dir().join("deno");
|
|
|
|
if cfg!(windows) {
|
|
|
|
p.set_extension("exe");
|
|
|
|
}
|
|
|
|
p
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:06:25 -05:00
|
|
|
pub struct HttpServerGuard<'a> {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
g: MutexGuard<'a, ()>,
|
|
|
|
child: Child,
|
|
|
|
}
|
2019-09-19 14:48:05 -04:00
|
|
|
|
2019-11-13 20:06:25 -05:00
|
|
|
impl<'a> Drop for HttpServerGuard<'a> {
|
2019-09-19 14:48:05 -04:00
|
|
|
fn drop(&mut self) {
|
2019-11-13 20:06:25 -05:00
|
|
|
match self.child.try_wait() {
|
|
|
|
Ok(None) => {
|
|
|
|
self.child.kill().expect("failed to kill http_server.py");
|
|
|
|
}
|
|
|
|
Ok(Some(status)) => {
|
|
|
|
panic!("http_server.py exited unexpectedly {}", status)
|
|
|
|
}
|
|
|
|
Err(e) => panic!("http_server.py err {}", e),
|
2019-09-19 14:48:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:06:25 -05:00
|
|
|
/// Starts tools/http_server.py when the returned guard is dropped, the server
|
|
|
|
/// will be killed.
|
|
|
|
pub fn http_server<'a>() -> HttpServerGuard<'a> {
|
|
|
|
// TODO(ry) Allow tests to use the http server in parallel.
|
|
|
|
let g = GUARD.lock().unwrap();
|
2019-09-19 14:48:05 -04:00
|
|
|
|
2019-11-13 20:06:25 -05:00
|
|
|
println!("tools/http_server.py starting...");
|
|
|
|
let mut child = Command::new("python")
|
|
|
|
.current_dir(root_path())
|
|
|
|
.args(&["-u", "tools/http_server.py"])
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("failed to execute child");
|
2019-09-19 14:48:05 -04:00
|
|
|
|
2019-11-13 20:06:25 -05:00
|
|
|
let stdout = child.stdout.as_mut().unwrap();
|
|
|
|
use std::io::{BufRead, BufReader};
|
|
|
|
let mut lines = BufReader::new(stdout).lines();
|
|
|
|
let line = lines.next().unwrap().unwrap();
|
|
|
|
assert!(line.starts_with("ready"));
|
|
|
|
|
|
|
|
HttpServerGuard { child, g }
|
2019-09-19 14:48:05 -04:00
|
|
|
}
|