chore(cli): use NPM_CONFIG_REGISTRY for all tests (#20320)

We never want tests to hit the real npm registry because this causes
test flakes. In addition, we set a sentinal "unset" value for
`NPM_CONFIG_REGISTRY` to ensure that all tests requiring npm go through
the test server.
This commit is contained in:
Matt Mastracci 2023-08-29 13:02:54 -06:00 committed by GitHub
parent 441b860978
commit 64045ebc7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 1 deletions

View file

@ -86,13 +86,17 @@ itest!(check_static_response_json {
itest!(check_node_builtin_modules_ts {
args: "check --quiet check/node_builtin_modules/mod.ts",
output: "check/node_builtin_modules/mod.ts.out",
envs: env_vars_for_npm_tests(),
exit_code: 1,
http_server: true,
});
itest!(check_node_builtin_modules_js {
args: "check --quiet check/node_builtin_modules/mod.js",
output: "check/node_builtin_modules/mod.js.out",
envs: env_vars_for_npm_tests(),
exit_code: 1,
http_server: true,
});
itest!(check_no_error_truncation {

View file

@ -3,6 +3,7 @@
use std::fs;
use test_util as util;
use test_util::TempDir;
use util::env_vars_for_npm_tests;
use util::TestContext;
use util::TestContextBuilder;
@ -326,7 +327,7 @@ fn no_tests_included(test_name: &str, extension: &str) {
#[test]
fn no_npm_cache_coverage() {
let context = TestContext::default();
let context = TestContext::with_http_server();
let tempdir = context.temp_dir();
let tempdir = tempdir.path().join("cov");
@ -339,6 +340,7 @@ fn no_npm_cache_coverage() {
format!("--coverage={}", tempdir),
format!("coverage/no_npm_coverage/no_npm_coverage_test.ts"),
])
.envs(env_vars_for_npm_tests())
.run();
output.assert_exit_code(0);

View file

@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use test_util as util;
use util::env_vars_for_npm_tests;
#[test]
fn node_compat_tests() {
@ -21,5 +22,7 @@ fn node_compat_tests() {
itest!(node_test_module {
args: "test node/test.js",
output: "node/test.out",
envs: env_vars_for_npm_tests(),
exit_code: 1,
http_server: true,
});

View file

@ -109,6 +109,7 @@ fn node_unit_test(test: String) {
.join("unit_node")
.join(format!("{test}.ts")),
)
.envs(env_vars_for_npm_tests())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.spawn()

View file

@ -2,6 +2,8 @@
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";
@ -53,6 +55,7 @@ fn build() {
fn napi_tests() {
build();
let _http_guard = http_server();
let output = deno_cmd()
.current_dir(test_util::napi_tests_path())
.env("RUST_BACKTRACE", "1")
@ -61,6 +64,7 @@ fn napi_tests() {
.arg("--allow-env")
.arg("--allow-ffi")
.arg("--allow-run")
.envs(env_vars_for_npm_tests())
.spawn()
.unwrap()
.wait_with_output()

View file

@ -19,6 +19,7 @@ use crate::env_vars_for_npm_tests_no_sync_download;
use crate::fs::PathRef;
use crate::http_server;
use crate::lsp::LspClientBuilder;
use crate::npm_registry_unset_url;
use crate::pty::Pty;
use crate::strip_ansi_codes;
use crate::testdata_path;
@ -266,6 +267,17 @@ impl TestCommandBuilder {
self
}
pub fn envs<S: AsRef<OsStr>>(
self,
envs: impl IntoIterator<Item = (S, S)>,
) -> Self {
let mut this = self;
for (k, v) in envs {
this = this.env(k, v);
}
this
}
pub fn env_clear(mut self) -> Self {
self.env_clear = true;
self
@ -391,6 +403,10 @@ impl TestCommandBuilder {
command.env_clear();
}
command.env("DENO_DIR", self.context.deno_dir.path());
let envs = self.build_envs();
if !envs.contains_key("NPM_CONFIG_REGISTRY") {
command.env("NPM_CONFIG_REGISTRY", npm_registry_unset_url());
}
command.envs(self.build_envs());
command.current_dir(cwd);
command.stdin(Stdio::piped());

View file

@ -160,6 +160,10 @@ pub fn npm_registry_url() -> String {
"http://localhost:4545/npm/registry/".to_string()
}
pub fn npm_registry_unset_url() -> String {
"http://NPM_CONFIG_REGISTRY.is.unset".to_string()
}
pub fn std_path() -> PathRef {
root_path().join("test_util").join("std")
}
@ -2309,6 +2313,7 @@ pub fn deno_cmd_with_deno_dir(deno_dir: &TempDir) -> DenoCmd {
assert!(exe_path.exists());
let mut cmd = Command::new(exe_path);
cmd.env("DENO_DIR", deno_dir.path());
cmd.env("NPM_CONFIG_REGISTRY", npm_registry_unset_url());
DenoCmd {
_deno_dir: deno_dir.clone(),
cmd,