-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlib.rs
More file actions
867 lines (778 loc) · 25.9 KB
/
Copy pathlib.rs
File metadata and controls
867 lines (778 loc) · 25.9 KB
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
//! This crate provides a [`Watch`](crate::Watch) that launch a given command,
//! re-launching the command when changes are detected in your source code.
//!
//! This [`Watch`](crate::Watch) struct is intended to be used with the
//! [xtask concept](https://github.com/matklad/cargo-xtask/) and implements
//! [`clap::Parser`](https://docs.rs/clap/latest/clap/trait.Parser.html) so it
//! can easily be used in your xtask crate. See [clap's `flatten`](https://github.com/clap-rs/clap/blob/master/examples/derive_ref/flatten_hand_args.rs)
//! to see how to extend it.
//!
//! # Setup
//!
//! The best way to add xtask-watch to your project is to create a workspace
//! with two packages: your project's package and the xtask package.
//!
//! ## Create a project using xtask
//!
//! * Create a new directory that will contains the two package of your project
//! and the workspace's `Cargo.toml`
//!
//! ```console
//! mkdir my-project
//! cd my-project
//! touch Cargo.toml
//! ```
//!
//! * Create the project package and the xtask package using `cargo new`:
//!
//! ```console
//! cargo new my-project
//! cargo new xtask
//! ```
//!
//! * Open the workspace's Cargo.toml and add the following:
//!
//! ```toml
//! [workspace]
//! members = [
//! "my-project",
//! "xtask",
//! ]
//! ```
//!
//!
//! * Create a `.cargo/config.toml` file and add the following content:
//!
//! ```toml
//! [alias]
//! xtask = "run --package xtask --"
//! ```
//!
//! The directory layout should look like this:
//!
//! ```console
//! my-project
//! ├── .cargo
//! │ └── config.toml
//! ├── Cargo.toml
//! ├── my-project
//! │ ├── Cargo.toml
//! │ └── src
//! │ └── ...
//! └── xtask
//! ├── Cargo.toml
//! └── src
//! └── main.rs
//! ```
//!
//! And now you can run your xtask package using:
//!
//! ```console
//! cargo xtask
//! ```
//! You can find more informations about xtask
//! [here](https://github.com/matklad/cargo-xtask/).
//!
//! ## Use xtask-watch as a dependency
//!
//! Finally, add the following to the xtask package's Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! xtask-watch = "0.1.0"
//! ```
//!
//! # Examples
//!
//! ## A basic implementation
//!
//! ```rust,no_run
//! use std::process::Command;
//! use xtask_watch::{
//! anyhow::Result,
//! clap,
//! };
//!
//! #[derive(clap::Parser)]
//! enum Opt {
//! Watch(xtask_watch::Watch),
//! }
//!
//! fn main() -> Result<()> {
//! let opt: Opt = clap::Parser::parse();
//!
//! let mut run_command = Command::new("cargo");
//! run_command.arg("check");
//!
//! match opt {
//! Opt::Watch(watch) => {
//! log::info!("Starting to watch `cargo check`");
//! watch.run(run_command)?;
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## A more complex demonstration
//!
//! [`examples/demo`](https://github.com/rustminded/xtask-watch/tree/main/examples/demo)
//! provides an implementation of xtask-watch that naively parse a command given
//! by the user (or use `cargo check` by default) and watch the workspace after
//! launching this command.
//!
//! # Troubleshooting
//!
//! When using the re-export of [`clap`](https://docs.rs/clap/latest/clap), you
//! might encounter this error:
//!
//! ```console
//! error[E0433]: failed to resolve: use of undeclared crate or module `clap`
//! --> xtask/src/main.rs:4:10
//! |
//! 4 | #[derive(Parser)]
//! | ^^^^^^ use of undeclared crate or module `clap`
//! |
//! = note: this error originates in the derive macro `Parser` (in Nightly builds, run with -Z macro-backtrace for more info)
//! ```
//!
//! This occurs because you need to import clap in the scope too. This error can
//! be resolved like this:
//!
//! ```rust
//! use xtask_watch::clap;
//!
//! #[derive(clap::Parser)]
//! struct MyStruct {}
//! ```
//!
//! Or like this:
//!
//! ```rust
//! use xtask_watch::{clap, clap::Parser};
//!
//! #[derive(Parser)]
//! struct MyStruct {}
//! ```
#![deny(missing_docs)]
use anyhow::{Context, Result};
use clap::Parser;
use glob::Pattern;
use lazy_static::lazy_static;
use notify::{Event, EventHandler, RecursiveMode, Watcher};
use std::{
env, io,
path::{Path, PathBuf},
process::{Child, Command, ExitStatus},
sync::{Arc, Mutex, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, mpsc},
thread,
time::{Duration, Instant},
};
pub use anyhow;
pub use cargo_metadata;
pub use cargo_metadata::camino;
pub use clap;
/// Fetch the metadata of the crate.
pub fn metadata() -> &'static cargo_metadata::Metadata {
lazy_static! {
static ref METADATA: cargo_metadata::Metadata = cargo_metadata::MetadataCommand::new()
.exec()
.expect("cannot get crate's metadata");
}
&METADATA
}
/// Fetch information of a package in the current crate.
pub fn package(name: &str) -> Option<&cargo_metadata::Package> {
metadata().packages.iter().find(|x| x.name == name)
}
/// Return a [`std::process::Command`] of the xtask command currently running.
pub fn xtask_command() -> Command {
Command::new(env::args_os().next().unwrap())
}
/// Watches over your project's source code, relaunching a given command when
/// changes are detected.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Parser)]
#[clap(about = "Watches over your project's source code.")]
pub struct Watch {
/// Shell command(s) to execute on changes.
#[clap(long = "shell", short = 's')]
pub shell_commands: Vec<String>,
/// Cargo command(s) to execute on changes.
///
/// The default is `[ check ]`
#[clap(long = "exec", short = 'x')]
pub cargo_commands: Vec<String>,
/// Watch specific file(s) or folder(s).
///
/// The default is the workspace root.
#[clap(long = "watch", short = 'w')]
pub watch_paths: Vec<PathBuf>,
/// Paths or glob patterns that will be excluded.
///
/// Relative values are resolved from the current working directory.
#[clap(long = "ignore", short = 'i')]
pub exclude_paths: Vec<PathBuf>,
/// Paths or glob patterns, relative to the workspace root, that will be excluded.
#[clap(skip)]
pub workspace_exclude_paths: Vec<PathBuf>,
/// Throttle events to prevent the command to be re-executed too early
/// right after an execution already occurred.
///
/// The default is 2 seconds.
#[clap(skip = Duration::from_secs(2))]
pub debounce: Duration,
#[clap(skip)]
exclude_globs: Vec<Pattern>,
#[clap(skip)]
workspace_exclude_globs: Vec<Pattern>,
#[clap(skip)]
watch_lock: Option<WatchLock>,
}
impl Watch {
/// Add a path to watch for changes.
pub fn watch_path(mut self, path: impl AsRef<Path>) -> Self {
self.watch_paths.push(path.as_ref().to_path_buf());
self
}
/// Add multiple paths to watch for changes.
pub fn watch_paths(mut self, paths: impl IntoIterator<Item = impl AsRef<Path>>) -> Self {
for path in paths {
self.watch_paths.push(path.as_ref().to_path_buf())
}
self
}
/// Add a path that will be ignored if changes are detected.
pub fn exclude_path(mut self, path: impl AsRef<Path>) -> Self {
self.exclude_paths.push(path.as_ref().to_path_buf());
self
}
/// Add multiple paths that will be ignored if changes are detected.
pub fn exclude_paths(mut self, paths: impl IntoIterator<Item = impl AsRef<Path>>) -> Self {
for path in paths {
self.exclude_paths.push(path.as_ref().to_path_buf());
}
self
}
/// Add a path, relative to the workspace, that will be ignored if changes
/// are detected.
pub fn exclude_workspace_path(mut self, path: impl AsRef<Path>) -> Self {
self.workspace_exclude_paths
.push(path.as_ref().to_path_buf());
self
}
/// Add multiple paths, relative to the workspace, that will be ignored if
/// changes are detected.
pub fn exclude_workspace_paths(
mut self,
paths: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Self {
for path in paths {
self.workspace_exclude_paths
.push(path.as_ref().to_path_buf());
}
self
}
/// Return the shared lock for this watcher, creating it if it does not exist yet.
///
/// Clone and share this lock with external code (e.g. HTTP handlers) to coordinate with
/// watch-driven command execution.
pub fn lock(&mut self) -> WatchLock {
self.watch_lock.get_or_insert_with(WatchLock::new).clone()
}
/// Set the debounce duration after relaunching the command.
pub fn debounce(mut self, duration: Duration) -> Self {
self.debounce = duration;
self
}
/// Run the given `command`, monitor the watched paths and relaunch the
/// command when changes are detected.
///
/// Workspace's `target` directory and hidden paths are excluded by default.
pub fn run(mut self, commands: impl Into<CommandList>) -> Result<()> {
let metadata = metadata();
let list = commands.into();
{
let mut commands = list.commands.lock().expect("not poisoned");
commands.extend(self.shell_commands.iter().map(|x| {
let mut command =
Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()));
command.arg("-c");
command.arg(x);
command
}));
commands.extend(self.cargo_commands.iter().map(|x| {
let mut command =
Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()));
command.arg("-c");
command.arg(format!("cargo {x}"));
command
}));
}
self.prepare_excludes()?;
if self.watch_paths.is_empty() {
self.watch_paths
.push(metadata.workspace_root.clone().into_std_path_buf());
}
self.watch_paths = self
.watch_paths
.into_iter()
.map(|x| {
x.canonicalize()
.with_context(|| format!("can't find {}", x.display()))
})
.collect::<Result<Vec<_>, _>>()?;
let (tx, rx) = mpsc::channel();
let handler = WatchEventHandler {
watch: self.clone(),
tx,
command_start: Instant::now(),
};
let mut watcher =
notify::recommended_watcher(handler).context("could not initialize watcher")?;
for path in &self.watch_paths {
match watcher.watch(path, RecursiveMode::Recursive) {
Ok(()) => log::trace!("Watching {}", path.display()),
Err(err) => log::error!("cannot watch {}: {err}", path.display()),
}
}
let mut current_child = SharedChild::new();
loop {
{
log::info!("Re-running command");
let mut current_child = current_child.clone();
let mut list = list.clone();
let lock = self.watch_lock.clone();
thread::spawn(move || {
let mut status = ExitStatus::default();
let mut run_batch = || {
list.spawn(|res| match res {
Err(err) => {
log::error!("Could not execute command: {err}");
false
}
Ok(child) => {
log::trace!("new child: {}", child.id());
current_child.replace(child);
status = current_child.wait();
status.success()
}
});
};
if let Some(lock) = lock {
match lock.write() {
Ok(_guard) => run_batch(),
Err(err) => {
log::error!("could not acquire write lock: {err}");
return;
}
}
} else {
run_batch();
}
if status.success() {
log::info!("Command succeeded.");
} else if let Some(code) = status.code() {
log::error!("Command failed (exit code: {code})");
} else {
log::error!("Command failed.");
}
});
}
let res = rx.recv();
if res.is_ok() {
log::trace!("Changes detected, re-generating");
}
current_child.terminate();
if res.is_err() {
break;
}
}
Ok(())
}
fn is_excluded_path(&self, path: &Path) -> bool {
if self.exclude_paths.iter().any(|x| path.starts_with(x)) {
return true;
}
if self.exclude_globs.iter().any(|p| p.matches_path(path)) {
return true;
}
if let Ok(stripped_path) = path.strip_prefix(metadata().workspace_root.as_std_path()) {
if self
.workspace_exclude_paths
.iter()
.any(|x| stripped_path.starts_with(x))
{
return true;
}
if self
.workspace_exclude_globs
.iter()
.any(|p| p.matches_path(stripped_path))
{
return true;
}
}
false
}
fn is_hidden_path(&self, path: &Path) -> bool {
self.watch_paths.iter().any(|x| {
path.strip_prefix(x)
.iter()
.any(|x| x.to_string_lossy().starts_with('.'))
})
}
fn is_backup_file(&self, path: &Path) -> bool {
self.watch_paths.iter().any(|x| {
path.strip_prefix(x)
.iter()
.any(|x| x.to_string_lossy().ends_with('~'))
})
}
fn is_glob_pattern(path: &Path) -> bool {
let s = path.as_os_str().to_string_lossy();
s.contains('*') || s.contains('?') || (!cfg!(windows) && s.contains('['))
}
fn compile_glob(path: &Path) -> Result<Pattern> {
let pattern = path
.to_str()
.with_context(|| format!("glob pattern must be valid UTF-8: {}", path.display()))?;
Pattern::new(pattern).with_context(|| format!("invalid glob pattern: `{}`", path.display()))
}
fn prepare_excludes(&mut self) -> Result<()> {
let metadata = metadata();
self.exclude_paths
.push(metadata.target_directory.clone().into_std_path_buf());
let current_dir = env::current_dir().context("failed to get current directory")?;
let mut exclude_paths = Vec::new();
for path in self.exclude_paths.iter() {
if Self::is_glob_pattern(path) {
let absolute = if path.is_absolute() {
path.to_path_buf()
} else {
current_dir.join(path)
};
self.exclude_globs.push(Self::compile_glob(&absolute)?);
} else {
let canonical = path
.canonicalize()
.with_context(|| format!("can't find `{}`", path.display()))?;
exclude_paths.push(canonical);
}
}
self.exclude_paths = exclude_paths;
let workspace_root = metadata.workspace_root.as_std_path();
let mut workspace_exclude_paths = Vec::new();
for path in self.workspace_exclude_paths.iter() {
let path = if path.is_absolute() {
path.strip_prefix(workspace_root)
.with_context(|| {
format!(
"workspace exclude path must be inside workspace root: `{}`",
path.display()
)
})?
.to_path_buf()
} else {
path.to_path_buf()
};
if Self::is_glob_pattern(&path) {
self.workspace_exclude_globs
.push(Self::compile_glob(&path)?);
} else {
workspace_exclude_paths.push(path);
}
}
self.workspace_exclude_paths = workspace_exclude_paths;
Ok(())
}
}
struct WatchEventHandler {
watch: Watch,
tx: mpsc::Sender<()>,
command_start: Instant,
}
impl EventHandler for WatchEventHandler {
fn handle_event(&mut self, event: Result<Event, notify::Error>) {
match event {
Ok(event) => {
if (event.kind.is_modify() || event.kind.is_create() || event.kind.is_create())
&& event.paths.iter().any(|x| {
!self.watch.is_excluded_path(x)
&& x.exists()
&& !self.watch.is_hidden_path(x)
&& !self.watch.is_backup_file(x)
&& self.command_start.elapsed() >= self.watch.debounce
})
{
log::trace!("Changes detected in {event:?}");
self.command_start = Instant::now();
self.tx.send(()).expect("can send");
} else {
log::trace!("Ignoring changes in {event:?}");
}
}
Err(err) => log::error!("watch error: {err}"),
}
}
}
#[derive(Debug, Clone)]
struct SharedChild {
child: Arc<Mutex<Option<Child>>>,
}
impl SharedChild {
fn new() -> Self {
Self {
child: Default::default(),
}
}
fn replace(&mut self, child: impl Into<Option<Child>>) {
*self.child.lock().expect("not poisoned") = child.into();
}
fn wait(&mut self) -> ExitStatus {
loop {
let mut child = self.child.lock().expect("not poisoned");
match child.as_mut().map(|child| child.try_wait()) {
Some(Ok(Some(status))) => {
break status;
}
Some(Ok(None)) => {
drop(child);
thread::sleep(Duration::from_millis(10));
}
Some(Err(err)) => {
log::error!("could not wait for child process: {err}");
break Default::default();
}
None => {
break Default::default();
}
}
}
}
fn terminate(&mut self) {
if let Some(child) = self.child.lock().expect("not poisoned").as_mut() {
#[cfg(unix)]
{
let killing_start = Instant::now();
unsafe {
log::trace!("sending SIGTERM to {}", child.id());
libc::kill(child.id() as _, libc::SIGTERM);
}
while killing_start.elapsed().as_secs() < 2 {
std::thread::sleep(Duration::from_millis(200));
if let Ok(Some(_)) = child.try_wait() {
break;
}
}
}
match child.try_wait() {
Ok(Some(_)) => {}
_ => {
log::trace!("killing {}", child.id());
let _ = child.kill();
let _ = child.wait();
}
}
} else {
log::trace!("nothing to terminate");
}
}
}
/// A list of commands to run.
#[derive(Debug, Clone)]
pub struct CommandList {
commands: Arc<Mutex<Vec<Command>>>,
}
impl From<Command> for CommandList {
fn from(command: Command) -> Self {
Self {
commands: Arc::new(Mutex::new(vec![command])),
}
}
}
impl From<Vec<Command>> for CommandList {
fn from(commands: Vec<Command>) -> Self {
Self {
commands: Arc::new(Mutex::new(commands)),
}
}
}
impl<const SIZE: usize> From<[Command; SIZE]> for CommandList {
fn from(commands: [Command; SIZE]) -> Self {
Self {
commands: Arc::new(Mutex::new(Vec::from(commands))),
}
}
}
impl CommandList {
/// Returns `true` if the list is empty.
pub fn is_empty(&self) -> bool {
self.commands.lock().expect("not poisoned").is_empty()
}
/// Spawn each command of the list one after the other.
///
/// The caller is responsible to wait the commands.
pub fn spawn(&mut self, mut callback: impl FnMut(io::Result<Child>) -> bool) {
for process in self.commands.lock().expect("not poisoned").iter_mut() {
if !callback(process.spawn()) {
break;
}
}
}
/// Run all the commands sequentially using [`std::process::Command::status`] and stop at the
/// first failure.
pub fn status(&mut self) -> io::Result<ExitStatus> {
for process in self.commands.lock().expect("not poisoned").iter_mut() {
let exit_status = process.status()?;
if !exit_status.success() {
return Ok(exit_status);
}
}
Ok(Default::default())
}
}
/// Shared reader/writer synchronization primitive used to coordinate watch-driven
/// command execution with external code.
///
/// Clone this type to share the same lock across threads/components.
#[derive(Clone, Debug, Default)]
pub struct WatchLock(Arc<RwLock<()>>);
impl WatchLock {
/// Create a new lock instance.
pub fn new() -> Self {
Self::default()
}
/// Acquire a shared read lock, blocking until no writer holds the lock.
///
/// Multiple readers may hold this lock concurrently.
pub fn read(&self) -> Result<RwLockReadGuard<'_, ()>, PoisonError<RwLockReadGuard<'_, ()>>> {
self.0.read()
}
/// Acquire an exclusive write lock, blocking until all readers/writers release it.
///
/// Use this for operations that mutate shared state (e.g. rebuild output files).
pub fn write(&self) -> Result<RwLockWriteGuard<'_, ()>, PoisonError<RwLockWriteGuard<'_, ()>>> {
self.0.write()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn exclude_relative_path() {
let watch = Watch::default().exclude_workspace_path("src/watch.rs");
assert!(
watch.is_excluded_path(
metadata()
.workspace_root
.join("src")
.join("watch.rs")
.as_std_path()
)
);
assert!(!watch.is_excluded_path(metadata().workspace_root.join("src").as_std_path()));
}
#[test]
fn exclude_absolute_glob_path() {
let absolute = metadata()
.workspace_root
.join("src")
.join("**")
.join("*.rs");
let mut watch = Watch::default().exclude_path(absolute);
watch
.prepare_excludes()
.expect("exclude parsing should succeed");
assert_eq!(watch.exclude_globs.len(), 1);
assert!(
watch.is_excluded_path(
metadata()
.workspace_root
.join("src")
.join("lib.rs")
.as_std_path()
)
);
}
#[test]
fn exclude_workspace_glob_path() {
let mut watch = Watch::default().exclude_workspace_path("src/**/*.rs");
watch
.prepare_excludes()
.expect("exclude parsing should succeed");
assert_eq!(watch.workspace_exclude_globs.len(), 1);
assert!(
watch.is_excluded_path(
metadata()
.workspace_root
.join("src")
.join("lib.rs")
.as_std_path()
)
);
}
#[test]
fn exclude_workspace_absolute_glob_path() {
let absolute = metadata()
.workspace_root
.join("src")
.join("**")
.join("*.rs");
let mut watch = Watch::default().exclude_workspace_path(absolute);
watch
.prepare_excludes()
.expect("exclude parsing should succeed");
assert_eq!(watch.workspace_exclude_globs.len(), 1);
assert!(
watch.is_excluded_path(
metadata()
.workspace_root
.join("src")
.join("lib.rs")
.as_std_path()
)
);
}
#[test]
fn exclude_workspace_glob_non_match() {
let mut watch = Watch::default().exclude_workspace_path("tests/**/*.rs");
watch
.prepare_excludes()
.expect("exclude parsing should succeed");
assert!(
!watch.is_excluded_path(
metadata()
.workspace_root
.join("src")
.join("lib.rs")
.as_std_path()
)
);
}
#[test]
fn glob_detection() {
assert!(Watch::is_glob_pattern(Path::new("src/**/*.rs")));
assert!(Watch::is_glob_pattern(Path::new("foo?.rs")));
#[cfg(not(windows))]
assert!(Watch::is_glob_pattern(Path::new("[ab].rs")));
#[cfg(windows)]
assert!(!Watch::is_glob_pattern(Path::new("[ab].rs")));
assert!(!Watch::is_glob_pattern(Path::new("src/lib.rs")));
}
#[test]
fn invalid_glob_pattern() {
let err = Watch::compile_glob(Path::new("[abc")).expect_err("should fail");
assert!(
err.to_string().contains("invalid glob pattern"),
"unexpected error: {err}"
);
}
#[test]
fn command_list_froms() {
let _: CommandList = Command::new("foo").into();
let _: CommandList = vec![Command::new("foo")].into();
let _: CommandList = [Command::new("foo")].into();
}
}