From 00f2057ae488c75b656326c73e6fbda6963c585e Mon Sep 17 00:00:00 2001 From: qwjyh <62229267+qwjyh@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:52:06 +0900 Subject: [PATCH 1/7] Create rust.yml --- .github/workflows/rust.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..31000a2 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,22 @@ +name: Rust + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose From 462c34cf96c21231457129cae9d1fe4c9315b1f0 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Thu, 25 Apr 2024 16:46:59 +0900 Subject: [PATCH 2/7] [fix] test: setup gitconfig at repo init --- tests/cli.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/cli.rs b/tests/cli.rs index f27e1bd..de3bf6e 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,5 +1,8 @@ mod integrated_test { - use std::fs::DirBuilder; + use std::{ + fs::{DirBuilder, File}, + io::{BufWriter, Write}, path::Path, + }; use anyhow::{Ok, Result}; use assert_cmd::{assert::OutputAssertExt, Command}; @@ -7,9 +10,27 @@ mod integrated_test { use log::trace; use predicates::prelude::predicate; + fn setup_gitconfig(dir_path: &Path) -> Result<()> { + DirBuilder::new().create(dir_path.join(".git"))?; + { + let f = File::create(dir_path.join(".git/config"))?; + let mut buf = BufWriter::new(f); + buf.write_all( + r#" + [user] + email = "test@example.com" + name = "testuser" + "# + .as_bytes(), + )?; + } + Ok(()) + } + #[test] fn single_device() -> Result<()> { let config_dir = assert_fs::TempDir::new()?; + setup_gitconfig(&config_dir)?; // init let mut cmd = Command::cargo_bin("xdbm")?; cmd.arg("-c") @@ -103,6 +124,7 @@ mod integrated_test { fn two_devices_with_same_name() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; + setup_gitconfig(&config_dir_1)?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -127,6 +149,7 @@ mod integrated_test { // 2nd device let config_dir_2 = assert_fs::TempDir::new()?; + setup_gitconfig(&config_dir_2)?; let mut cmd2 = Command::cargo_bin("xdbm")?; cmd2.arg("-c") .arg(config_dir_2.path()) @@ -142,6 +165,7 @@ mod integrated_test { fn directory_without_parent() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; + setup_gitconfig(&config_dir_1)?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -174,6 +198,7 @@ mod integrated_test { fn two_devices() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; + setup_gitconfig(&config_dir_1)?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) From 30ae6a6417a6810be70bbc04d2f581c60a5d63ac Mon Sep 17 00:00:00 2001 From: qwjyh Date: Thu, 25 Apr 2024 17:01:34 +0900 Subject: [PATCH 3/7] fixup! [fix] test: setup gitconfig at repo init --- tests/cli.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index de3bf6e..b1b8fe9 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,7 +1,8 @@ mod integrated_test { use std::{ fs::{DirBuilder, File}, - io::{BufWriter, Write}, path::Path, + io::{BufWriter, Write}, + path::Path, }; use anyhow::{Ok, Result}; @@ -17,10 +18,10 @@ mod integrated_test { let mut buf = BufWriter::new(f); buf.write_all( r#" - [user] - email = "test@example.com" - name = "testuser" - "# +[user] + email = "test@example.com" + name = "testuser" +"# .as_bytes(), )?; } From 607013c6499b528db73a68547b7cec06a2c0c01f Mon Sep 17 00:00:00 2001 From: qwjyh Date: Fri, 26 Apr 2024 00:21:42 +0900 Subject: [PATCH 4/7] fixup! [fix] test: setup gitconfig at repo init --- tests/cli.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/cli.rs b/tests/cli.rs index b1b8fe9..f3cd9d1 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -3,6 +3,8 @@ mod integrated_test { fs::{DirBuilder, File}, io::{BufWriter, Write}, path::Path, + thread, + time::Duration, }; use anyhow::{Ok, Result}; @@ -118,6 +120,12 @@ mod integrated_test { .assert() .success(); + let out = std::process::Command::new("git") + .arg("config") + .arg("--get") + .arg("user.email") + .output()?; + panic!("{}", String::from_utf8_lossy(&out.stdout)); Ok(()) } From 4fcb7fc354faefe900725ac48574c83369ae7e03 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Fri, 26 Apr 2024 01:35:42 +0900 Subject: [PATCH 5/7] fixup! [fix] test: setup gitconfig at repo init --- tests/cli.rs | 54 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index f3cd9d1..4b2ff14 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,39 +1,47 @@ mod integrated_test { use std::{ fs::{DirBuilder, File}, - io::{BufWriter, Write}, - path::Path, - thread, - time::Duration, + io::{self, BufWriter, Write}, }; - use anyhow::{Ok, Result}; + use anyhow::{Context, Ok, Result}; use assert_cmd::{assert::OutputAssertExt, Command}; + use dirs::home_dir; use git2::Repository; use log::trace; use predicates::prelude::predicate; - fn setup_gitconfig(dir_path: &Path) -> Result<()> { - DirBuilder::new().create(dir_path.join(".git"))?; - { - let f = File::create(dir_path.join(".git/config"))?; - let mut buf = BufWriter::new(f); - buf.write_all( - r#" + /// Setup global gitconfig if it doesn't exist. + /// + /// # Errors + /// + /// This function will return an error if it failed to get home directory. + fn setup_gitconfig() -> Result<()> { + let f = match File::create_new( + home_dir() + .context("Failed to find home directory")? + .join(".gitconfig"), + ) { + io::Result::Ok(f) => f, + io::Result::Err(_err) => return Ok(()), + }; + let mut buf = BufWriter::new(f); + buf.write_all( + r#" [user] email = "test@example.com" name = "testuser" "# - .as_bytes(), - )?; - } + .as_bytes(), + )?; + Ok(()) } #[test] fn single_device() -> Result<()> { let config_dir = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir)?; + setup_gitconfig()?; // init let mut cmd = Command::cargo_bin("xdbm")?; cmd.arg("-c") @@ -120,12 +128,6 @@ mod integrated_test { .assert() .success(); - let out = std::process::Command::new("git") - .arg("config") - .arg("--get") - .arg("user.email") - .output()?; - panic!("{}", String::from_utf8_lossy(&out.stdout)); Ok(()) } @@ -133,7 +135,7 @@ mod integrated_test { fn two_devices_with_same_name() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -158,7 +160,7 @@ mod integrated_test { // 2nd device let config_dir_2 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_2)?; + setup_gitconfig()?; let mut cmd2 = Command::cargo_bin("xdbm")?; cmd2.arg("-c") .arg(config_dir_2.path()) @@ -174,7 +176,7 @@ mod integrated_test { fn directory_without_parent() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -207,7 +209,7 @@ mod integrated_test { fn two_devices() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) From c9eb34aa69fb07f41b176eaf448557cd41996bfa Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sat, 4 May 2024 14:55:00 +0900 Subject: [PATCH 6/7] fix test to fetch refspecs from the git2 --- tests/cli.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index 4b2ff14..3bf833f 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -151,12 +151,13 @@ mod integrated_test { let upstream_name = "remote"; let mut repo_1_remote = repo_1.remote(upstream_name, bare_repo_dir.path().to_str().unwrap())?; - repo_1_remote.push(&["refs/heads/main"], None)?; + repo_1_remote.push(&[repo_1.head().unwrap().name().unwrap()] as &[&str], None)?; trace!("bare repo {:?}", bare_repo_dir.display()); println!("{:?}", bare_repo_dir.read_dir()?); // set up upstream branch let (mut repo_1_branch, _branch_type) = repo_1.branches(None)?.next().unwrap()?; - repo_1_branch.set_upstream(Some(format!("{}/{}", upstream_name, "main").as_str()))?; + println!("head {}", repo_1.head().unwrap().name().unwrap()); + repo_1_branch.set_upstream(Some(format!("{}/{}", upstream_name, repo_1_branch.name().unwrap().unwrap()).as_str()))?; // 2nd device let config_dir_2 = assert_fs::TempDir::new()?; @@ -225,12 +226,12 @@ mod integrated_test { let upstream_name = "remote"; let mut repo_1_remote = repo_1.remote(upstream_name, bare_repo_dir.path().to_str().unwrap())?; - repo_1_remote.push(&["refs/heads/main"], None)?; + repo_1_remote.push(&[repo_1.head().unwrap().name().unwrap()], None)?; trace!("bare repo {:?}", bare_repo_dir.display()); println!("{:?}", bare_repo_dir.read_dir()?); // set up upstream branch let (mut repo_1_branch, _branch_type) = repo_1.branches(None)?.next().unwrap()?; - repo_1_branch.set_upstream(Some(format!("{}/{}", upstream_name, "main").as_str()))?; + repo_1_branch.set_upstream(Some(format!("{}/{}", upstream_name, repo_1_branch.name().unwrap().unwrap()).as_str()))?; // 2nd device let config_dir_2 = assert_fs::TempDir::new()?; From 51fd4207ea7924df861e8258a05f05d3f092dae5 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sat, 4 May 2024 15:03:02 +0900 Subject: [PATCH 7/7] fixup! fix test to fetch refspecs from the git2 --- tests/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli.rs b/tests/cli.rs index 3bf833f..45bc945 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -151,7 +151,7 @@ mod integrated_test { let upstream_name = "remote"; let mut repo_1_remote = repo_1.remote(upstream_name, bare_repo_dir.path().to_str().unwrap())?; - repo_1_remote.push(&[repo_1.head().unwrap().name().unwrap()] as &[&str], None)?; + repo_1_remote.push(&[repo_1.head().unwrap().name().unwrap()], None)?; trace!("bare repo {:?}", bare_repo_dir.display()); println!("{:?}", bare_repo_dir.read_dir()?); // set up upstream branch