use indoc::{formatdoc, indoc};
use pre_commit_sort::{ConfigHook, DeclareHook, Local, Meta, PreCommitConfig, Remote};
#[test]
fn test_serialize() {
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
"};
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
}
#[test]
fn test_sort() {
let mut example = PreCommitConfig::new();
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["end-of-file-fixer", "check-yaml", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
"};
assert_ne!(yaml_serde::to_string(&example).unwrap(), yaml);
example.sort();
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
}
#[test]
fn test_deserialize() {
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
"};
assert_eq!(example, yaml_serde::from_str(yaml).unwrap());
}
#[test]
fn test_dedup() {
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black.clone());
example.add_remote(black);
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
"};
assert_ne!(yaml_serde::to_string(&example).unwrap(), yaml);
example.sort();
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
}
#[test]
fn test_install() {
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
example.install();
const VERSION: &str = env!("CARGO_PKG_VERSION");
const REPO: &str = env!("CARGO_PKG_REPOSITORY");
const NAME: &str = env!("CARGO_PKG_NAME");
let yaml = formatdoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: {REPO}
rev: v{VERSION}
hooks:
- id: {NAME}
"};
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
}
#[test]
fn test_dedup_rev() {
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"20.1.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
"};
assert_ne!(yaml_serde::to_string(&example).unwrap(), yaml);
example.sort();
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
}
#[test]
fn test_local() {
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: local
hooks:
- id: some-id
name: some-name
entry: some entry
language: rust
"};
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let mut local = Local::new();
let hook = DeclareHook::new(
"some-id".to_string(),
"some-name".to_string(),
"some entry".to_string(),
"rust".to_string(),
);
local.add_hook(hook);
example.add_local(local);
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
assert_eq!(example, yaml_serde::from_str(yaml).unwrap());
}
#[test]
fn test_meta() {
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespaces
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: local
hooks:
- id: some-id
name: some-name
entry: some entry
language: rust
- repo: meta
hooks:
- id: check-useless-excludes
"};
let mut example = PreCommitConfig::new();
let mut pre_commit = Remote::new(
"https://github.com/pre-commit/pre-commit-hooks".to_string(),
"v2.3.0".to_string(),
);
for hook in ["check-yaml", "end-of-file-fixer", "trailing-whitespaces"] {
pre_commit.add_hook(ConfigHook::new(hook.to_string()));
}
example.add_remote(pre_commit);
let mut black = Remote::new(
"https://github.com/psf/black".to_string(),
"22.10.0".to_string(),
);
black.add_hook(ConfigHook::new("black".to_string()));
example.add_remote(black);
let mut local = Local::new();
let hook = DeclareHook::new(
"some-id".to_string(),
"some-name".to_string(),
"some entry".to_string(),
"rust".to_string(),
);
local.add_hook(hook);
example.add_local(local);
let mut meta = Meta::new();
let hook = ConfigHook::new("check-useless-excludes".to_string());
meta.add_hook(hook);
example.add_meta(meta);
assert_eq!(yaml_serde::to_string(&example).unwrap(), yaml);
assert_eq!(example, yaml_serde::from_str(yaml).unwrap());
}
#[test]
fn test_stages_array_parsing_flow() {
let yaml = indoc! {"
repos:
- repo: https://github.com/crate-ci/committed
rev: v1.1.8
hooks:
- id: committed
stages: [commit-msg]
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with stages array (flow): {:?}",
result.err()
);
}
#[test]
fn test_stages_array_parsing_block() {
let yaml = indoc! {"
repos:
- repo: https://github.com/crate-ci/committed
rev: v1.1.8
hooks:
- id: committed
stages:
- commit-msg
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with stages array (block): {:?}",
result.err()
);
}
#[test]
fn test_types_array_parsing_flow() {
let yaml = indoc! {"
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
types: [python]
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with types array (flow): {:?}",
result.err()
);
}
#[test]
fn test_types_array_parsing_block() {
let yaml = indoc! {"
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
types:
- python
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with types array (block): {:?}",
result.err()
);
}
#[test]
fn test_types_or_array_parsing_flow() {
let yaml = indoc! {"
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
types_or: [python, pyi]
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with types_or array (flow): {:?}",
result.err()
);
}
#[test]
fn test_types_or_array_parsing_block() {
let yaml = indoc! {"
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
types_or:
- python
- pyi
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with types_or array (block): {:?}",
result.err()
);
}
#[test]
fn test_exclude_types_array_parsing_flow() {
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude_types: [markdown, rst]
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with exclude_types array (flow): {:?}",
result.err()
);
}
#[test]
fn test_exclude_types_array_parsing_block() {
let yaml = indoc! {"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude_types:
- markdown
- rst
"};
let result: Result<PreCommitConfig, _> = yaml_serde::from_str(yaml);
assert!(
result.is_ok(),
"Failed to parse valid pre-commit config with exclude_types array (block): {:?}",
result.err()
);
}