Skip to content

Commit ecb9559

Browse files
committed
Normalize windows paths.
Add resolve_module test
1 parent e2f9b0e commit ecb9559

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/deno_dir.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -250,29 +250,15 @@ impl DenoDir {
250250

251251
match j.scheme() {
252252
"file" => {
253-
let mut p = j
254-
.to_file_path()
255-
.unwrap()
256-
.into_os_string()
257-
.into_string()
258-
.unwrap();
259-
260-
if cfg!(target_os = "windows") {
261-
// On windows, replace backward slashes to forward slashes.
262-
// TODO(piscisaureus): This may not me be right, I just did it to make
263-
// the tests pass.
264-
p = p.replace("\\", "/");
265-
}
266-
267-
module_name = p.to_string();
268-
filename = p.to_string();
253+
let mut p = fs::normalize_path(j.to_file_path().unwrap().as_ref());
254+
module_name = p.clone();
255+
filename = p;
269256
}
270257
_ => {
271258
module_name = module_specifier.to_string();
272-
filename = get_cache_filename(self.deps.as_path(), j)
273-
.to_str()
274-
.unwrap()
275-
.to_string();
259+
filename = fs::normalize_path(
260+
get_cache_filename(self.deps.as_path(), j).as_ref(),
261+
)
276262
}
277263
}
278264

@@ -422,6 +408,13 @@ fn test_src_file_to_url() {
422408
fn test_resolve_module() {
423409
let (_temp_dir, deno_dir) = test_setup();
424410

411+
let d = fs::normalize_path(
412+
deno_dir
413+
.deps
414+
.join("localhost/testdata/subdir/print_hello.ts")
415+
.as_ref(),
416+
);
417+
425418
let test_cases = [
426419
(
427420
"./subdir/print_hello.ts",
@@ -453,13 +446,13 @@ fn test_resolve_module() {
453446
add_root!("/this/module/got/imported.js"),
454447
add_root!("/this/module/got/imported.js"),
455448
),
449+
(
450+
"http://localhost:4545/testdata/subdir/print_hello.ts",
451+
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"),
452+
"http://localhost:4545/testdata/subdir/print_hello.ts",
453+
d.as_ref(),
454+
),
456455
/*
457-
(
458-
"http://localhost:4545/testdata/subdir/print_hello.ts",
459-
add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"),
460-
"http://localhost:4545/testdata/subdir/print_hello.ts",
461-
path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts"),
462-
),
463456
(
464457
path.Join(SrcDir, "unpkg.com/[email protected]/index.ts"),
465458
".",

src/fs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ pub fn mkdir(path: &Path) -> std::io::Result<()> {
3434
}
3535
})
3636
}
37+
38+
pub fn normalize_path(path: &Path) -> String {
39+
let s = String::from(path.to_str().unwrap());
40+
if cfg!(windows) {
41+
// TODO This isn't correct. Probbly should iterate over components.
42+
s.replace("\\", "/")
43+
} else {
44+
s
45+
}
46+
}

src/handlers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ fn handle_start(
127127
let argv_off = builder.create_vector_of_strings(argv.as_slice());
128128

129129
let cwd_path = std::env::current_dir().unwrap();
130-
let cwd_off = builder.create_string(cwd_path.to_str().unwrap());
130+
let cwd_off =
131+
builder.create_string(fs::normalize_path(cwd_path.as_ref()).as_ref());
131132

132133
let msg = msg::StartRes::create(
133134
builder,

0 commit comments

Comments
 (0)