summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-07-07 16:50:35 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-07-07 17:24:46 -0400
commit6bff970d69480272102b3aba8c88e287fcebbcd5 (patch)
tree2a5b467bc6dba04a32244d265c96b7cce2f10d6a
parenta2dde56c5961f451b9042c1f651af372c4984034 (diff)
Add rust_test to gn build, with working example.
-rw-r--r--.travis.yml3
-rw-r--r--BUILD.gn5
-rw-r--r--build_extra/rust/rust.gni69
-rw-r--r--src/handlers.rs5
4 files changed, 64 insertions, 18 deletions
diff --git a/.travis.yml b/.travis.yml
index c3ad1e8c4..64654912c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -42,9 +42,10 @@ install:
- gn args $BUILD_PATH --list
- ccache -s
# Travis hangs without -j2 argument to ninja.
- - ninja -j2 -C $BUILD_PATH mock_runtime_test deno_cc deno
+ - ninja -j2 -C $BUILD_PATH mock_runtime_test handlers_test deno_cc deno
script:
- $BUILD_PATH/mock_runtime_test
+ - $BUILD_PATH/handlers_test
- $BUILD_PATH/deno_cc foo bar
- $BUILD_PATH/deno meow
- ./tools/lint.sh
diff --git a/BUILD.gn b/BUILD.gn
index b76439f38..9ff3d1011 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -34,6 +34,11 @@ rust_component("handlers") {
extern = [ ":libc" ]
}
+rust_test("handlers_test") {
+ source_root = "src/handlers.rs"
+ extern = [ ":libc" ]
+}
+
executable("deno_cc") {
sources = [
"src/main.cc",
diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni
index 985fff5f6..20641fadc 100644
--- a/build_extra/rust/rust.gni
+++ b/build_extra/rust/rust.gni
@@ -10,7 +10,11 @@ template("run_rustc") {
"source_root",
"deps",
"extern",
+ "is_test",
])
+ if (defined(invoker.testonly)) {
+ testonly = invoker.testonly
+ }
if (defined(invoker.crate_name)) {
crate_name = invoker.crate_name
} else {
@@ -21,7 +25,6 @@ template("run_rustc") {
source_root,
]
outputs = []
- depfile = "$target_out_dir/$target_name.d"
script = "//third_party/v8/tools/run.py"
args = [
@@ -29,26 +32,41 @@ template("run_rustc") {
rebase_path(source_root, root_build_dir),
"--crate-name=$crate_name",
"--crate-type=$crate_type",
- "--emit=dep-info=" + rebase_path(depfile, root_build_dir),
]
- # We only use staticlib for the special "empty" lib.
- if (crate_type == "staticlib") {
- staticlib = "$target_out_dir/$crate_name.a"
- outputs += [ staticlib ]
- args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ]
- }
+ if (defined(is_test) && is_test) {
+ # Test outputs are executables which should be in root_out_dir.
+ output = "$root_out_dir/$crate_name"
+ args += [
+ "--test",
+ "-o",
+ rebase_path(output, root_build_dir),
+ ]
+ outputs += [ output ]
+ } else {
+ # Non-test targets are handled differently.
- if (crate_type == "rlib" || crate_type == "bin") {
- obj = "$target_out_dir/$crate_name.o"
- outputs += [ obj ]
- args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ]
- }
+ # For unknown reasons emitting a depfile on tests doesn't work.
+ depfile = "$target_out_dir/$target_name.d"
+ args += [ "--emit=dep-info=" + rebase_path(depfile, root_build_dir) ]
+
+ if (crate_type == "staticlib") {
+ staticlib = "$target_out_dir/$crate_name.a"
+ outputs += [ staticlib ]
+ args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ]
+ }
+
+ if (crate_type == "rlib" || crate_type == "bin") {
+ obj = "$target_out_dir/$crate_name.o"
+ outputs += [ obj ]
+ args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ]
+ }
- if (crate_type == "rlib") {
- rlib = "$target_out_dir/lib$crate_name.rlib"
- outputs += [ rlib ]
- args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ]
+ if (crate_type == "rlib") {
+ rlib = "$target_out_dir/lib$crate_name.rlib"
+ outputs += [ rlib ]
+ args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ]
+ }
}
if (is_debug) {
@@ -99,6 +117,7 @@ template("rust_component") {
"extern",
"cfg",
"source_root",
+ "testonly",
])
if (!defined(invoker.crate_type)) {
crate_type = "rlib"
@@ -113,6 +132,7 @@ template("rust_component") {
[
"libs",
"deps",
+ "testonly",
])
if (!defined(deps)) {
deps = []
@@ -151,3 +171,18 @@ template("rust_executable") {
}
}
}
+
+template("rust_test") {
+ run_rustc(target_name) {
+ crate_name = target_name
+ crate_type = "bin"
+ testonly = true
+ is_test = true
+ forward_variables_from(invoker,
+ [
+ "extern",
+ "cfg",
+ "source_root",
+ ])
+ }
+}
diff --git a/src/handlers.rs b/src/handlers.rs
index 64a076f11..405fe29da 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -10,6 +10,11 @@ fn string_from_ptr(ptr: *const c_char) -> String {
String::from(cstr.to_str().unwrap())
}
+#[test]
+fn test_example() {
+ assert_eq!(2 + 2, 4);
+}
+
#[no_mangle]
pub extern "C" fn handle_code_fetch(
module_specifier: *const c_char,