summaryrefslogtreecommitdiff
path: root/crates/gfold/src/main.rs
blob: b0d069a440b46117a8faa9707aa64192cdf47b49 (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
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
//! [gfold](https://github.com/nickgerace/gfold) is a CLI-driven application that helps you keep
//! track of multiple Git repositories. The source code uses private modules rather than leveraging
//! a library via `lib.rs`.

use env_logger::Builder;
use log::debug;
use log::LevelFilter;
use std::env;

mod cli;
mod config;
mod display;
mod error;
mod report;
mod run;
mod status;

/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable and calls
/// [`cli::parse_and_run()`] to generate a [`config::Config`] and eventually call [`run::run()`].
fn main() -> anyhow::Result<()> {
    match env::var("RUST_LOG").is_err() {
        true => Builder::new().filter_level(LevelFilter::Off).init(),
        false => env_logger::init(),
    }
    debug!("initialized logger");

    cli::parse_and_run()?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::config::{ColorMode, Config, DisplayMode};
    use crate::report::{LabeledReports, Report};
    use crate::status::Status;

    use anyhow::{anyhow, Result};
    use git2::ErrorCode;
    use git2::Repository;
    use pretty_assertions::assert_eq;
    use std::collections::BTreeMap;
    use std::path::{Path, PathBuf};
    use std::{env, fs, io};

    /// This integration test for `gfold` covers an end-to-end usage scenario. It does not
    /// _remove_ anything in the filesystem (for saftey), so you must delete the `test`
    /// directory underneath `target` to regenerate a clean dataset.
    #[test]
    fn integration() -> Result<()> {
        // Test directory structure within "target":
        // └── test
        //     ├── bar
        //     ├── baz
        //     ├── foo
        //     │   └── newfile
        //     └── nested
        //         ├── one
        //         │   └── newfile
        //         ├── three
        //         └── two
        let test_directory = integration_init()?;
        create_directory(&test_directory)?;

        for name in ["foo", "bar", "baz"] {
            let current = test_directory.join(name);
            create_directory(&current)?;
            Repository::init(&current)?;

            if name == "foo" {
                create_file(&current.join("newfile"))?;
            }
        }

        let nested = test_directory.join("nested");
        create_directory(&nested)?;
        for name in ["one", "two", "three"] {
            let current = nested.join(name);
            create_directory(&current)?;
            let repository = Repository::init(&current)?;

            if name == "one" {
                create_file(&current.join("newfile"))?;
            }

            if name == "two" {
                if let Err(e) = repository.remote("origin", "https://github.com/nickgerace/gfold") {
                    if e.code() != ErrorCode::Exists {
                        return Err(e.into());
                    }
                }
            }

            if name == "three" {
                if let Err(e) = repository.remote("fork", "https://github.com/nickgerace/gfold") {
                    if e.code() != ErrorCode::Exists {
                        return Err(e.into());
                    }
                }
            }
        }

        let mut config = Config::new()?;
        config.path = test_directory.clone();
        config.color_mode = ColorMode::Never;
        assert!(run::run(&config).is_ok());

        // Now, let's ensure our reports are what we expect.
        let mut expected_reports: LabeledReports = BTreeMap::new();

        let key = test_directory
            .to_str()
            .ok_or_else(|| anyhow!("could not convert PathBuf to &str"))?
            .to_string();
        let mut reports = vec![
            Report::new(
                &test_directory.join("foo"),
                "HEAD",
                &Status::Unclean,
                None,
                None,
            )?,
            Report::new(
                &test_directory.join("bar"),
                "HEAD",
                &Status::Clean,
                None,
                None,
            )?,
            Report::new(
                &test_directory.join("baz"),
                "HEAD",
                &Status::Clean,
                None,
                None,
            )?,
        ];
        reports.sort_by(|a, b| a.name.cmp(&b.name));
        expected_reports.insert(Some(key), reports);

        let nested_test_dir = test_directory.join("nested");
        let key = nested_test_dir
            .to_str()
            .ok_or_else(|| anyhow!("could not convert PathBuf to &str"))?
            .to_string();
        let mut reports = vec![
            Report::new(
                &nested_test_dir.join("one"),
                "HEAD",
                &Status::Unclean,
                None,
                None,
            )?,
            Report::new(
                &nested_test_dir.join("two"),
                "HEAD",
                &Status::Clean,
                Some("https://github.com/nickgerace/gfold".to_string()),
                None,
            )?,
            Report::new(
                &nested_test_dir.join("three"),
                "HEAD",
                &Status::Clean,
                Some("https://github.com/nickgerace/gfold".to_string()),
                None,
            )?,
        ];
        reports.sort_by(|a, b| a.name.cmp(&b.name));
        expected_reports.insert(Some(key), reports);

        // Use classic display mode to avoid collecting email results.
        config.display_mode = DisplayMode::Classic;
        let found_labeled_reports = report::generate_reports(&config.path, &config.display_mode)?;
        let mut found_labeled_reports_sorted = LabeledReports::new();
        for labeled_report in found_labeled_reports {
            let mut value = labeled_report.1;
            value.sort_by(|a, b| a.name.cmp(&b.name));
            found_labeled_reports_sorted.insert(labeled_report.0.clone(), value.clone());
        }

        assert_eq!(found_labeled_reports_sorted, expected_reports);
        Ok(())
    }

    /// Ensure we are underneath the repository root. Safely create the test directory.
    fn integration_init() -> Result<PathBuf> {
        let manifest_directory = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let repo_root = manifest_directory
            .parent()
            .ok_or_else(|| anyhow!("could not get parent"))?
            .parent()
            .ok_or_else(|| anyhow!("could not get parent"))?;
        assert!(Repository::open(repo_root).is_ok());

        let target = repo_root.join("target");
        create_directory(&target)?;
        let test = target.join("test");
        Ok(test)
    }

    fn create_directory(path: &Path) -> Result<()> {
        if let Err(e) = fs::create_dir(path) {
            if e.kind() != io::ErrorKind::AlreadyExists {
                return Err(anyhow!(
                    "could not create directory ({:?}) due to error kind: {:?}",
                    path,
                    e.kind()
                ));
            }
        }
        Ok(())
    }

    fn create_file(path: &Path) -> Result<()> {
        if let Err(e) = fs::File::create(path) {
            if e.kind() != io::ErrorKind::AlreadyExists {
                return Err(anyhow!(
                    "could not create file ({:?}) due to error kind: {:?}",
                    path,
                    e.kind()
                ));
            }
        }
        Ok(())
    }
}