Skip to content

Commit

Permalink
Merge pull request #11 from KnpLabs/test/develop-and-master-workflow
Browse files Browse the repository at this point in the history
[RFR] Test/develop and master workflow
  • Loading branch information
nm2107 authored Jan 6, 2020
2 parents a56fa16 + 5ec085f commit 8d0916f
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 20 deletions.
16 changes: 13 additions & 3 deletions tests/branch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,30 @@ fn it_should_detect_changes_on_branch() {

set_remote_repo(&local_repo_path, &remote_repo_path);

create_and_push_initial_commit(&local_repo_path);
create_and_push_initial_commit(
&local_repo_path,
&String::from("master"),
);

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

append_content_to_api_readme(&local_repo_path);
append_content_to_api_readme(
&local_repo_path,
&String::from("\nmore content"),
);

commit_and_push_changes(
&local_repo_path,
&branch_name,
&String::from("first commit"),
);

create_api_test_file(&local_repo_path);
create_api_test_file(
&local_repo_path,
&String::from("test.txt"),
&String::from("test content"),
);

commit_and_push_changes(
&local_repo_path,
Expand Down
165 changes: 165 additions & 0 deletions tests/master_and_develop_branches_based_workflow_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
mod utils;

use std::path::{
Path,
PathBuf,
};
use std::fs::read_to_string;
use rand;
use ssc::should_skip_ci;
use utils::{
create_remote_repo,
create_local_repo,
set_remote_repo,
create_and_push_initial_commit,
checkout_new_branch,
append_content_to_api_readme,
commit_and_push_changes,
create_api_test_file,
checkout_branch,
merge_given_branch_on_current_branch_non_fast_forward,
};

/*
This test file is meant to test a `master` and `develop` branches git based
workflow.
In this workflow, the `master` branch is ISO to the prod, and the `develop`
branch contains the latest features.
Developers usually issue new feature branches from the `develop` branch.
The workflow is represented by the following schema :
master ---o-------o
\ /
develop o---o
\ /
feature1 o
---> direction
*/

#[test]
fn it_should_detect_changes_on_master_when_merging_develop_on_master_and_having_develop_as_the_base_branch() {
// Setup test case

let rdm = rand::random::<u32>();
let base_path = PathBuf::from(format!(
"/tmp/ssc-functional-test-{}",
rdm,
));
let remote_repo_path = create_remote_repo(&base_path);
let local_repo_path = create_local_repo(&base_path);

set_remote_repo(&local_repo_path, &remote_repo_path);

create_and_push_initial_commit(
&local_repo_path,
&String::from("master"),
);

let develop_branch_name = String::from("develop");
checkout_new_branch(&local_repo_path, &develop_branch_name);

append_content_to_api_readme(
&local_repo_path,
&String::from("\nmore content"),
);

commit_and_push_changes(
&local_repo_path,
&develop_branch_name,
&String::from("first commit on develop"),
);

let feature_branch_name = String::from("feature/first-one");
checkout_new_branch(&local_repo_path, &feature_branch_name);

create_api_test_file(
&local_repo_path,
&String::from("test.txt"),
&String::from("test content"),
);

commit_and_push_changes(
&local_repo_path,
&feature_branch_name,
&String::from("first commit on feature branch"),
);

checkout_branch(
&local_repo_path,
&develop_branch_name,
);

merge_given_branch_on_current_branch_non_fast_forward(
&local_repo_path,
&feature_branch_name,
&String::from("merge feature branch into develop"),
);

checkout_branch(
&local_repo_path,
&String::from("master"),
);

merge_given_branch_on_current_branch_non_fast_forward(
&local_repo_path,
&develop_branch_name,
&String::from("merge develop branch into master"),
);

// Run should-skip-ci and make assertions

let mut api_app_path = PathBuf::from(&local_repo_path);
api_app_path.push("apps/api");

let mut front_app_path = PathBuf::from(&local_repo_path);
front_app_path.push("apps/front");

let witness_filename = "witness.txt";
let witness_content = "stop command executed";
let mut witness_filepath = PathBuf::from(&base_path);
witness_filepath.push(&witness_filename);

let cmd = format!(
"echo -n \"{}\" > {}",
&witness_content,
&witness_filepath.to_str().unwrap(),
);

// assert that the file created by the stop command is not here before using
// should-skip-ci
assert_eq!(false, Path::new(&witness_filepath).exists());

// should not skip the CI as we made changes on the api app
should_skip_ci(
&local_repo_path,
&vec![api_app_path],
&cmd,
&String::from("origin"),
&develop_branch_name,
);

// the stop command should not have been ran
assert_eq!(false, Path::new(&witness_filepath).exists());

// should skip the CI as we did not make any changes on the front app
should_skip_ci(
&local_repo_path,
&vec![front_app_path],
&cmd,
&String::from("origin"),
&develop_branch_name,
);

// the stop command should have been ran
assert_eq!(true, Path::new(&witness_filepath).exists());

let actual_content = read_to_string(&witness_filepath)
.expect("Unable to read witness file content")
;

assert_eq!(&actual_content, &witness_content);
}
29 changes: 21 additions & 8 deletions tests/merge_commit_on_base_branch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use utils::{
append_content_to_api_readme,
commit_and_push_changes,
create_api_test_file,
checkout_base_branch,
merge_branch_on_base_branch_non_fast_forward,
checkout_branch,
merge_given_branch_on_current_branch_non_fast_forward,
};

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

set_remote_repo(&local_repo_path, &remote_repo_path);

create_and_push_initial_commit(&local_repo_path);
create_and_push_initial_commit(
&local_repo_path,
&String::from("master"),
);

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

append_content_to_api_readme(&local_repo_path);
append_content_to_api_readme(
&local_repo_path,
&String::from("\nmore content"),
);

commit_and_push_changes(
&local_repo_path,
&branch_name,
&String::from("first commit"),
);

create_api_test_file(&local_repo_path);
create_api_test_file(
&local_repo_path,
&String::from("test.txt"),
&String::from("test content"),
);

commit_and_push_changes(
&local_repo_path,
&branch_name,
&String::from("second commit"),
);

checkout_base_branch(&local_repo_path);
checkout_branch(
&local_repo_path,
&String::from("master"),
);

merge_branch_on_base_branch_non_fast_forward(
merge_given_branch_on_current_branch_non_fast_forward(
&local_repo_path,
&branch_name,
&String::from("merge test branch into base branch")
&String::from("merge test branch into base branch"),
);

// Run should-skip-ci and make assertions
Expand Down
29 changes: 20 additions & 9 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub fn set_remote_repo(

pub fn create_and_push_initial_commit(
repo_path: &PathBuf,
branch_name: &String,
) {
let filename = "README.md";
let apps = vec!["apps/api", "apps/front"];
Expand Down Expand Up @@ -176,10 +177,13 @@ pub fn create_and_push_initial_commit(
Command::new("git")
.arg("push")
.arg("origin")
.arg("master")
.arg(&branch_name)
.current_dir(&repo_path)
.output()
.expect("Unable to push master branch to remote.")
.expect(&format!(
"Unable to push {} branch to remote.",
&branch_name,
))
;
}

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

pub fn append_content_to_api_readme(
repo_path: &PathBuf,
content: &String,
) {
let mut filepath = PathBuf::from(&repo_path);
filepath.push("apps/api");
Expand All @@ -214,7 +219,7 @@ pub fn append_content_to_api_readme(
.unwrap()
;

writeln!(file, "\nmore content").unwrap();
writeln!(file, "{}", &content).unwrap();
}

pub fn commit_and_push_changes(
Expand Down Expand Up @@ -251,33 +256,39 @@ pub fn commit_and_push_changes(

pub fn create_api_test_file(
repo_path: &PathBuf,
file_name: &String,
content: &String,
) {
let mut filepath = PathBuf::from(&repo_path);
filepath.push("apps/api");
filepath.push("test.txt");
filepath.push(&file_name);

write(
&filepath,
"test content",
&content,
).expect(&format!(
"Unable to write {} file",
&filepath.to_str().unwrap(),
));
}

pub fn checkout_base_branch(
pub fn checkout_branch(
repo_path: &PathBuf,
branch_name: &String,
) {
Command::new("git")
.arg("checkout")
.arg("master")
.arg(&branch_name)
.current_dir(&repo_path)
.output()
.expect("Unable to go on the base branch")
.expect(&format!(
"Unable to go on the {} branch.",
&branch_name,
))
;
}

pub fn merge_branch_on_base_branch_non_fast_forward(
pub fn merge_given_branch_on_current_branch_non_fast_forward(
repo_path: &PathBuf,
branch_name: &String,
message: &String,
Expand Down

0 comments on commit 8d0916f

Please sign in to comment.