grass/dev/strategy/git/
mock.rs

1use crate::dev::public::api::RepositoryLocation;
2
3use super::{GitStrategy, GitStrategyError, RepositoryChangeStatus, Result};
4
5/// Strategy used for mocking
6///
7/// # Data
8///
9/// This strategy simulates the following data.
10/// Each repository has an associated state.
11/// The first level are the categories.
12/// One indentation are the repositories.
13///
14/// - all_good (all of these are working and have no changes)
15///   - first
16///   - second
17///   - third
18/// - with_changes
19///   - first (no changes)
20///   - second (no repository)
21///   - third (9 uncommitted changes)
22/// - with_error
23///   - first (invalid repository)
24///   - second (unsufficient file permissions)
25///
26/// # remotes
27///
28/// This stratgy simulates the following remotes:
29///
30/// - good_remote (no errors)
31/// - no_access (authentication error)
32/// - bad_response (invalid response)
33#[derive(Default)]
34pub struct MockGitStrategy;
35
36impl GitStrategy for MockGitStrategy {
37    fn clean<T>(&self, repository: T) -> Result<()>
38    where
39        T: Into<RepositoryLocation>,
40    {
41        let repository: RepositoryLocation = repository.into();
42        let repository = (repository.category.as_ref(), repository.repository.as_str());
43        match repository {
44            ("all_good" | "with_changes", _) => Ok(()),
45            ("with_error", "first") => Err(GitStrategyError::RepositoryError {
46                message: "Mocked error".into(),
47                reason: "invalid repository".into(),
48            }),
49            ("with_error", "second") => Err(GitStrategyError::FileSystemError {
50                message: "Mocked error".into(),
51                reason: "insufficient permission".into(),
52                reasons: vec![],
53            }),
54            _ => Err(GitStrategyError::RepositoryNotFound {
55                message: "Mocked error".into(),
56                reason: "cannot find repository".into(),
57            }),
58        }
59    }
60
61    fn clone<T, U>(&self, repository: T, remote: U) -> Result<()>
62    where
63        T: Into<RepositoryLocation>,
64        U: AsRef<str>,
65    {
66        let repository: RepositoryLocation = repository.into();
67        let repository = (repository.category.as_ref(), repository.repository.as_str());
68        match repository {
69            ("all_good" | "with_changes" | "with_error", "first" | "second")
70            | ("all_good" | "with_changes", "third") => Err(GitStrategyError::RepositryExists {
71                message: "Mocked error".into(),
72                reason: "Can't fetch repository because it already exists locally".into(),
73            }),
74            ("all_good" | "with_changes" | "wth_error", _) => Ok(()),
75            _ => Err(GitStrategyError::RepositoryNotFound {
76                message: "Mocked error".into(),
77                reason: "Category does not exist".into(),
78            }),
79        }?;
80
81        match remote.as_ref() {
82            "good_remote" => Ok(()),
83            "no_access" => Err(GitStrategyError::RemoteAuthenticationError {
84                message: "Mocked error".into(),
85                reason: "You are not authorized to access this remote".into(),
86            }),
87            "bad_response" => Err(GitStrategyError::RemoteFetchError {
88                message: "Mocked error".into(),
89                reason: "The remote returned an invalid response".into(),
90            }),
91            _ => Err(GitStrategyError::RemoteFetchError {
92                message: "Mocked error".into(),
93                reason: "Could not access the remote".into(),
94            }),
95        }
96    }
97
98    fn get_changes<T>(&self, repository: T) -> Result<RepositoryChangeStatus>
99    where
100        T: Into<RepositoryLocation>,
101    {
102        let repository: RepositoryLocation = repository.into();
103        let repository = (repository.category.as_ref(), repository.repository.as_str());
104        match repository {
105            ("all_good", "first" | "second" | "third") => Ok(RepositoryChangeStatus::UpToDate),
106            ("with_changes", "first") => Ok(RepositoryChangeStatus::UpToDate),
107            ("with_changes", "second") => Ok(RepositoryChangeStatus::NoRepository),
108            ("with_changes", "third") => {
109                Ok(RepositoryChangeStatus::UncommittedChanges { num_changes: 9 })
110            }
111            ("with_error", "first") => Err(GitStrategyError::RepositoryError {
112                message: "Mocked error".into(),
113                reason: "invalid repository".into(),
114            }),
115            ("with_error", "second") => Err(GitStrategyError::FileSystemError {
116                message: "Mocked error".into(),
117                reason: "insufficient permission".into(),
118                reasons: vec![],
119            }),
120            ("all_good" | "with_changes" | "with_error", _) => {
121                Err(GitStrategyError::RepositoryNotFound {
122                    message: "Mocked error".into(),
123                    reason: "repository not found".into(),
124                })
125            }
126            _ => Err(GitStrategyError::RepositoryNotFound {
127                message: "Mocked error".into(),
128                reason: "category not found".into(),
129            }),
130        }
131    }
132}