Skip to content

Commit 8d0916f

Browse files
authored
Merge pull request #11 from KnpLabs/test/develop-and-master-workflow
[RFR] Test/develop and master workflow
2 parents a56fa16 + 5ec085f commit 8d0916f

File tree

4 files changed

+219
-20
lines changed

4 files changed

+219
-20
lines changed

tests/branch_test.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,30 @@ fn it_should_detect_changes_on_branch() {
3232

3333
set_remote_repo(&local_repo_path, &remote_repo_path);
3434

35-
create_and_push_initial_commit(&local_repo_path);
35+
create_and_push_initial_commit(
36+
&local_repo_path,
37+
&String::from("master"),
38+
);
3639

3740
let branch_name = String::from("test/new-branch");
3841
checkout_new_branch(&local_repo_path, &branch_name);
3942

40-
append_content_to_api_readme(&local_repo_path);
43+
append_content_to_api_readme(
44+
&local_repo_path,
45+
&String::from("\nmore content"),
46+
);
4147

4248
commit_and_push_changes(
4349
&local_repo_path,
4450
&branch_name,
4551
&String::from("first commit"),
4652
);
4753

48-
create_api_test_file(&local_repo_path);
54+
create_api_test_file(
55+
&local_repo_path,
56+
&String::from("test.txt"),
57+
&String::from("test content"),
58+
);
4959

5060
commit_and_push_changes(
5161
&local_repo_path,
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
mod utils;
2+
3+
use std::path::{
4+
Path,
5+
PathBuf,
6+
};
7+
use std::fs::read_to_string;
8+
use rand;
9+
use ssc::should_skip_ci;
10+
use utils::{
11+
create_remote_repo,
12+
create_local_repo,
13+
set_remote_repo,
14+
create_and_push_initial_commit,
15+
checkout_new_branch,
16+
append_content_to_api_readme,
17+
commit_and_push_changes,
18+
create_api_test_file,
19+
checkout_branch,
20+
merge_given_branch_on_current_branch_non_fast_forward,
21+
};
22+
23+
/*
24+
25+
This test file is meant to test a `master` and `develop` branches git based
26+
workflow.
27+
In this workflow, the `master` branch is ISO to the prod, and the `develop`
28+
branch contains the latest features.
29+
Developers usually issue new feature branches from the `develop` branch.
30+
The workflow is represented by the following schema :
31+
32+
master ---o-------o
33+
\ /
34+
develop o---o
35+
\ /
36+
feature1 o
37+
38+
39+
---> direction
40+
41+
*/
42+
43+
#[test]
44+
fn it_should_detect_changes_on_master_when_merging_develop_on_master_and_having_develop_as_the_base_branch() {
45+
// Setup test case
46+
47+
let rdm = rand::random::<u32>();
48+
let base_path = PathBuf::from(format!(
49+
"/tmp/ssc-functional-test-{}",
50+
rdm,
51+
));
52+
let remote_repo_path = create_remote_repo(&base_path);
53+
let local_repo_path = create_local_repo(&base_path);
54+
55+
set_remote_repo(&local_repo_path, &remote_repo_path);
56+
57+
create_and_push_initial_commit(
58+
&local_repo_path,
59+
&String::from("master"),
60+
);
61+
62+
let develop_branch_name = String::from("develop");
63+
checkout_new_branch(&local_repo_path, &develop_branch_name);
64+
65+
append_content_to_api_readme(
66+
&local_repo_path,
67+
&String::from("\nmore content"),
68+
);
69+
70+
commit_and_push_changes(
71+
&local_repo_path,
72+
&develop_branch_name,
73+
&String::from("first commit on develop"),
74+
);
75+
76+
let feature_branch_name = String::from("feature/first-one");
77+
checkout_new_branch(&local_repo_path, &feature_branch_name);
78+
79+
create_api_test_file(
80+
&local_repo_path,
81+
&String::from("test.txt"),
82+
&String::from("test content"),
83+
);
84+
85+
commit_and_push_changes(
86+
&local_repo_path,
87+
&feature_branch_name,
88+
&String::from("first commit on feature branch"),
89+
);
90+
91+
checkout_branch(
92+
&local_repo_path,
93+
&develop_branch_name,
94+
);
95+
96+
merge_given_branch_on_current_branch_non_fast_forward(
97+
&local_repo_path,
98+
&feature_branch_name,
99+
&String::from("merge feature branch into develop"),
100+
);
101+
102+
checkout_branch(
103+
&local_repo_path,
104+
&String::from("master"),
105+
);
106+
107+
merge_given_branch_on_current_branch_non_fast_forward(
108+
&local_repo_path,
109+
&develop_branch_name,
110+
&String::from("merge develop branch into master"),
111+
);
112+
113+
// Run should-skip-ci and make assertions
114+
115+
let mut api_app_path = PathBuf::from(&local_repo_path);
116+
api_app_path.push("apps/api");
117+
118+
let mut front_app_path = PathBuf::from(&local_repo_path);
119+
front_app_path.push("apps/front");
120+
121+
let witness_filename = "witness.txt";
122+
let witness_content = "stop command executed";
123+
let mut witness_filepath = PathBuf::from(&base_path);
124+
witness_filepath.push(&witness_filename);
125+
126+
let cmd = format!(
127+
"echo -n \"{}\" > {}",
128+
&witness_content,
129+
&witness_filepath.to_str().unwrap(),
130+
);
131+
132+
// assert that the file created by the stop command is not here before using
133+
// should-skip-ci
134+
assert_eq!(false, Path::new(&witness_filepath).exists());
135+
136+
// should not skip the CI as we made changes on the api app
137+
should_skip_ci(
138+
&local_repo_path,
139+
&vec![api_app_path],
140+
&cmd,
141+
&String::from("origin"),
142+
&develop_branch_name,
143+
);
144+
145+
// the stop command should not have been ran
146+
assert_eq!(false, Path::new(&witness_filepath).exists());
147+
148+
// should skip the CI as we did not make any changes on the front app
149+
should_skip_ci(
150+
&local_repo_path,
151+
&vec![front_app_path],
152+
&cmd,
153+
&String::from("origin"),
154+
&develop_branch_name,
155+
);
156+
157+
// the stop command should have been ran
158+
assert_eq!(true, Path::new(&witness_filepath).exists());
159+
160+
let actual_content = read_to_string(&witness_filepath)
161+
.expect("Unable to read witness file content")
162+
;
163+
164+
assert_eq!(&actual_content, &witness_content);
165+
}

tests/merge_commit_on_base_branch_test.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use utils::{
1616
append_content_to_api_readme,
1717
commit_and_push_changes,
1818
create_api_test_file,
19-
checkout_base_branch,
20-
merge_branch_on_base_branch_non_fast_forward,
19+
checkout_branch,
20+
merge_given_branch_on_current_branch_non_fast_forward,
2121
};
2222

2323
#[test]
@@ -34,33 +34,46 @@ fn it_should_detect_changes_on_a_merge_commit_on_master() {
3434

3535
set_remote_repo(&local_repo_path, &remote_repo_path);
3636

37-
create_and_push_initial_commit(&local_repo_path);
37+
create_and_push_initial_commit(
38+
&local_repo_path,
39+
&String::from("master"),
40+
);
3841

3942
let branch_name = String::from("test/new-branch");
4043
checkout_new_branch(&local_repo_path, &branch_name);
4144

42-
append_content_to_api_readme(&local_repo_path);
45+
append_content_to_api_readme(
46+
&local_repo_path,
47+
&String::from("\nmore content"),
48+
);
4349

4450
commit_and_push_changes(
4551
&local_repo_path,
4652
&branch_name,
4753
&String::from("first commit"),
4854
);
4955

50-
create_api_test_file(&local_repo_path);
56+
create_api_test_file(
57+
&local_repo_path,
58+
&String::from("test.txt"),
59+
&String::from("test content"),
60+
);
5161

5262
commit_and_push_changes(
5363
&local_repo_path,
5464
&branch_name,
5565
&String::from("second commit"),
5666
);
5767

58-
checkout_base_branch(&local_repo_path);
68+
checkout_branch(
69+
&local_repo_path,
70+
&String::from("master"),
71+
);
5972

60-
merge_branch_on_base_branch_non_fast_forward(
73+
merge_given_branch_on_current_branch_non_fast_forward(
6174
&local_repo_path,
6275
&branch_name,
63-
&String::from("merge test branch into base branch")
76+
&String::from("merge test branch into base branch"),
6477
);
6578

6679
// Run should-skip-ci and make assertions

tests/utils.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub fn set_remote_repo(
124124

125125
pub fn create_and_push_initial_commit(
126126
repo_path: &PathBuf,
127+
branch_name: &String,
127128
) {
128129
let filename = "README.md";
129130
let apps = vec!["apps/api", "apps/front"];
@@ -176,10 +177,13 @@ pub fn create_and_push_initial_commit(
176177
Command::new("git")
177178
.arg("push")
178179
.arg("origin")
179-
.arg("master")
180+
.arg(&branch_name)
180181
.current_dir(&repo_path)
181182
.output()
182-
.expect("Unable to push master branch to remote.")
183+
.expect(&format!(
184+
"Unable to push {} branch to remote.",
185+
&branch_name,
186+
))
183187
;
184188
}
185189

@@ -203,6 +207,7 @@ pub fn checkout_new_branch(
203207

204208
pub fn append_content_to_api_readme(
205209
repo_path: &PathBuf,
210+
content: &String,
206211
) {
207212
let mut filepath = PathBuf::from(&repo_path);
208213
filepath.push("apps/api");
@@ -214,7 +219,7 @@ pub fn append_content_to_api_readme(
214219
.unwrap()
215220
;
216221

217-
writeln!(file, "\nmore content").unwrap();
222+
writeln!(file, "{}", &content).unwrap();
218223
}
219224

220225
pub fn commit_and_push_changes(
@@ -251,33 +256,39 @@ pub fn commit_and_push_changes(
251256

252257
pub fn create_api_test_file(
253258
repo_path: &PathBuf,
259+
file_name: &String,
260+
content: &String,
254261
) {
255262
let mut filepath = PathBuf::from(&repo_path);
256263
filepath.push("apps/api");
257-
filepath.push("test.txt");
264+
filepath.push(&file_name);
258265

259266
write(
260267
&filepath,
261-
"test content",
268+
&content,
262269
).expect(&format!(
263270
"Unable to write {} file",
264271
&filepath.to_str().unwrap(),
265272
));
266273
}
267274

268-
pub fn checkout_base_branch(
275+
pub fn checkout_branch(
269276
repo_path: &PathBuf,
277+
branch_name: &String,
270278
) {
271279
Command::new("git")
272280
.arg("checkout")
273-
.arg("master")
281+
.arg(&branch_name)
274282
.current_dir(&repo_path)
275283
.output()
276-
.expect("Unable to go on the base branch")
284+
.expect(&format!(
285+
"Unable to go on the {} branch.",
286+
&branch_name,
287+
))
277288
;
278289
}
279290

280-
pub fn merge_branch_on_base_branch_non_fast_forward(
291+
pub fn merge_given_branch_on_current_branch_non_fast_forward(
281292
repo_path: &PathBuf,
282293
branch_name: &String,
283294
message: &String,

0 commit comments

Comments
 (0)