From dcb0fd883a223e8dd89846aff10dde405387a7a2 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Mon, 23 Dec 2019 17:04:18 +0100 Subject: [PATCH 1/5] be able to specify branch name to push in tests --- tests/branch_test.rs | 5 ++++- tests/merge_commit_on_base_branch_test.rs | 12 +++++++++--- tests/utils.rs | 18 +++++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/tests/branch_test.rs b/tests/branch_test.rs index d2a6b25..acb2ca7 100644 --- a/tests/branch_test.rs +++ b/tests/branch_test.rs @@ -32,7 +32,10 @@ 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); diff --git a/tests/merge_commit_on_base_branch_test.rs b/tests/merge_commit_on_base_branch_test.rs index a16f227..db344b5 100644 --- a/tests/merge_commit_on_base_branch_test.rs +++ b/tests/merge_commit_on_base_branch_test.rs @@ -16,7 +16,7 @@ use utils::{ append_content_to_api_readme, commit_and_push_changes, create_api_test_file, - checkout_base_branch, + checkout_branch, merge_branch_on_base_branch_non_fast_forward, }; @@ -34,7 +34,10 @@ 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); @@ -55,7 +58,10 @@ fn it_should_detect_changes_on_a_merge_commit_on_master() { &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( &local_repo_path, diff --git a/tests/utils.rs b/tests/utils.rs index a4bf854..124b8cd 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -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"]; @@ -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, + )) ; } @@ -265,15 +269,19 @@ pub fn create_api_test_file( )); } -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, + )) ; } From 1ef4cf224acff56de8101470c96c0c9e14208323 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Mon, 23 Dec 2019 17:17:47 +0100 Subject: [PATCH 2/5] pass test content to append --- tests/branch_test.rs | 5 ++++- tests/merge_commit_on_base_branch_test.rs | 5 ++++- tests/utils.rs | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/branch_test.rs b/tests/branch_test.rs index acb2ca7..c30a10d 100644 --- a/tests/branch_test.rs +++ b/tests/branch_test.rs @@ -40,7 +40,10 @@ fn it_should_detect_changes_on_branch() { 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, diff --git a/tests/merge_commit_on_base_branch_test.rs b/tests/merge_commit_on_base_branch_test.rs index db344b5..f8ae17f 100644 --- a/tests/merge_commit_on_base_branch_test.rs +++ b/tests/merge_commit_on_base_branch_test.rs @@ -42,7 +42,10 @@ fn it_should_detect_changes_on_a_merge_commit_on_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, diff --git a/tests/utils.rs b/tests/utils.rs index 124b8cd..2210a77 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -207,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"); @@ -218,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( From e977b88ba97c473ff148a9662d9a3871d970813d Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Mon, 23 Dec 2019 17:48:42 +0100 Subject: [PATCH 3/5] specify api test filename and content --- tests/branch_test.rs | 6 +++++- tests/merge_commit_on_base_branch_test.rs | 6 +++++- tests/utils.rs | 6 ++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/branch_test.rs b/tests/branch_test.rs index c30a10d..f088d43 100644 --- a/tests/branch_test.rs +++ b/tests/branch_test.rs @@ -51,7 +51,11 @@ fn it_should_detect_changes_on_branch() { &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, diff --git a/tests/merge_commit_on_base_branch_test.rs b/tests/merge_commit_on_base_branch_test.rs index f8ae17f..58291ba 100644 --- a/tests/merge_commit_on_base_branch_test.rs +++ b/tests/merge_commit_on_base_branch_test.rs @@ -53,7 +53,11 @@ fn it_should_detect_changes_on_a_merge_commit_on_master() { &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, diff --git a/tests/utils.rs b/tests/utils.rs index 2210a77..222bd6a 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -256,14 +256,16 @@ 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(), From 3072582ac6597aefba3a440e4385a2a3877cc52a Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Mon, 23 Dec 2019 17:58:04 +0100 Subject: [PATCH 4/5] rename merge non ff util fn --- tests/merge_commit_on_base_branch_test.rs | 6 +++--- tests/utils.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/merge_commit_on_base_branch_test.rs b/tests/merge_commit_on_base_branch_test.rs index 58291ba..0fc6283 100644 --- a/tests/merge_commit_on_base_branch_test.rs +++ b/tests/merge_commit_on_base_branch_test.rs @@ -17,7 +17,7 @@ use utils::{ commit_and_push_changes, create_api_test_file, checkout_branch, - merge_branch_on_base_branch_non_fast_forward, + merge_given_branch_on_current_branch_non_fast_forward, }; #[test] @@ -70,10 +70,10 @@ fn it_should_detect_changes_on_a_merge_commit_on_master() { &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 diff --git a/tests/utils.rs b/tests/utils.rs index 222bd6a..614fffb 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -288,7 +288,7 @@ pub fn checkout_branch( ; } -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, From 5ec085fb3f30a3fb1058b54d28266bafcc61a325 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Mon, 23 Dec 2019 18:04:30 +0100 Subject: [PATCH 5/5] Add a functional test covering a master and develop branches based git workflow 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 ``` --- ...nd_develop_branches_based_workflow_test.rs | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 tests/master_and_develop_branches_based_workflow_test.rs diff --git a/tests/master_and_develop_branches_based_workflow_test.rs b/tests/master_and_develop_branches_based_workflow_test.rs new file mode 100644 index 0000000..199f1b2 --- /dev/null +++ b/tests/master_and_develop_branches_based_workflow_test.rs @@ -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::(); + 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); +}