diff options
author | Yusuke Tanaka <yusuktan@maguro.dev> | 2020-11-26 21:16:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-26 13:16:48 +0100 |
commit | 60e980c78180ee3b0a14d692307be275dc181c8d (patch) | |
tree | 03bfb36c6465d0299bc1d5be782e4ac74c0c1456 /cli | |
parent | a1a806ae0cb132ac63cdf83543e45a958010aeb2 (diff) |
refactor(cli): Remove unnecessary conversion into BTreeMap (#8498)
This commit removes unnecessary conversion into BTreeMap. The value
that gets converted into BTreeMap is _originally_ BTreeMap, so this
conversion is just superfluous.
Additionally, a test is added so that we can make sure the keys in the
emitted lockfile are sorted alphabetically.
Diffstat (limited to 'cli')
-rw-r--r-- | cli/lockfile.rs | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/cli/lockfile.rs b/cli/lockfile.rs index ca86e6d38..c817d26de 100644 --- a/cli/lockfile.rs +++ b/cli/lockfile.rs @@ -34,9 +34,7 @@ impl Lockfile { if !self.write { return Ok(()); } - // Will perform sort so output is deterministic - let map: BTreeMap<_, _> = self.map.iter().collect(); - let j = json!(map); + let j = json!(&self.map); let s = serde_json::to_string_pretty(&j).unwrap(); let mut f = std::fs::OpenOptions::new() .write(true) @@ -192,9 +190,30 @@ mod tests { .read_to_string(&mut contents) .expect("Unable to read the file"); - assert!(contents.contains( - "fedebba9bb82cce293196f54b21875b649e457f0eaf55556f1e318204947a28f" - )); // sha-256 hash of the source 'Here is some source code' + let contents_json = + serde_json::from_str::<serde_json::Value>(&contents).unwrap(); + let object = contents_json.as_object().unwrap(); + + assert_eq!( + object + .get("https://deno.land/std@0.71.0/textproto/mod.ts") + .and_then(|v| v.as_str()), + // sha-256 hash of the source 'Here is some source code' + Some("fedebba9bb82cce293196f54b21875b649e457f0eaf55556f1e318204947a28f") + ); + + // confirm that keys are sorted alphabetically + let mut keys = object.keys().map(|k| k.as_str()); + assert_eq!( + keys.next(), + Some("https://deno.land/std@0.71.0/async/delay.ts") + ); + assert_eq!(keys.next(), Some("https://deno.land/std@0.71.0/io/util.ts")); + assert_eq!( + keys.next(), + Some("https://deno.land/std@0.71.0/textproto/mod.ts") + ); + assert!(keys.next().is_none()); teardown(temp_dir); } |