summaryrefslogtreecommitdiff
path: root/cli/flags.rs
diff options
context:
space:
mode:
authorMatt Harrison <hi@matt-harrison.com>2019-06-11 15:34:39 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-06-11 10:34:39 -0400
commitd82c1991cf0919c312b87501bc588cf17781b32f (patch)
tree1cca87de6af963c379187726468eef8f47741d70 /cli/flags.rs
parentcb581620522febe618cbf084b0dc3428479e84a9 (diff)
Add --seed for setting RNG seed (#2483)
Diffstat (limited to 'cli/flags.rs')
-rw-r--r--cli/flags.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/cli/flags.rs b/cli/flags.rs
index 9f37fb184..fa86df594 100644
--- a/cli/flags.rs
+++ b/cli/flags.rs
@@ -29,6 +29,7 @@ pub struct DenoFlags {
pub allow_hrtime: bool,
pub no_prompts: bool,
pub no_fetch: bool,
+ pub seed: Option<u64>,
pub v8_flags: Option<Vec<String>>,
pub xeval_replvar: Option<String>,
pub xeval_delim: Option<String>,
@@ -146,6 +147,19 @@ To get help on the another subcommands (run in this case):
.takes_value(true)
.global(true),
).arg(
+ Arg::with_name("seed")
+ .long("seed")
+ .value_name("NUMBER")
+ .help("Seed Math.random()")
+ .takes_value(true)
+ .validator(|val: String| {
+ match val.parse::<u64>() {
+ Ok(_) => Ok(()),
+ Err(_) => Err("Seed should be a number".to_string())
+ }
+ })
+ .global(true),
+ ).arg(
Arg::with_name("v8-options")
.long("v8-options")
.help("Print V8 command line options")
@@ -379,6 +393,22 @@ pub fn parse_flags(matches: &ArgMatches) -> DenoFlags {
v8_flags.insert(0, "deno".to_string());
flags.v8_flags = Some(v8_flags);
}
+ if matches.is_present("seed") {
+ let seed_string = matches.value_of("seed").unwrap();
+ let seed = seed_string.parse::<u64>().unwrap();
+ flags.seed = Some(seed);
+
+ let v8_seed_flag = format!("--random-seed={}", seed);
+
+ match flags.v8_flags {
+ Some(ref mut v8_flags) => {
+ v8_flags.push(v8_seed_flag);
+ }
+ None => {
+ flags.v8_flags = Some(svec!["deno", v8_seed_flag]);
+ }
+ }
+ }
flags = parse_run_args(flags, matches);
// flags specific to "run" subcommand
@@ -1112,4 +1142,42 @@ mod tests {
assert_eq!(subcommand, DenoSubcommand::Run);
assert_eq!(argv, svec!["deno", "script.ts"]);
}
+
+ #[test]
+ fn test_flags_from_vec_28() {
+ let (flags, subcommand, argv) =
+ flags_from_vec(svec!["deno", "--seed", "250", "run", "script.ts"]);
+ assert_eq!(
+ flags,
+ DenoFlags {
+ seed: Some(250 as u64),
+ v8_flags: Some(svec!["deno", "--random-seed=250"]),
+ ..DenoFlags::default()
+ }
+ );
+ assert_eq!(subcommand, DenoSubcommand::Run);
+ assert_eq!(argv, svec!["deno", "script.ts"])
+ }
+
+ #[test]
+ fn test_flags_from_vec_29() {
+ let (flags, subcommand, argv) = flags_from_vec(svec![
+ "deno",
+ "--seed",
+ "250",
+ "--v8-flags=--expose-gc",
+ "run",
+ "script.ts"
+ ]);
+ assert_eq!(
+ flags,
+ DenoFlags {
+ seed: Some(250 as u64),
+ v8_flags: Some(svec!["deno", "--expose-gc", "--random-seed=250"]),
+ ..DenoFlags::default()
+ }
+ );
+ assert_eq!(subcommand, DenoSubcommand::Run);
+ assert_eq!(argv, svec!["deno", "script.ts"])
+ }
}