summaryrefslogtreecommitdiff
path: root/test_napi/tests/napi_tests.rs
blob: 0b0c9d311a04e7357b806b215754fc710fa7fb11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use std::process::Command;
use test_util::deno_cmd;
use test_util::env_vars_for_npm_tests;
use test_util::http_server;

#[cfg(debug_assertions)]
const BUILD_VARIANT: &str = "debug";

#[cfg(not(debug_assertions))]
const BUILD_VARIANT: &str = "release";

fn build() {
  let mut build_plugin_base = Command::new("cargo");
  let mut build_plugin =
    build_plugin_base.arg("build").arg("-p").arg("test_napi");
  if BUILD_VARIANT == "release" {
    build_plugin = build_plugin.arg("--release");
  }
  let build_plugin_output = build_plugin.output().unwrap();
  assert!(build_plugin_output.status.success());

  // cc module.c -undefined dynamic_lookup -shared -Wl,-no_fixup_chains -dynamic -o module.dylib
  #[cfg(not(target_os = "windows"))]
  {
    let out = if cfg!(target_os = "macos") {
      "module.dylib"
    } else {
      "module.so"
    };

    let mut cc = Command::new("cc");

    #[cfg(not(target_os = "macos"))]
    let c_module = cc.arg("module.c").arg("-shared").arg("-o").arg(out);

    #[cfg(target_os = "macos")]
    let c_module = {
      cc.arg("module.c")
        .arg("-undefined")
        .arg("dynamic_lookup")
        .arg("-shared")
        .arg("-Wl,-no_fixup_chains")
        .arg("-dynamic")
        .arg("-o")
        .arg(out)
    };
    let c_module_output = c_module.output().unwrap();
    assert!(c_module_output.status.success());
  }
}

#[test]
fn napi_tests() {
  build();

  let _http_guard = http_server();
  let output = deno_cmd()
    .current_dir(test_util::napi_tests_path())
    .env("RUST_BACKTRACE", "1")
    .arg("test")
    .arg("--allow-read")
    .arg("--allow-env")
    .arg("--allow-ffi")
    .arg("--allow-run")
    .envs(env_vars_for_npm_tests())
    .spawn()
    .unwrap()
    .wait_with_output()
    .unwrap();
  let stdout = std::str::from_utf8(&output.stdout).unwrap();
  let stderr = std::str::from_utf8(&output.stderr).unwrap();

  if !output.status.success() {
    eprintln!("exit code {:?}", output.status.code());
    println!("stdout {stdout}");
    println!("stderr {stderr}");
  }
  assert!(output.status.success());
}