summaryrefslogtreecommitdiff
path: root/cli/tests/integration/npm_tests.rs
blob: 7a2b249a11599ee75c5f47f8e32bcaddc220e9c1 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use deno_core::url::Url;
use std::process::Stdio;
use test_util as util;
use util::assert_contains;
use util::http_server;

// NOTE: It's possible to automatically update the npm registry data in the test server
// by setting the DENO_TEST_UTIL_UPDATE_NPM=1 environment variable.

itest!(esm_module {
  args: "run --allow-read npm/esm/main.js",
  output: "npm/esm/main.out",
  envs: env_vars(),
  http_server: true,
});

itest!(esm_module_eval {
  args_vec: vec![
    "eval",
    "import chalk from 'npm:chalk@5'; console.log(chalk.green('chalk esm loads'));",
  ],
  output: "npm/esm/main.out",
  envs: env_vars(),
  http_server: true,
});

itest!(esm_module_deno_test {
  args: "test --allow-read npm/esm/test.js",
  output: "npm/esm/test.out",
  envs: env_vars(),
  http_server: true,
});

itest!(cjs_with_deps {
  args: "run --allow-read --unstable npm/cjs_with_deps/main.js",
  output: "npm/cjs_with_deps/main.out",
  envs: env_vars(),
  http_server: true,
});

itest!(cjs_sub_path {
  args: "run --allow-read --unstable npm/cjs_sub_path/main.js",
  output: "npm/cjs_sub_path/main.out",
  envs: env_vars(),
  http_server: true,
});

itest!(dynamic_import {
  args: "run --allow-read --unstable npm/dynamic_import/main.ts",
  output: "npm/dynamic_import/main.out",
  envs: env_vars(),
  http_server: true,
});

itest!(cached_only {
  args: "run --cached-only --unstable npm/cached_only/main.ts",
  output: "npm/cached_only/main.out",
  envs: env_vars(),
  exit_code: 1,
});

itest!(import_map {
  args: "run --allow-read --unstable --import-map npm/import_map/import_map.json npm/import_map/main.js",
  output: "npm/import_map/main.out",
  envs: env_vars(),
  http_server: true,
});

#[test]
fn parallel_downloading() {
  let (out, _err) = util::run_and_collect_output_with_args(
    true,
    vec![
      "run",
      "--allow-read",
      "--unstable",
      "npm/cjs_with_deps/main.js",
    ],
    None,
    // don't use the sync env var
    Some(env_vars_no_sync_download()),
    true,
  );
  assert!(out.contains("chalk cjs loads"));
}

#[test]
fn cached_only_after_first_run() {
  let _server = http_server();

  let deno_dir = util::new_deno_dir();

  let deno = util::deno_cmd_with_deno_dir(&deno_dir)
    .current_dir(util::testdata_path())
    .arg("run")
    .arg("--unstable")
    .arg("--allow-read")
    .arg("--allow-env")
    .arg("npm/cached_only_after_first_run/main1.ts")
    .env("NO_COLOR", "1")
    .envs(env_vars())
    .stdout(Stdio::piped())
    .stderr(Stdio::piped())
    .spawn()
    .unwrap();
  let output = deno.wait_with_output().unwrap();
  let stderr = String::from_utf8_lossy(&output.stderr);
  let stdout = String::from_utf8_lossy(&output.stdout);
  assert_contains!(stderr, "Download");
  assert_contains!(stdout, "createChalk: chalk");
  assert!(output.status.success());

  let deno = util::deno_cmd_with_deno_dir(&deno_dir)
    .current_dir(util::testdata_path())
    .arg("run")
    .arg("--unstable")
    .arg("--allow-read")
    .arg("--allow-env")
    .arg("--cached-only")
    .arg("npm/cached_only_after_first_run/main2.ts")
    .env("NO_COLOR", "1")
    .envs(env_vars())
    .stdout(Stdio::piped())
    .stderr(Stdio::piped())
    .spawn()
    .unwrap();
  let output = deno.wait_with_output().unwrap();
  let stderr = String::from_utf8_lossy(&output.stderr);
  let stdout = String::from_utf8_lossy(&output.stdout);
  assert_contains!(
    stderr,
    "An npm specifier not found in cache: \"ansi-styles\", --cached-only is specified."
  );
  assert!(stdout.is_empty());
  assert!(!output.status.success());

  let deno = util::deno_cmd_with_deno_dir(&deno_dir)
    .current_dir(util::testdata_path())
    .arg("run")
    .arg("--unstable")
    .arg("--allow-read")
    .arg("--allow-env")
    .arg("--cached-only")
    .arg("npm/cached_only_after_first_run/main1.ts")
    .env("NO_COLOR", "1")
    .envs(env_vars())
    .stdout(Stdio::piped())
    .stderr(Stdio::piped())
    .spawn()
    .unwrap();

  eprintln!("DENO DIR: {}", deno_dir.path().display());
  std::mem::forget(deno_dir);
  let output = deno.wait_with_output().unwrap();
  let stderr = String::from_utf8_lossy(&output.stderr);
  let stdout = String::from_utf8_lossy(&output.stdout);
  eprintln!("stderr {}", stderr);
  eprintln!("stdout {}", stdout);
  assert!(output.status.success());
  assert!(stderr.is_empty());
  assert_contains!(stdout, "createChalk: chalk");
}

#[test]
fn ensure_registry_files_local() {
  // ensures the registry files all point at local tarballs
  let registry_dir_path = util::testdata_path().join("npm").join("registry");
  for entry in std::fs::read_dir(&registry_dir_path).unwrap() {
    let entry = entry.unwrap();
    if entry.metadata().unwrap().is_dir() {
      let registry_json_path = registry_dir_path
        .join(entry.file_name())
        .join("registry.json");
      let file_text = std::fs::read_to_string(&registry_json_path).unwrap();
      if file_text.contains("https://registry.npmjs.org/") {
        panic!(
          "file {} contained a reference to the npm registry",
          registry_json_path.display(),
        );
      }
    }
  }
}

fn std_file_url() -> String {
  let u = Url::from_directory_path(util::std_path()).unwrap();
  u.to_string()
}

fn env_vars_no_sync_download() -> Vec<(String, String)> {
  vec![
    ("DENO_NODE_COMPAT_URL".to_string(), std_file_url()),
    (
      "DENO_NPM_REGISTRY".to_string(),
      "http://localhost:4545/npm/registry/".to_string(),
    ),
  ]
}

fn env_vars() -> Vec<(String, String)> {
  let mut env_vars = env_vars_no_sync_download();
  env_vars.push((
    // make downloads determinstic
    "DENO_UNSTABLE_NPM_SYNC_DOWNLOAD".to_string(),
    "1".to_string(),
  ));
  env_vars
}