mirror of
https://github.com/denoland/deno.git
synced 2025-01-09 07:39:15 -05:00
refactor(cli/tools/coverage): strongly type inspector values (#11326)
This commit is contained in:
parent
5fa58c9216
commit
c76c09e335
1 changed files with 111 additions and 59 deletions
|
@ -11,7 +11,6 @@ use crate::program_state::ProgramState;
|
||||||
use crate::source_maps::SourceMapGetter;
|
use crate::source_maps::SourceMapGetter;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
use deno_core::serde_json::json;
|
|
||||||
use deno_core::url::Url;
|
use deno_core::url::Url;
|
||||||
use deno_core::LocalInspectorSession;
|
use deno_core::LocalInspectorSession;
|
||||||
use deno_runtime::permissions::Permissions;
|
use deno_runtime::permissions::Permissions;
|
||||||
|
@ -27,58 +26,6 @@ use std::path::PathBuf;
|
||||||
use swc_common::Span;
|
use swc_common::Span;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub struct CoverageCollector {
|
|
||||||
pub dir: PathBuf,
|
|
||||||
session: LocalInspectorSession,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CoverageCollector {
|
|
||||||
pub fn new(dir: PathBuf, session: LocalInspectorSession) -> Self {
|
|
||||||
Self { dir, session }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn start_collecting(&mut self) -> Result<(), AnyError> {
|
|
||||||
self.session.post_message("Debugger.enable", None).await?;
|
|
||||||
self.session.post_message("Profiler.enable", None).await?;
|
|
||||||
self
|
|
||||||
.session
|
|
||||||
.post_message(
|
|
||||||
"Profiler.startPreciseCoverage",
|
|
||||||
Some(json!({"callCount": true, "detailed": true})),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn stop_collecting(&mut self) -> Result<(), AnyError> {
|
|
||||||
let result = self
|
|
||||||
.session
|
|
||||||
.post_message("Profiler.takePreciseCoverage", None)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let take_coverage_result: TakePreciseCoverageResult =
|
|
||||||
serde_json::from_value(result)?;
|
|
||||||
|
|
||||||
fs::create_dir_all(&self.dir)?;
|
|
||||||
|
|
||||||
let script_coverages = take_coverage_result.result;
|
|
||||||
for script_coverage in script_coverages {
|
|
||||||
let filename = format!("{}.json", Uuid::new_v4());
|
|
||||||
let filepath = self.dir.join(filename);
|
|
||||||
|
|
||||||
let mut out = BufWriter::new(File::create(filepath)?);
|
|
||||||
serde_json::to_writer_pretty(&mut out, &script_coverage)?;
|
|
||||||
out.write_all(b"\n")?;
|
|
||||||
out.flush()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.session.post_message("Profiler.disable", None).await?;
|
|
||||||
self.session.post_message("Debugger.disable", None).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(caspervonb) all of these structs can and should be made private, possibly moved to
|
// TODO(caspervonb) all of these structs can and should be made private, possibly moved to
|
||||||
// inspector::protocol.
|
// inspector::protocol.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
@ -107,15 +54,120 @@ pub struct ScriptCoverage {
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct TakePreciseCoverageResult {
|
pub struct StartPreciseCoverageParameters {
|
||||||
result: Vec<ScriptCoverage>,
|
pub call_count: bool,
|
||||||
|
pub detailed: bool,
|
||||||
|
pub allow_triggered_updates: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct GetScriptSourceResult {
|
pub struct StartPreciseCoverageReturnObject {
|
||||||
pub script_source: String,
|
pub timestamp: f64,
|
||||||
pub bytecode: Option<String>,
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct TakePreciseCoverageReturnObject {
|
||||||
|
pub result: Vec<ScriptCoverage>,
|
||||||
|
pub timestamp: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CoverageCollector {
|
||||||
|
pub dir: PathBuf,
|
||||||
|
session: LocalInspectorSession,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CoverageCollector {
|
||||||
|
pub fn new(dir: PathBuf, session: LocalInspectorSession) -> Self {
|
||||||
|
Self { dir, session }
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn enable_debugger(&mut self) -> Result<(), AnyError> {
|
||||||
|
self.session.post_message("Debugger.enable", None).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn enable_profiler(&mut self) -> Result<(), AnyError> {
|
||||||
|
self.session.post_message("Profiler.enable", None).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn disable_debugger(&mut self) -> Result<(), AnyError> {
|
||||||
|
self.session.post_message("Debugger.disable", None).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn disable_profiler(&mut self) -> Result<(), AnyError> {
|
||||||
|
self.session.post_message("Profiler.disable", None).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start_precise_coverage(
|
||||||
|
&mut self,
|
||||||
|
parameters: StartPreciseCoverageParameters,
|
||||||
|
) -> Result<StartPreciseCoverageReturnObject, AnyError> {
|
||||||
|
let parameters_value = serde_json::to_value(parameters)?;
|
||||||
|
let return_value = self
|
||||||
|
.session
|
||||||
|
.post_message("Profiler.startPreciseCoverage", Some(parameters_value))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let return_object = serde_json::from_value(return_value)?;
|
||||||
|
|
||||||
|
Ok(return_object)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn take_precise_coverage(
|
||||||
|
&mut self,
|
||||||
|
) -> Result<TakePreciseCoverageReturnObject, AnyError> {
|
||||||
|
let return_value = self
|
||||||
|
.session
|
||||||
|
.post_message("Profiler.takePreciseCoverage", None)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let return_object = serde_json::from_value(return_value)?;
|
||||||
|
|
||||||
|
Ok(return_object)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn start_collecting(&mut self) -> Result<(), AnyError> {
|
||||||
|
self.enable_debugger().await?;
|
||||||
|
self.enable_profiler().await?;
|
||||||
|
self
|
||||||
|
.start_precise_coverage(StartPreciseCoverageParameters {
|
||||||
|
call_count: true,
|
||||||
|
detailed: true,
|
||||||
|
allow_triggered_updates: false,
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stop_collecting(&mut self) -> Result<(), AnyError> {
|
||||||
|
fs::create_dir_all(&self.dir)?;
|
||||||
|
|
||||||
|
let script_coverages = self.take_precise_coverage().await?.result;
|
||||||
|
for script_coverage in script_coverages {
|
||||||
|
let filename = format!("{}.json", Uuid::new_v4());
|
||||||
|
let filepath = self.dir.join(filename);
|
||||||
|
|
||||||
|
let mut out = BufWriter::new(File::create(filepath)?);
|
||||||
|
serde_json::to_writer_pretty(&mut out, &script_coverage)?;
|
||||||
|
out.write_all(b"\n")?;
|
||||||
|
out.flush()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.disable_debugger().await?;
|
||||||
|
self.disable_profiler().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum CoverageReporterKind {
|
pub enum CoverageReporterKind {
|
||||||
|
|
Loading…
Reference in a new issue