summaryrefslogtreecommitdiff
path: root/cli/cache/emit.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/cache/emit.rs')
-rw-r--r--cli/cache/emit.rs27
1 files changed, 9 insertions, 18 deletions
diff --git a/cli/cache/emit.rs b/cli/cache/emit.rs
index 6807f06c1..3c9eecfcb 100644
--- a/cli/cache/emit.rs
+++ b/cli/cache/emit.rs
@@ -10,6 +10,7 @@ use deno_core::unsync::sync::AtomicFlag;
use super::DiskCache;
/// The cache that stores previously emitted files.
+#[derive(Debug)]
pub struct EmitCache {
disk_cache: DiskCache,
emit_failed_flag: AtomicFlag,
@@ -39,7 +40,7 @@ impl EmitCache {
&self,
specifier: &ModuleSpecifier,
expected_source_hash: u64,
- ) -> Option<Vec<u8>> {
+ ) -> Option<String> {
let emit_filename = self.get_emit_filename(specifier)?;
let bytes = self.disk_cache.get(&emit_filename).ok()?;
self
@@ -91,6 +92,7 @@ impl EmitCache {
const LAST_LINE_PREFIX: &str = "\n// denoCacheMetadata=";
+#[derive(Debug)]
struct EmitFileSerializer {
cli_version: &'static str,
}
@@ -100,7 +102,7 @@ impl EmitFileSerializer {
&self,
mut bytes: Vec<u8>,
expected_source_hash: u64,
- ) -> Option<Vec<u8>> {
+ ) -> Option<String> {
let last_newline_index = bytes.iter().rposition(|&b| b == b'\n')?;
let (content, last_line) = bytes.split_at(last_newline_index);
let hashes = last_line.strip_prefix(LAST_LINE_PREFIX.as_bytes())?;
@@ -120,7 +122,7 @@ impl EmitFileSerializer {
// everything looks good, truncate and return it
bytes.truncate(content.len());
- Some(bytes)
+ String::from_utf8(bytes).ok()
}
pub fn serialize(&self, code: &[u8], source_hash: u64) -> Vec<u8> {
@@ -170,8 +172,6 @@ mod test {
},
emit_failed_flag: Default::default(),
};
- let to_string =
- |bytes: Vec<u8>| -> String { String::from_utf8(bytes).unwrap() };
let specifier1 =
ModuleSpecifier::from_file_path(temp_dir.path().join("file1.ts"))
@@ -188,13 +188,10 @@ mod test {
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
// providing the correct source hash
assert_eq!(
- cache.get_emit_code(&specifier1, 10).map(to_string),
+ cache.get_emit_code(&specifier1, 10),
Some(emit_code1.clone()),
);
- assert_eq!(
- cache.get_emit_code(&specifier2, 2).map(to_string),
- Some(emit_code2)
- );
+ assert_eq!(cache.get_emit_code(&specifier2, 2), Some(emit_code2));
// try changing the cli version (should not load previous ones)
let cache = EmitCache {
@@ -215,18 +212,12 @@ mod test {
},
emit_failed_flag: Default::default(),
};
- assert_eq!(
- cache.get_emit_code(&specifier1, 5).map(to_string),
- Some(emit_code1)
- );
+ assert_eq!(cache.get_emit_code(&specifier1, 5), Some(emit_code1));
// adding when already exists should not cause issue
let emit_code3 = "asdf".to_string();
cache.set_emit_code(&specifier1, 20, emit_code3.as_bytes());
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
- assert_eq!(
- cache.get_emit_code(&specifier1, 20).map(to_string),
- Some(emit_code3)
- );
+ assert_eq!(cache.get_emit_code(&specifier1, 20), Some(emit_code3));
}
}