Skip to content

Commit

Permalink
Add two helper functions to TestContext (#84)
Browse files Browse the repository at this point in the history
* Add two helper functions to TestContext

* fix
  • Loading branch information
j178 authored Nov 20, 2024
1 parent b9d9279 commit 08dc389
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 164 deletions.
21 changes: 20 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#![allow(dead_code, unreachable_pub)]

use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;

use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::{ChildPath, PathChild};
use assert_fs::fixture::{ChildPath, FileWriteStr, PathChild};
use etcetera::BaseStrategy;

pub struct TestContext {
Expand Down Expand Up @@ -227,6 +228,24 @@ impl TestContext {
.assert()
.success();
}

/// Run `git add` in the temporary directory.
pub fn git_add(&self, path: impl AsRef<OsStr>) {
Command::new("git")
.arg("add")
.arg(path)
.current_dir(&self.temp_dir)
.assert()
.success();
}

/// Write a `.pre-commit-config.yaml` file in the temporary directory.
pub fn write_pre_commit_config(&self, content: &str) {
self.temp_dir
.child(".pre-commit-config.yaml")
.write_str(content)
.expect("Failed to write pre-commit config");
}
}

#[doc(hidden)] // Macro and test context only, don't use directly.
Expand Down
24 changes: 6 additions & 18 deletions tests/hook_impl.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::fixture::{FileWriteStr, PathChild};
use std::process::Command;

use common::TestContext;
use indoc::indoc;
use std::process::Command;

use crate::common::cmd_snapshot;

mod common;

#[test]
fn hook_impl() -> Result<()> {
fn hook_impl() {
let context = TestContext::new();

context.init_project();

context
.workdir()
.child(".pre-commit-config.yaml")
.write_str(indoc! { r"
context.write_pre_commit_config(indoc! { r"
repos:
- repo: local
hooks:
Expand All @@ -28,14 +23,9 @@ fn hook_impl() -> Result<()> {
entry: always fail
always_run: true
"
})?;
});

Command::new("git")
.arg("add")
.current_dir(context.workdir())
.arg(".")
.assert()
.success();
context.git_add(".");

let mut commit = Command::new("git");
commit
Expand Down Expand Up @@ -66,6 +56,4 @@ fn hook_impl() -> Result<()> {
.pre-commit-config.yaml
"#);

Ok(())
}
18 changes: 6 additions & 12 deletions tests/language.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::common::{cmd_snapshot, TestContext};
use anyhow::Result;
use assert_cmd::Command;
use assert_fs::prelude::*;

use crate::common::{cmd_snapshot, TestContext};

mod common;

#[test]
Expand All @@ -15,8 +15,7 @@ fn fail() -> Result<()> {
cwd.child("changelog").create_dir_all()?;
cwd.child("changelog/changelog.md").touch()?;

cwd.child(".pre-commit-config.yaml")
.write_str(indoc::indoc! {r"
context.write_pre_commit_config(indoc::indoc! {r"
repos:
- repo: local
hooks:
Expand All @@ -25,14 +24,9 @@ fn fail() -> Result<()> {
entry: changelog filenames must end in .rst
language: fail
files: 'changelog/.*(?<!\.rst)$'
"})?;

Command::new("git")
.current_dir(cwd)
.arg("add")
.arg(".")
.assert()
.success();
"});

context.git_add(".");

cmd_snapshot!(context.filters(), context.run(), @r#"
success: false
Expand Down
Loading

0 comments on commit 08dc389

Please sign in to comment.