Skip to content

Commit

Permalink
add tests, fix inspection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Oct 26, 2021
1 parent ca84b18 commit 9d87592
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 8 deletions.
7 changes: 4 additions & 3 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub fn create_main_worker(

let create_web_worker_cb = create_web_worker_callback(ps.clone());

let maybe_storage_origin = if let Some(location) = &ps.flags.location {
let maybe_storage_key = if let Some(location) = &ps.flags.location {
// if a location is set, then the ascii serialization of the location is
// used, unless the origin is opaque, and then no storage origin is set, as
// we can't expect the origin to be reproducible
Expand All @@ -222,11 +222,12 @@ pub fn create_main_worker(
Some(main_module.to_string())
};

let origin_storage_dir = maybe_storage_origin.map(|so| {
let origin_storage_dir = maybe_storage_key.map(|key| {
ps.dir
.root
// TODO(@crowlKats): change to origin_data for 2.0
.join("location_data")
.join(checksum::gen(&[so.as_bytes()]))
.join(checksum::gen(&[key.as_bytes()]))
});

let options = WorkerOptions {
Expand Down
190 changes: 186 additions & 4 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,192 @@ itest!(_071_location_unset {
});

itest!(_072_location_relative_fetch {
args: "run --location http://127.0.0.1:4545/ --allow-net 072_location_relative_fetch.ts",
output: "072_location_relative_fetch.ts.out",
http_server: true,
});
args: "run --location http://127.0.0.1:4545/ --allow-net 072_location_relative_fetch.ts",
output: "072_location_relative_fetch.ts.out",
http_server: true,
});

// tests the serialization of webstorage (both localStorage and sessionStorage)
itest!(webstorage_serialization {
args: "run webstorage/serialization.ts",
output: "webstorage/serialization.ts.out",
});

// tests to ensure that when `--location` is set, all code shares the same
// localStorage cache based on the origin of the location URL.
#[test]
fn webstorage_location_shares_origin() {
let deno_dir = util::new_deno_dir();

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--location")
.arg("https://example.com/a.ts")
.arg("webstorage/fixture.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--location")
.arg("https://example.com/b.ts")
.arg("webstorage/logger.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 1, hello: \"deno\" }\n");
}

// test to ensure that when a --config file is set, but no --location, that
// storage persists against unique configuration files.
#[test]
fn webstorage_config_file() {
let deno_dir = util::new_deno_dir();

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--config")
.arg("webstorage/config_a.jsonc")
.arg("webstorage/fixture.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--config")
.arg("webstorage/config_b.jsonc")
.arg("webstorage/logger.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--config")
.arg("webstorage/config_a.jsonc")
.arg("webstorage/logger.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 1, hello: \"deno\" }\n");
}

// tests to ensure `--config` does not effect persisted storage when a
// `--location` is provided.
#[test]
fn webstorage_location_precedes_config() {
let deno_dir = util::new_deno_dir();

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--location")
.arg("https://example.com/a.ts")
.arg("--config")
.arg("webstorage/config_a.jsonc")
.arg("webstorage/fixture.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("--location")
.arg("https://example.com/b.ts")
.arg("--config")
.arg("webstorage/config_b.jsonc")
.arg("webstorage/logger.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 1, hello: \"deno\" }\n");
}

// test to ensure that when there isn't a configuration or location, that the
// main module is used to determine how to persist storage data.
#[test]
fn webstorage_main_module() {
let deno_dir = util::new_deno_dir();

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("webstorage/fixture.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("webstorage/logger.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 0 }\n");

let mut deno_cmd = util::deno_cmd_with_deno_dir(deno_dir.path());
let output = deno_cmd
.current_dir(util::testdata_path())
.arg("run")
.arg("webstorage/fixture.ts")
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap()
.wait_with_output()
.unwrap();
assert!(output.status.success());
assert_eq!(output.stdout, b"Storage { length: 1, hello: \"deno\" }\n");
}

itest!(_075_import_local_query_hash {
args: "run 075_import_local_query_hash.ts",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/testdata/webstorage/config_a.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}
3 changes: 3 additions & 0 deletions cli/tests/testdata/webstorage/config_b.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compilerOptions": {}
}
2 changes: 2 additions & 0 deletions cli/tests/testdata/webstorage/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./logger.ts";
import "./setter.ts";
1 change: 1 addition & 0 deletions cli/tests/testdata/webstorage/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(window.localStorage);
4 changes: 4 additions & 0 deletions cli/tests/testdata/webstorage/serialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
window.sessionStorage.setItem("hello", "deno");

console.log(window.localStorage);
console.log(window.sessionStorage);
3 changes: 3 additions & 0 deletions cli/tests/testdata/webstorage/serialization.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[WILDCARD]
Storage {[WILDCARD]
Storage { length: 1, hello: "deno" }
1 change: 1 addition & 0 deletions cli/tests/testdata/webstorage/setter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.localStorage.setItem("hello", "deno");
3 changes: 2 additions & 1 deletion ext/webstorage/01_webstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
return true;
},
has(target, p) {
return (typeof target.getItem(p)) === "string";
return p === SymbolFor("Deno.customInspect") ||
(typeof target.getItem(p)) === "string";
},
ownKeys() {
return core.opSync("op_webstorage_iterate_keys", persistent);
Expand Down

0 comments on commit 9d87592

Please sign in to comment.