1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
//! Implementation of the analyzer.
use std::ffi::OsStr;
use std::fmt;
use std::future::Future;
use std::mem::ManuallyDrop;
use std::ops::Range;
use std::path::absolute;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::thread::JoinHandle;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use indexmap::IndexSet;
use line_index::LineCol;
use line_index::LineIndex;
use line_index::WideEncoding;
use line_index::WideLineCol;
use path_clean::clean;
use rowan::GreenNode;
use tokio::runtime::Handle;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use url::Url;
use walkdir::WalkDir;
use wdl_ast::AstNode;
use wdl_ast::Diagnostic;
use wdl_ast::SyntaxNode;
use wdl_ast::Validator;
use crate::graph::DocumentGraphNode;
use crate::graph::ParseState;
use crate::queue::AddRequest;
use crate::queue::AnalysisQueue;
use crate::queue::AnalyzeRequest;
use crate::queue::NotifyChangeRequest;
use crate::queue::NotifyIncrementalChangeRequest;
use crate::queue::RemoveRequest;
use crate::queue::Request;
use crate::rayon::RayonHandle;
use crate::DocumentScope;
/// Represents the kind of analysis progress being reported.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProgressKind {
/// The progress is for parsing documents.
Parsing,
/// The progress is for analyzing documents.
Analyzing,
}
impl fmt::Display for ProgressKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parsing => write!(f, "parsing"),
Self::Analyzing => write!(f, "analyzing"),
}
}
}
/// Converts a local path to a file schemed URI.
pub fn path_to_uri(path: &Path) -> Option<Url> {
Url::from_file_path(clean(absolute(path).ok()?)).ok()
}
/// Represents the result of a parse.
#[derive(Debug, Clone)]
pub enum ParseResult {
/// There was an error parsing the document.
Error(Arc<anyhow::Error>),
/// The document was parsed.
Parsed {
/// The monotonic version of the document that was parsed.
///
/// This value comes from incremental changes to the file.
///
/// If `None`, the parsed version had no incremental changes.
version: Option<i32>,
/// The root node of the document.
root: GreenNode,
/// The line index used to map line/column offsets to byte offsets and
/// vice versa.
lines: Arc<LineIndex>,
},
}
impl ParseResult {
/// Gets the version of the parsed document.
///
/// Returns `None` if there was an error parsing the document or the parsed
/// document had no incremental changes.
pub fn version(&self) -> Option<i32> {
match self {
Self::Error(_) => None,
Self::Parsed { version, .. } => *version,
}
}
/// Gets the root from the parse result.
///
/// Returns `None` if there was an error parsing the document.
pub fn root(&self) -> Option<&GreenNode> {
match self {
Self::Error(_) => None,
Self::Parsed { root, .. } => Some(root),
}
}
/// Gets the line index from the parse result.
///
/// Returns `None` if there was an error parsing the document.
pub fn lines(&self) -> Option<&Arc<LineIndex>> {
match self {
Self::Error(_) => None,
Self::Parsed { lines, .. } => Some(lines),
}
}
/// Gets the AST document of the parse result.
///
/// Returns `None` if there was an error parsing the document.
pub fn document(&self) -> Option<wdl_ast::Document> {
match &self {
ParseResult::Error(_) => None,
ParseResult::Parsed { root, .. } => Some(
wdl_ast::Document::cast(SyntaxNode::new_root(root.clone()))
.expect("node should cast"),
),
}
}
/// Gets the error parsing the document.
///
/// Returns` None` if the document was parsed.
pub fn error(&self) -> Option<&Arc<anyhow::Error>> {
match self {
Self::Error(e) => Some(e),
ParseResult::Parsed { .. } => None,
}
}
}
impl From<&ParseState> for ParseResult {
fn from(state: &ParseState) -> Self {
match state {
ParseState::NotParsed => {
panic!("cannot create a result for an file that hasn't been parsed")
}
ParseState::Error(e) => Self::Error(e.clone()),
ParseState::Parsed {
version,
root,
lines,
diagnostics: _,
} => Self::Parsed {
version: *version,
root: root.clone(),
lines: lines.clone(),
},
}
}
}
/// Represents the result of an analysis.
///
/// Analysis results are cheap to clone.
#[derive(Debug, Clone)]
pub struct AnalysisResult {
/// The analysis result id.
///
/// The identifier changes every time the document is analyzed.
id: Arc<String>,
/// The URI of the analyzed document.
uri: Arc<Url>,
/// The result from parsing the file.
parse_result: ParseResult,
/// The diagnostics for the document.
diagnostics: Arc<[Diagnostic]>,
/// The scope of the analyzed document.
scope: Arc<DocumentScope>,
}
impl AnalysisResult {
/// Constructs a new analysis result for the given graph node.
pub(crate) fn new(node: &DocumentGraphNode) -> Self {
let analysis = node.analysis().expect("analysis not completed");
Self {
id: analysis.id().clone(),
uri: node.uri().clone(),
parse_result: node.parse_state().into(),
diagnostics: analysis.diagnostics().clone(),
scope: analysis.scope().clone(),
}
}
/// Gets the identifier of the analysis result.
///
/// This value changes when a document is reanalyzed.
pub fn id(&self) -> &Arc<String> {
&self.id
}
/// Gets the URI of the document that was analyzed.
pub fn uri(&self) -> &Arc<Url> {
&self.uri
}
/// Gets the result of the parse.
pub fn parse_result(&self) -> &ParseResult {
&self.parse_result
}
/// Gets the diagnostics associated with the document.
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
/// Gets the scope of the analyzed document.
pub fn scope(&self) -> &Arc<DocumentScope> {
&self.scope
}
}
/// Represents a position in a document's source.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)]
pub struct SourcePosition {
/// Line position in a document (zero-based).
// NOTE: this field must come before `character` to maintain a correct sort order.
pub line: u32,
/// Character offset on a line in a document (zero-based). The meaning of
/// this offset is determined by the position encoding.
pub character: u32,
}
impl SourcePosition {
/// Constructs a new source position from a line and character offset.
pub fn new(line: u32, character: u32) -> Self {
Self { line, character }
}
}
/// Represents the encoding of a source position.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum SourcePositionEncoding {
/// The position is UTF8 encoded.
///
/// A position's character is the UTF-8 offset from the start of the line.
UTF8,
/// The position is UTF16 encoded.
///
/// A position's character is the UTF-16 offset from the start of the line.
UTF16,
}
/// Represents an edit to a document's source.
#[derive(Debug, Clone)]
pub struct SourceEdit {
/// The range of the edit.
///
/// Note that invalid ranges will cause the edit to be ignored.
range: Range<SourcePosition>,
/// The encoding of the edit positions.
encoding: SourcePositionEncoding,
/// The replacement text.
text: String,
}
impl SourceEdit {
/// Creates a new source edit for the given range and replacement text.
pub fn new(
range: Range<SourcePosition>,
encoding: SourcePositionEncoding,
text: impl Into<String>,
) -> Self {
Self {
range,
encoding,
text: text.into(),
}
}
/// Gets the range of the edit.
pub(crate) fn range(&self) -> Range<SourcePosition> {
self.range.start..self.range.end
}
/// Applies the edit to the given string if it's in range.
pub(crate) fn apply(&self, source: &mut String, lines: &LineIndex) -> Result<()> {
let (start, end) = match self.encoding {
SourcePositionEncoding::UTF8 => (
LineCol {
line: self.range.start.line,
col: self.range.start.character,
},
LineCol {
line: self.range.end.line,
col: self.range.end.character,
},
),
SourcePositionEncoding::UTF16 => (
lines
.to_utf8(
WideEncoding::Utf16,
WideLineCol {
line: self.range.start.line,
col: self.range.start.character,
},
)
.context("invalid edit start position")?,
lines
.to_utf8(
WideEncoding::Utf16,
WideLineCol {
line: self.range.end.line,
col: self.range.end.character,
},
)
.context("invalid edit end position")?,
),
};
let range: Range<usize> = lines
.offset(start)
.context("invalid edit start position")?
.into()
..lines
.offset(end)
.context("invalid edit end position")?
.into();
if !source.is_char_boundary(range.start) {
bail!("edit start position is not at a character boundary");
}
if !source.is_char_boundary(range.end) {
bail!("edit end position is not at a character boundary");
}
source.replace_range(range, &self.text);
Ok(())
}
}
/// Represents an incremental change to a document.
#[derive(Clone, Debug)]
pub struct IncrementalChange {
/// The monotonic version of the document.
///
/// This is expected to increase for each incremental change.
pub version: i32,
/// The source to start from for applying edits.
///
/// If this is `Some`, a full reparse will occur after applying edits to
/// this string.
///
/// If this is `None`, edits will be applied to the existing CST and an
/// attempt will be made to incrementally parse the file.
pub start: Option<String>,
/// The source edits to apply.
pub edits: Vec<SourceEdit>,
}
/// Represents a Workflow Description Language (WDL) document analyzer.
///
/// By default, analysis parses documents, performs validation checks, resolves
/// imports, and performs type checking.
///
/// Each analysis operation is processed in order of request; however, the
/// individual parsing, resolution, and analysis of documents is performed
/// across a thread pool.
///
/// Note that dropping the analyzer is a blocking operation as it will wait for
/// the queue thread to join.
///
/// The type parameter is the context type passed to the progress callback.
#[derive(Debug)]
pub struct Analyzer<Context> {
/// The sender for sending analysis requests to the queue.
sender: ManuallyDrop<mpsc::UnboundedSender<Request<Context>>>,
/// The join handle for the queue task.
handle: Option<JoinHandle<()>>,
}
impl<Context> Analyzer<Context>
where
Context: Send + Clone + 'static,
{
/// Constructs a new analyzer.
///
/// The provided progress callback will be invoked during analysis.
///
/// The analyzer will use a default validator for validation.
///
/// The analyzer must be constructed from the context of a Tokio runtime.
pub fn new<Progress, Return>(progress: Progress) -> Self
where
Progress: Fn(Context, ProgressKind, usize, usize) -> Return + Send + 'static,
Return: Future<Output = ()>,
{
Self::new_with_validator(progress, Validator::default)
}
/// Constructs a new analyzer with the given validator function.
///
/// The provided progress callback will be invoked during analysis.
///
/// This validator function will be called once per worker thread to
/// initialize a thread-local validator.
///
/// The analyzer must be constructed from the context of a Tokio runtime.
pub fn new_with_validator<Progress, Return, Validator>(
progress: Progress,
validator: Validator,
) -> Self
where
Progress: Fn(Context, ProgressKind, usize, usize) -> Return + Send + 'static,
Return: Future<Output = ()>,
Validator: Fn() -> wdl_ast::Validator + Send + Sync + 'static,
{
let (tx, rx) = mpsc::unbounded_channel();
let tokio = Handle::current();
let handle = std::thread::spawn(move || {
let queue = AnalysisQueue::new(tokio, progress, validator);
queue.run(rx);
});
Self {
sender: ManuallyDrop::new(tx),
handle: Some(handle),
}
}
/// Adds documents to the analyzer.
///
/// If a specified path is a directory, it is recursively searched for WDL
/// documents.
///
/// Returns an error if there was a problem discovering documents for the
/// specified paths.
pub async fn add_documents(&self, paths: Vec<PathBuf>) -> Result<()> {
// Start by searching for documents
let documents = RayonHandle::spawn(move || -> Result<IndexSet<Url>> {
let mut documents = IndexSet::new();
for path in paths {
let metadata = path.metadata().with_context(|| {
format!(
"failed to read metadata for `{path}`",
path = path.display()
)
})?;
if metadata.is_file() {
documents.insert(path_to_uri(&path).with_context(|| {
format!(
"failed to convert path `{path}` to a URI",
path = path.display()
)
})?);
continue;
}
for result in WalkDir::new(&path).follow_links(true) {
let entry = result.with_context(|| {
format!("failed to read directory `{path}`", path = path.display())
})?;
if !entry.file_type().is_file()
|| entry.path().extension().and_then(OsStr::to_str) != Some("wdl")
{
continue;
}
documents.insert(path_to_uri(entry.path()).with_context(|| {
format!(
"failed to convert path `{path}` to a URI",
path = entry.path().display()
)
})?);
}
}
Ok(documents)
})
.await?;
if documents.is_empty() {
return Ok(());
}
// Send the add request to the queue
let (tx, rx) = oneshot::channel();
self.sender
.send(Request::Add(AddRequest {
documents,
completed: tx,
}))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})?;
rx.await.map_err(|_| {
anyhow!("failed to receive response from analysis queue because the channel has closed")
})?;
Ok(())
}
/// Removes the specified documents from the analyzer.
///
/// If a specified URI is a prefix (i.e. directory) of documents known to
/// the analyzer, those documents will be removed.
///
/// Documents are only removed when not referenced from importing documents.
pub async fn remove_documents<'a>(&self, documents: Vec<Url>) -> Result<()> {
// Send the remove request to the queue
let (tx, rx) = oneshot::channel();
self.sender
.send(Request::Remove(RemoveRequest {
documents,
completed: tx,
}))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})?;
rx.await.map_err(|_| {
anyhow!("failed to receive response from analysis queue because the channel has closed")
})?;
Ok(())
}
/// Notifies the analyzer that a document has an incremental change.
///
/// Changes to documents that aren't known to the analyzer are ignored.
pub fn notify_incremental_change(
&self,
document: Url,
change: IncrementalChange,
) -> Result<()> {
self.sender
.send(Request::NotifyIncrementalChange(
NotifyIncrementalChangeRequest { document, change },
))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})
}
/// Notifies the analyzer that a document has fully changed and should be
/// fetched again.
///
/// Changes to documents that aren't known to the analyzer are ignored.
///
/// If `discard_pending` is true, then any pending incremental changes are
/// discarded; otherwise, the full change is ignored if there are pending
/// incremental changes.
pub fn notify_change(&self, document: Url, discard_pending: bool) -> Result<()> {
self.sender
.send(Request::NotifyChange(NotifyChangeRequest {
document,
discard_pending,
}))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})
}
/// Analyzes a specific document.
///
/// The provided context is passed to the progress callback.
///
/// If the document is up-to-date and was previously analyzed, the current
/// analysis result is returned.
///
/// Returns an analysis result for each document that was analyzed.
pub async fn analyze_document(
&self,
context: Context,
document: Url,
) -> Result<Vec<AnalysisResult>> {
// Send the analyze request to the queue
let (tx, rx) = oneshot::channel();
self.sender
.send(Request::Analyze(AnalyzeRequest {
document: Some(document),
context,
completed: tx,
}))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})?;
rx.await.map_err(|_| {
anyhow!("failed to receive response from analysis queue because the channel has closed")
})?
}
/// Performs analysis of all documents.
///
/// The provided context is passed to the progress callback.
///
/// If a document is up-to-date and was previously analyzed, the current
/// analysis result is returned.
///
/// Returns an analysis result for each document that was analyzed.
pub async fn analyze(&self, context: Context) -> Result<Vec<AnalysisResult>> {
// Send the analyze request to the queue
let (tx, rx) = oneshot::channel();
self.sender
.send(Request::Analyze(AnalyzeRequest {
document: None,
context,
completed: tx,
}))
.map_err(|_| {
anyhow!("failed to send request to analysis queue because the channel has closed")
})?;
rx.await.map_err(|_| {
anyhow!("failed to receive response from analysis queue because the channel has closed")
})?
}
}
impl<C> Drop for Analyzer<C> {
fn drop(&mut self) {
unsafe { ManuallyDrop::drop(&mut self.sender) };
if let Some(handle) = self.handle.take() {
handle.join().unwrap();
}
}
}
/// Constant that asserts `Analyzer` is `Send + Sync`; if not, it fails to
/// compile.
const _: () = {
/// Helper that will fail to compile if T is not `Send + Sync`.
const fn _assert<T: Send + Sync>() {}
_assert::<Analyzer<()>>();
};
#[cfg(test)]
mod test {
use std::fs;
use tempfile::TempDir;
use wdl_ast::Severity;
use super::*;
#[tokio::test]
async fn it_returns_empty_results() {
let analyzer = Analyzer::new(|_: (), _, _, _| async {});
let results = analyzer.analyze(()).await.unwrap();
assert!(results.is_empty());
}
#[tokio::test]
async fn it_analyzes_a_document() {
let dir = TempDir::new().expect("failed to create temporary directory");
let path = dir.path().join("foo.wdl");
fs::write(
&path,
r#"version 1.1
task test {
command <<<>>>
}
workflow test {
}
"#,
)
.expect("failed to create test file");
// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(|_: (), _, _, _| async {});
analyzer
.add_documents(vec![path])
.await
.expect("should add document");
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].diagnostics().len(), 1);
assert_eq!(results[0].diagnostics()[0].rule(), None);
assert_eq!(results[0].diagnostics()[0].severity(), Severity::Error);
assert_eq!(
results[0].diagnostics()[0].message(),
"conflicting workflow name `test`"
);
// Analyze again and ensure the analysis result id is unchanged
let id = results[0].id().clone();
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].id().as_ref(), id.as_ref());
assert_eq!(results[0].diagnostics().len(), 1);
assert_eq!(results[0].diagnostics()[0].rule(), None);
assert_eq!(results[0].diagnostics()[0].severity(), Severity::Error);
assert_eq!(
results[0].diagnostics()[0].message(),
"conflicting workflow name `test`"
);
}
#[tokio::test]
async fn it_reanalyzes_a_document_on_change() {
let dir = TempDir::new().expect("failed to create temporary directory");
let path = dir.path().join("foo.wdl");
fs::write(
&path,
r#"version 1.1
task test {
command <<<>>>
}
workflow test {
}
"#,
)
.expect("failed to create test file");
// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(|_: (), _, _, _| async {});
analyzer
.add_documents(vec![path.clone()])
.await
.expect("should add document");
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].diagnostics().len(), 1);
assert_eq!(results[0].diagnostics()[0].rule(), None);
assert_eq!(results[0].diagnostics()[0].severity(), Severity::Error);
assert_eq!(
results[0].diagnostics()[0].message(),
"conflicting workflow name `test`"
);
// Rewrite the file to correct the issue
fs::write(
&path,
r#"version 1.1
task test {
command <<<>>>
}
workflow something_else {
}
"#,
)
.expect("failed to create test file");
let uri = path_to_uri(&path).expect("should convert to URI");
analyzer.notify_change(uri.clone(), false).unwrap();
// Analyze again and ensure the analysis result id is changed and the issue
// fixed
let id = results[0].id().clone();
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 1);
assert!(results[0].id().as_ref() != id.as_ref());
assert_eq!(results[0].diagnostics().len(), 0);
// Analyze again and ensure the analysis result id is unchanged
let id = results[0].id().clone();
let results = analyzer.analyze_document((), uri).await.unwrap();
assert_eq!(results.len(), 1);
assert!(results[0].id().as_ref() == id.as_ref());
assert_eq!(results[0].diagnostics().len(), 0);
}
#[tokio::test]
async fn it_reanalyzes_a_document_on_incremental_change() {
let dir = TempDir::new().expect("failed to create temporary directory");
let path = dir.path().join("foo.wdl");
fs::write(
&path,
r#"version 1.1
task test {
command <<<>>>
}
workflow test {
}
"#,
)
.expect("failed to create test file");
// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(|_: (), _, _, _| async {});
analyzer
.add_documents(vec![path.clone()])
.await
.expect("should add document");
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].diagnostics().len(), 1);
assert_eq!(results[0].diagnostics()[0].rule(), None);
assert_eq!(results[0].diagnostics()[0].severity(), Severity::Error);
assert_eq!(
results[0].diagnostics()[0].message(),
"conflicting workflow name `test`"
);
// Edit the file to correct the issue
let uri = path_to_uri(&path).expect("should convert to URI");
analyzer
.notify_incremental_change(
uri.clone(),
IncrementalChange {
version: 2,
start: None,
edits: vec![SourceEdit {
range: SourcePosition::new(6, 9)..SourcePosition::new(6, 13),
encoding: SourcePositionEncoding::UTF8,
text: "something_else".to_string(),
}],
},
)
.unwrap();
// Analyze again and ensure the analysis result id is changed and the issue was
// fixed
let id = results[0].id().clone();
let results = analyzer.analyze_document((), uri).await.unwrap();
assert_eq!(results.len(), 1);
assert!(results[0].id().as_ref() != id.as_ref());
assert_eq!(results[0].diagnostics().len(), 0);
}
#[tokio::test]
async fn it_removes_documents() {
let dir = TempDir::new().expect("failed to create temporary directory");
let foo = dir.path().join("foo.wdl");
fs::write(
&foo,
r#"version 1.1
workflow test {
}
"#,
)
.expect("failed to create test file");
let bar = dir.path().join("bar.wdl");
fs::write(
&bar,
r#"version 1.1
workflow test {
}
"#,
)
.expect("failed to create test file");
let baz = dir.path().join("baz.wdl");
fs::write(
&baz,
r#"version 1.1
workflow test {
}
"#,
)
.expect("failed to create test file");
// Add all three documents to the analyzer
let analyzer = Analyzer::new(|_: (), _, _, _| async {});
analyzer
.add_documents(vec![dir.path().to_path_buf()])
.await
.expect("should add documents");
// Analyze the documents
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 3);
assert!(results[0].diagnostics().is_empty());
assert!(results[1].diagnostics().is_empty());
assert!(results[2].diagnostics().is_empty());
// Analyze the documents again
let results = analyzer.analyze(()).await.unwrap();
assert_eq!(results.len(), 3);
// Remove the documents by directory
analyzer
.remove_documents(vec![
path_to_uri(dir.path()).expect("should convert to URI"),
])
.await
.unwrap();
let results = analyzer.analyze(()).await.unwrap();
assert!(results.is_empty());
}
}