Skip to content

Commit

Permalink
refactor: refactor test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 12, 2021
1 parent 53f23d9 commit f7c639e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
14 changes: 4 additions & 10 deletions src/conventional/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,13 @@ impl Changelog {
out
}

pub(crate) fn default_header() -> String {
let title = "# Changelog";
let link = "[conventional commits]";
format!(
"{}\nAll notable changes to this project will be documented in this file. \
See {}(https://www.conventionalcommits.org/) for commit guidelines.\n\n- - -\n",
title, link
)
pub(crate) const fn default_header() -> &'static str {
"# Changelog\nAll notable changes to this project will be documented in this file. \
See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.\n\n- - -\n"
}

pub(crate) fn default_footer() -> String {
pub(crate) const fn default_footer() -> &'static str {
"\nThis changelog was generated by [cocogitto](https://github.com/oknozor/cocogitto)."
.to_string()
}

fn changelog_template() -> String {
Expand Down
29 changes: 8 additions & 21 deletions tests/cog_verify_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@ use anyhow::Result;
use assert_cmd::prelude::*;
use std::process::Command;

mod helper;

#[test]
#[cfg(not(tarpaulin))]
fn verify_ok() -> Result<()> {
let message = "chore: a commit message";
let username = Command::new("git")
.arg("config")
.arg("user.name")
.output()?
.stdout;

let username = String::from_utf8(username)?;
let user = username.trim_end();
let username = helper::get_git_user_name()?;

let mut command = Command::cargo_bin("cog")?;
command.arg("verify").arg(message);

command.assert().success().stdout(format!(
r#"a commit message (not committed) - now
Author: {username}
Author: {}
Type: chore
Scope: none
"#,
username = user
username
));

Ok(())
Expand All @@ -35,26 +29,19 @@ fn verify_ok() -> Result<()> {
#[cfg(not(tarpaulin))]
fn verify_with_scope() -> Result<()> {
let message = "feat(feature): a commit message";
let username = Command::new("git")
.arg("config")
.arg("user.name")
.output()?
.stdout;

let username = String::from_utf8(username)?;
let user = username.trim_end();
let username = helper::get_git_user_name()?;

let mut command = Command::cargo_bin("cog")?;
command.arg("verify").arg(message);

command.assert().success().stdout(format!(
r#"a commit message (not committed) - now
Author: {username}
Author: {}
Type: feat
Scope: feature
"#,
username = user
username
));

Ok(())
Expand Down
1 change: 1 addition & 0 deletions tests/git_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use cocogitto::CocoGitto;
use helper::*;
use tempfile::TempDir;

mod helper;

#[test]
Expand Down
37 changes: 21 additions & 16 deletions tests/helper.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
#![allow(dead_code)]

use anyhow::Result;
use cocogitto::CONFIG_PATH;
use rand::Rng;
use std::process::{Command, Stdio};

// Why those are picked as dead code by rustc ?

#[allow(dead_code)]
pub fn git_init(path: &str) -> Result<()> {
Command::new("git")
.arg("init")
.arg(path)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()?;

Ok(())
}

#[allow(dead_code)]
pub fn git_log() -> Result<()> {
Command::new("git")
.arg("log")
.stdout(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.output()?;

Ok(())
}

#[allow(dead_code)]
pub fn git_add() -> Result<()> {
Command::new("git")
.arg("add")
.arg(".")
.stdout(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.output()?;

Ok(())
}

#[allow(dead_code)]
pub fn git_commit(message: &str) -> Result<()> {
let mut rng = rand::thread_rng();
let random: f64 = rng.gen();
Expand All @@ -50,7 +46,7 @@ pub fn git_commit(message: &str) -> Result<()> {
Command::new("git")
.arg("add")
.arg(".")
.stdout(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.output()?;

Expand All @@ -59,24 +55,22 @@ pub fn git_commit(message: &str) -> Result<()> {
.arg("commit")
.arg("-m")
.arg(message)
.stdout(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

#[allow(dead_code)]
pub fn git_tag(tag: &str) -> Result<()> {
Command::new("git")
.arg("tag")
.arg(tag)
.stdout(Stdio::inherit())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.output()?;
Ok(())
}

#[allow(dead_code)]
pub fn assert_tag(tag: &str) -> Result<()> {
let out = Command::new("ls").arg(".git/refs/tags").output()?.stdout;

Expand All @@ -86,7 +80,18 @@ pub fn assert_tag(tag: &str) -> Result<()> {
Ok(())
}

#[allow(dead_code)]
pub fn get_git_user_name() -> Result<String> {
let username = Command::new("git")
.arg("config")
.arg("user.name")
.output()?
.stdout;

Ok(String::from_utf8(username)?
.trim_end()
.to_string())
}

pub fn create_empty_config() -> Result<()> {
std::fs::File::create(CONFIG_PATH)?;
Ok(())
Expand Down

0 comments on commit f7c639e

Please sign in to comment.