-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
helpers.rs
153 lines (130 loc) · 4.26 KB
/
helpers.rs
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
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use anyhow::anyhow;
use anyhow::Result;
use cmd_lib::{run_cmd, run_fun};
use speculoos::assert_that;
use speculoos::iter::ContainingIntoIterAssertions;
use speculoos::option::OptionAssertions;
use cocogitto::git::tag::Tag;
use cocogitto::settings::{MonoRepoPackage, Settings};
use cocogitto::CONFIG_PATH;
pub fn init_monorepo(settings: &mut Settings) -> Result<()> {
let mut packages = HashMap::new();
packages.insert(
"one".to_string(),
MonoRepoPackage {
path: PathBuf::from("one"),
..Default::default()
},
);
settings.packages = packages;
let settings = toml::to_string(&settings)?;
git_init()?;
run_cmd!(
echo $settings > cog.toml;
git add .;
git commit -m "chore: first commit";
mkdir one;
echo "changes" > one/file;
git add .;
git commit -m "feat: package one feature";
)?;
Ok(())
}
/// - Init a repository in the current directory
/// - Setup a local git user named Tom <toml.bombadil@themail.org>
pub fn git_init() -> Result<()> {
run_cmd!(
git init -b master;
git config --local commit.gpgsign false;
git config --local user.name Tom;
git config --local user.email toml.bombadil@themail.org;
)?;
Ok(())
}
/// - Init a repository in the given path
/// - Change the current directory to the newly created repository
/// - Setup a local git user named Tom <toml.bombadil@themail.org>
pub fn git_init_and_set_current_path(path: &str) -> Result<()> {
run_cmd!(
git init $path;
cd $path;
git config --local commit.gpgsign false;
)?;
std::env::set_current_dir(path).expect("Unable to move into to test repository");
Ok(())
}
/// Can be used to make assertion on 'git status' output.
pub fn git_status() -> Result<String> {
run_fun!(git status).map_err(|e| anyhow!(e))
}
/// Write the given content to the provided path and add it to the git index
pub fn git_add<S: AsRef<Path>>(content: &str, path: S) -> Result<()>
where
S: ToString,
{
let path = path.to_string();
run_cmd!(
echo "writing $content to $path";
echo $content > $path;
git add $path;
)
.map_err(|e| anyhow!(e))
}
/// Create an empty git commit and return its sha1
pub fn git_commit(message: &str) -> Result<String> {
run_fun!(
git commit --allow-empty -q -m $message;
git log --format=%H -n 1;
)
.map_err(|e| anyhow!(e))
}
pub fn git_tag(tag: &str) -> Result<()> {
run_cmd!(
git tag $tag;
)
.map_err(|e| anyhow!(e))
}
pub fn assert_tag_exists(tag: &str) -> Result<()> {
let tags = run_fun!(git --no-pager tag)?;
let tags: Vec<&str> = tags.split('\n').collect();
assert_that!(tags).contains(tag);
Ok(())
}
pub fn assert_tag_does_not_exist(tag: &str) -> Result<()> {
let tags = run_fun!(git --no-pager tag)?;
let tags: Vec<&str> = tags.split('\n').collect();
assert_that!(tags).does_not_contain(tag);
Ok(())
}
pub fn assert_latest_tag(tag: &str) -> Result<()> {
let tags = run_fun!(git --no-pager tag)?;
let tag = Tag::from_str(tag, None, None)?;
let mut tags: Vec<Tag> = tags
.split('\n')
.filter_map(|tag| Tag::from_str(tag, None, None).ok())
.collect();
tags.sort();
assert_that!(tags.last()).is_some().is_equal_to(&tag);
Ok(())
}
pub fn assert_tag_is_annotated(tag: &str) -> Result<()> {
let objtype = run_fun!(git for-each-ref --format="%(objecttype)" refs/tags/$tag)?;
let objtype: Vec<&str> = objtype.split('\n').collect();
assert_that!(objtype.first()).is_some().is_equal_to(&"tag");
Ok(())
}
/// Git log showing only the HEAD commit message, this can be used to make assertion on the last commit
pub fn git_log_head_message() -> Result<String> {
run_fun!(git log -1 --pretty=%B).map_err(|e| anyhow!(e))
}
/// Git log showing only the HEAD commit sha, this can be used to make assertion on the last commit
pub fn git_log_head_sha() -> Result<String> {
run_fun!(git log -1 --pretty=%H).map_err(|e| anyhow!(e))
}
/// Create an empty `cog.toml` config file in the current directory
pub fn create_empty_config() -> Result<()> {
std::fs::File::create(CONFIG_PATH)?;
Ok(())
}