summaryrefslogtreecommitdiffstats
path: root/tests/version-sync/src/lib.rs
blob: 7e74f80dad590624ebcc1a8c06602fd6ff30e01b (plain)
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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

#[cfg(test)] extern crate version_sync;
#[cfg(test)] extern crate walkdir;
#[cfg(test)] extern crate env_logger;
#[cfg(test)] #[macro_use] extern crate log;
#[cfg(test)] extern crate toml_query;
#[cfg(test)] extern crate toml;

#[cfg(test)] use std::path::PathBuf;

#[cfg(test)]
fn setup_logging() {
    let _ = env_logger::try_init();
}

#[test]
fn test_readme() {
    version_sync::assert_markdown_deps_updated!("../../README.md");
}

#[test]
fn test_doc() {
    version_sync::assert_contains_regex!("../../doc/src/00000.md", "^version: {version}$");
    version_sync::assert_contains_regex!("../../doc/src/02000-store.md", "^version = \"{version}\"$");
    version_sync::assert_contains_regex!("../../doc/src/03020-writing-modules.md", "version = \"{version}\"");

    version_sync::assert_contains_regex!("../../doc/user/src/approach.md", "^version = \"{version}\"$");
}

#[test]
fn test_all_cargotoml_files() {
    use toml::Value;

    setup_logging();

    let current_version = env!("CARGO_PKG_VERSION");
    let imag_root = PathBuf::from(format!("{}/../../", env!("CARGO_MANIFEST_DIR")));
    println!("imag_root = {}", imag_root.display());

    walkdir::WalkDir::new(&imag_root)
        .follow_links(false)
        .into_iter()
        .collect::<Result<Vec<_>, _>>()
        .expect("Failed collecting files")
        .into_iter()
        .filter(|e| !e.path().to_str().unwrap().contains("target"))
        .filter_map(|element| if element.file_type().is_file() && element.path().ends_with("Cargo.toml") {
            debug!("Using = {:?}", element);
            Some(element.into_path())
        } else {
            debug!("Ignoring = {:?}", element);
            None
        })
        .for_each(|cargotoml| {
            let filecontent = std::fs::read_to_string(&cargotoml).unwrap_or_else(|_| panic!("Failed to read {}", cargotoml.display()));
            let toml = filecontent.parse::<Value>().unwrap_or_else(|_| panic!("Failed to parse toml: {}", cargotoml.display()));

            match toml.get("dependencies") {
                Some(Value::Table(ref tab)) => {
                    for (k, v) in tab.iter() {
                        if k.contains("libimag") {
                            match v {
                                Value::String(s) => assert!(s.contains(current_version)),
                                Value::Table(ref dep) => {
                                    let version = dep.get("version").unwrap_or_else(|| panic!("No 'version' key for dependencies at {}", cargotoml.display()));
                                    let version_str = version.as_str().unwrap();
                                    assert!(version_str.contains(current_version), "failed for: {} ('{}')", cargotoml.display(), version_str)
                                },
                                _ => unimplemented!(),
                            }
                        }
                    }
                },
                Some(_) => panic!("Dependencies is not a table?"),
                None => /* ignore if there is no "dependencies" */ {},
            }

            match toml.get("dev-dependencies") {
                Some(Value::Table(ref tab)) => {
                    for (k, v) in tab.iter() {
                        if k.contains("libimag") {
                            match v {
                                Value::String(s) => assert!(s.contains(current_version)),
                                Value::Table(ref dep) => {
                                    let version = dep.get("version").unwrap_or_else(|| panic!("No 'version' key for dev-dependencies at {}", cargotoml.display()));
                                    let version_str = version.as_str().unwrap();
                                    assert!(version_str.contains(current_version), "failed for: {} ('{}')", cargotoml.display(), version_str)
                                },
                                _ => unimplemented!(),
                            }
                        }
                    }
                },
                Some(_) => panic!("dev-dependencies is not a table?"),
                None => /* ignore if there is no "dependencies" */ {},
            }
        });
}