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
//! # Overview
//! Update informer for CLI applications written in Rust. It checks for a new version on [`Crates.io`], [`GitHub`] and [`PyPI`].
//!
//! ## Benefits
//! * Support of [Crates.io](#cratesio), [GitHub](#github) and [PyPI](#pypi).
//! * [Ability to implement your own registry to check updates](#implementing-your-own-registry).
//! * Configurable [check frequency](#interval) and [request timeout](#request-timeout).
//! * Minimum dependencies - only [`directories`], [`ureq`], [`semver`] and [`serde`].
//!
//! ## Usage
//!
//! By default, `update-informer` can only check on [`Crates.io`].
//! To enable support for other registries, use `features`:
//!
//! ```toml
//! [dependencies]
//! update-informer = { version = "0.5.0", default_features = false, features = ["github"] }
//! ```
//!
//! Available features:
//!
//! | Name   | Default? |
//! |--------|----------|
//! | cargo  | Yes      |
//! | github | No       |
//! | pypi   | No       |
//!
//! ## Crates.io
//!
//! To check for a new version on Crates.io, use the [`UpdateInformer::check_version`] function. This function takes the project name and current version:
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0");
//! if let Some(version) = informer.check_version().ok().flatten() {
//!     println!("New version is available: {}", version);
//! }
//! ```
//!
//! Also, you can take the name and version of the project from `Cargo` using environment variables:
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let name = env!("CARGO_PKG_NAME");
//! let version = env!("CARGO_PKG_VERSION");
//! update_informer::new(registry::Crates, name, version).check_version();
//! ```
//!
//! ## Interval
//!
//! Note that the first check will start only after the interval has expired.
//! By default, the interval is 24 hours, but you can change it:
//!
//! ```rust
//! use std::time::Duration;
//! use update_informer::{registry, Check};
//!
//! const EVERY_HOUR: Duration = Duration::from_secs(60 * 60);
//!
//! let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").interval(EVERY_HOUR);
//! informer.check_version(); // The check will start only after an hour
//! ```
//!
//! ## Cache file
//!
//! By default, `update-informer` creates a file in the cache directory to avoid spam requests to the registry API.
//!
//! In order not to cache requests, use a zero interval:
//!
//! ```rust
//! use std::time::Duration;
//! use update_informer::{registry, Check};
//!
//! let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").interval(Duration::ZERO);
//! informer.check_version();
//! ```
//!
//! ## Request timeout
//!
//! You can also change the request timeout. By default, it is 5 seconds:
//!
//! ```rust
//! use std::time::Duration;
//! use update_informer::{registry, Check};
//!
//! const THIRTY_SECONDS: Duration = Duration::from_secs(30);
//!
//! let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").timeout(THIRTY_SECONDS);
//! informer.check_version();
//! ```
//!
//! ## GitHub
//!
//! To check for a new version on GitHub (note that the project name must contain the owner):
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let informer = update_informer::new(registry::GitHub, "owner/repo", "0.1.0");
//! informer.check_version();
//! ```
//!
//! ## PyPi
//!
//! To check for a new version on PyPI:
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let informer = update_informer::new(registry::PyPI, "package_name", "0.1.0");
//! informer.check_version();
//! ```
//!
//! ## Implementing your own registry
//!
//! You can implement your own registry to check updates. For example:
//!
//! ```rust
//! use std::time::Duration;
//! use update_informer::{registry, Check, Package, Registry, Result, Version};
//!
//! struct YourOwnRegistry;
//!
//! impl Registry for YourOwnRegistry {
//!     const NAME: &'static str = "your_own_registry";
//!
//!     fn get_latest_version(pkg: &Package, _current_version: &Version, _timeout: Duration) -> Result<Option<String>> {
//!         let url = format!("https://your_own_registry.com/{}/latest-version", pkg);
//!         let result = ureq::get(&url).call()?.into_string()?;
//!         let version = result.trim().to_string();
//!
//!         Ok(Some(version))
//!     }
//! }
//!
//! let informer = update_informer::new(YourOwnRegistry, "package_name", "0.1.0");
//! informer.check_version();
//! ```
//!
//! ## Tests
//!
//! In order not to check for updates in tests, you can use the [`FakeUpdateInformer::check_version`] function, which returns the desired version.
//!
//! Example of usage in unit tests:
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let name = "crate_name";
//! let version = "0.1.0";
//!
//! #[cfg(not(test))]
//! let informer = update_informer::new(registry::Crates, name, version);
//!
//! #[cfg(test)]
//! let informer = update_informer::fake(registry::Crates, name, version, "1.0.0");
//!
//! if let Some(version) = informer.check_version().ok().flatten() {
//!     println!("New version is available: {}", version);
//! }
//! ```
//!
//! ## Integration tests
//!
//! To use the [`FakeUpdateInformer::check_version`] function in integration tests, you must first add the feature flag to `Cargo.toml`:
//!
//! ```toml
//! [features]
//! stub_check_version = []
//! ```
//!
//! Then use this feature flag in your code and integration tests:
//!
//! ```rust
//! use update_informer::{registry, Check};
//!
//! let name = "crate_name";
//! let version = "0.1.0";
//!
//! #[cfg(not(feature = "stub_check_version"))]
//! let informer = update_informer::new(registry::Crates, name, version);
//!
//! #[cfg(feature = "stub_check_version")]
//! let informer = update_informer::fake(Crates, name, version, "1.0.0");
//!
//! informer.check_version();
//! ```
//!
//! [`Crates.io`]: https://crates.io
//! [`GitHub`]: https://github.com
//! [`PyPI`]: https://pypi.org
//! [`directories`]: https://github.com/dirs-dev/directories-rs
//! [`ureq`]: https://github.com/algesten/ureq
//! [`semver`]: https://github.com/dtolnay/semver
//! [`serde`]: https://github.com/serde-rs/serde

#[doc = include_str!("../README.md")]
use crate::version_file::VersionFile;
use std::time::Duration;

pub use package::Package;
pub use registry::Registry;
pub use version::Version;

mod http;
mod package;
mod version;
mod version_file;

#[cfg(test)]
mod test_helper;

/// A registry service that stores information about releases.
pub mod registry;

type Error = Box<dyn std::error::Error>;
pub type Result<T> = std::result::Result<T, Error>;

pub trait Check {
    /// Checks for a new version in the registry.
    fn check_version(&self) -> Result<Option<Version>> {
        Ok(None)
    }
}

/// Checks for a new version on Crates.io, GitHub and PyPi.
pub struct UpdateInformer<R: Registry, N: AsRef<str>, V: AsRef<str>> {
    _registry: R,
    name: N,
    version: V,
    interval: Duration,
    timeout: Duration,
}

/// Constructs a new `UpdateInformer`.
///
/// # Arguments
///
/// * `registry` - A registry service such as Crates.io or GitHub.
/// * `name` - A project name.
/// * `version` - Current version of the project.
///
/// # Examples
///
/// ```rust
/// use update_informer::{registry, Check};
///
/// let name = env!("CARGO_PKG_NAME");
/// let version = env!("CARGO_PKG_VERSION");
/// let informer = update_informer::new(registry::Crates, name, version);
/// ```
pub fn new<R, N, V>(registry: R, name: N, version: V) -> UpdateInformer<R, N, V>
where
    R: Registry,
    N: AsRef<str>,
    V: AsRef<str>,
{
    UpdateInformer {
        _registry: registry,
        name,
        version,
        interval: Duration::from_secs(60 * 60 * 24), // Once a day
        timeout: Duration::from_secs(5),
    }
}

impl<R: Registry, N: AsRef<str>, V: AsRef<str>> UpdateInformer<R, N, V> {
    /// Constructs a new `UpdateInformer`.
    ///
    /// # Arguments
    ///
    /// * `registry` - A registry service such as Crates.io or GitHub.
    /// * `name` - A project name.
    /// * `version` - Current version of the project.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use update_informer::{registry, Check, UpdateInformer};
    ///
    /// let name = env!("CARGO_PKG_NAME");
    /// let version = env!("CARGO_PKG_VERSION");
    /// let informer = UpdateInformer::new(registry::Crates, name, version);
    /// ```
    #[deprecated(since = "0.5.0", note = "Use `update_informer::new` instead")]
    pub fn new(registry: R, name: N, version: V) -> Self {
        crate::new(registry, name, version)
    }

    /// Sets an interval how often to check for a new version.
    ///
    /// # Arguments
    ///
    /// * `interval` - An interval in seconds. By default, it is 24 hours.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    /// use update_informer::{registry, Check};
    ///
    /// const EVERY_HOUR: Duration = Duration::from_secs(60 * 60);
    ///
    /// let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").interval(EVERY_HOUR);
    /// informer.check_version(); // The check will start only after an hour
    /// ```
    pub fn interval(self, interval: Duration) -> Self {
        Self { interval, ..self }
    }

    /// Sets a request timeout.
    ///
    /// # Arguments
    ///
    /// * `timeout` - A request timeout. By default, it is 5 seconds.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    /// use update_informer::{registry, Check};
    ///
    /// const THIRTY_SECONDS: Duration = Duration::from_secs(30);
    ///
    /// let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").timeout(THIRTY_SECONDS);
    /// informer.check_version();
    /// ```
    pub fn timeout(self, timeout: Duration) -> Self {
        Self { timeout, ..self }
    }
}

impl<R: Registry, N: AsRef<str>, V: AsRef<str>> Check for UpdateInformer<R, N, V> {
    /// Checks for a new version in the registry.
    ///
    /// # Examples
    ///
    /// To check for a new version on Crates.io:
    ///
    /// ```rust
    /// use update_informer::{registry, Check};
    ///
    /// let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0");
    /// informer.check_version();
    /// ```
    fn check_version(&self) -> Result<Option<Version>> {
        let pkg = Package::new(self.name.as_ref());
        let current_version = Version::parse(self.version.as_ref())?;

        // If the interval is zero, don't use the cache file
        let latest_version = if self.interval.is_zero() {
            match R::get_latest_version(&pkg, &current_version, self.timeout)? {
                Some(v) => v,
                None => return Ok(None),
            }
        } else {
            let latest_version_file = VersionFile::new(R::NAME, &pkg, self.version.as_ref())?;
            let last_modified = latest_version_file.last_modified()?;

            if last_modified >= self.interval {
                // This is needed to update mtime of the file
                latest_version_file.recreate_file()?;

                match R::get_latest_version(&pkg, &current_version, self.timeout)? {
                    Some(v) => {
                        latest_version_file.write_version(&v)?;
                        v
                    }
                    None => return Ok(None),
                }
            } else {
                latest_version_file.get_version()?
            }
        };

        let latest_version = Version::parse(latest_version)?;

        if latest_version > current_version {
            return Ok(Some(latest_version));
        }

        Ok(None)
    }
}

/// Fake `UpdateInformer`. Used only for tests.
pub struct FakeUpdateInformer<V: AsRef<str>> {
    version: V,
}

/// Constructs a new `FakeUpdateInformer`.
///
/// # Arguments
///
/// * `registry` - A registry service such as Crates.io or GitHub (not used).
/// * `name` - A project name (not used).
/// * `version` - Current version of the project (not used).
/// * `interval` - An interval how often to check for a new version (not used).
/// * `new_version` - A desired version.
///
/// # Examples
///
/// ```rust
/// use update_informer::{registry, Check};
///
/// let informer = update_informer::fake(registry::Crates, "repo", "0.1.0", "1.0.0");
/// ```
pub fn fake<R, N, V>(_registry: R, _name: N, _version: V, new_version: V) -> FakeUpdateInformer<V>
where
    R: Registry,
    N: AsRef<str>,
    V: AsRef<str>,
{
    FakeUpdateInformer {
        version: new_version,
    }
}

impl<V: AsRef<str>> FakeUpdateInformer<V> {
    /// Constructs a new `FakeUpdateInformer`.
    ///
    /// # Arguments
    ///
    /// * `registry` - A registry service such as Crates.io or GitHub (not used).
    /// * `name` - A project name (not used).
    /// * `version` - Current version of the project (not used).
    /// * `interval` - An interval how often to check for a new version (not used).
    /// * `new_version` - A desired version.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use update_informer::{registry, Check, FakeUpdateInformer};
    ///
    /// let informer = FakeUpdateInformer::new(registry::Crates, "repo", "0.1.0", "1.0.0");
    /// ```
    #[deprecated(since = "0.5.0", note = "Use `update_informer::fake` instead")]
    pub fn new<R, N>(registry: R, name: N, version: V, new_version: V) -> Self
    where
        R: Registry,
        N: AsRef<str>,
        V: AsRef<str>,
    {
        crate::fake(registry, name, version, new_version)
    }

    /// Returns `FakeUpdateInformer`.
    pub fn interval(self, _interval: Duration) -> Self {
        self
    }

    /// Returns `FakeUpdateInformer`.
    pub fn timeout(self, _timeout: Duration) -> Self {
        self
    }
}

impl<V: AsRef<str>> Check for FakeUpdateInformer<V> {
    /// Returns the desired version as a new version.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use update_informer::{registry, Check};
    ///
    /// let informer = update_informer::fake(registry::Crates, "crate_name", "0.1.0", "1.0.0");
    /// let result = informer.check_version();
    /// assert!(result.is_ok());
    ///
    /// let version = result.unwrap();
    /// assert!(version.is_some());
    /// assert_eq!(version.unwrap().to_string(), "v1.0.0");
    /// ```
    fn check_version(&self) -> Result<Option<Version>> {
        let version = Version::parse(self.version.as_ref())?;

        Ok(Some(version))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{registry::Crates, test_helper::within_test_dir};
    use mockito::Mock;
    use std::fs;

    const PKG_NAME: &str = "repo";
    const CURRENT_VERSION: &str = "3.1.0";
    const LATEST_VERSION: &str = "3.1.1";

    fn mock_crates(pkg: &str) -> Mock {
        let pkg = Package::new(pkg);
        let (mock, _) = crate::test_helper::mock_crates(
            &pkg,
            200,
            "tests/fixtures/registry/crates/versions.json",
        );

        mock
    }

    #[test]
    fn no_new_version_with_interval_test() {
        within_test_dir(|_| {
            let informer = crate::new(Crates, PKG_NAME, CURRENT_VERSION);
            let result = informer.check_version();

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), None);
        });
    }

    #[test]
    fn no_new_version_on_registry_test() {
        within_test_dir(|_| {
            let _mock = mock_crates(PKG_NAME);
            let informer = crate::new(Crates, PKG_NAME, LATEST_VERSION).interval(Duration::ZERO);
            let result = informer.check_version();

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), None);
        });
    }

    #[test]
    fn check_version_on_crates_test() {
        within_test_dir(|_| {
            let _mock = mock_crates(PKG_NAME);
            let informer = crate::new(Crates, PKG_NAME, CURRENT_VERSION).interval(Duration::ZERO);
            let result = informer.check_version();
            let version = Version::parse(LATEST_VERSION).expect("parse version");

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some(version));
        });
    }

    #[test]
    fn return_version_from_file_test() {
        within_test_dir(|version_file| {
            fs::write(version_file, "4.0.0").expect("create file");

            let informer = crate::new(Crates, PKG_NAME, CURRENT_VERSION);
            let result = informer.check_version();
            let version = Version::parse("4.0.0").expect("parse version");

            assert!(result.is_ok());
            assert_eq!(result.unwrap(), Some(version));
        });
    }

    #[test]
    fn create_version_file_test() {
        within_test_dir(|version_file| {
            assert!(!version_file.exists());

            let informer = crate::new(Crates, PKG_NAME, CURRENT_VERSION);
            let result = informer.check_version();
            assert!(result.is_ok());
            assert!(version_file.exists());

            let version = fs::read_to_string(version_file).expect("read file");
            assert_eq!(version, CURRENT_VERSION);
        });
    }

    #[test]
    fn do_not_create_version_file_test() {
        within_test_dir(|version_file| {
            assert!(!version_file.exists());

            let _mock = mock_crates(PKG_NAME);
            let informer = crate::new(Crates, PKG_NAME, CURRENT_VERSION).interval(Duration::ZERO);
            let result = informer.check_version();

            assert!(result.is_ok());
            assert!(!version_file.exists());
        });
    }

    #[test]
    fn check_version_with_string_name_test() {
        within_test_dir(|_| {
            let pkg_name = format!("{}/{}", "owner", PKG_NAME);
            let informer = crate::new(Crates, pkg_name, CURRENT_VERSION);
            let result = informer.check_version();

            assert!(result.is_ok());
        });
    }

    #[test]
    fn check_version_with_string_version_test() {
        within_test_dir(|_| {
            let version = String::from(CURRENT_VERSION);
            let informer = crate::new(Crates, PKG_NAME, version);
            let result = informer.check_version();

            assert!(result.is_ok());
        });
    }

    #[test]
    fn check_version_with_amp_string_test() {
        within_test_dir(|_| {
            let pkg_name = format!("{}/{}", "owner", PKG_NAME);
            let version = String::from(CURRENT_VERSION);
            let informer = crate::new(Crates, &pkg_name, &version);
            let result = informer.check_version();

            assert!(result.is_ok());
        });
    }

    #[test]
    fn fake_check_version_test() {
        let version = "1.0.0";
        let informer = crate::fake(Crates, PKG_NAME, CURRENT_VERSION, version)
            .interval(Duration::ZERO)
            .timeout(Duration::ZERO);
        let result = informer.check_version();
        let version = Version::parse(version).expect("parse version");

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Some(version));
    }
}