This repository has been archived on 2021-03-14. You can view files and clone it, but cannot push or open issues or pull requests.
git-config/tests/integration_tests/parser_integration_tests.rs

229 lines
5.3 KiB
Rust
Raw Normal View History

2021-02-21 11:43:41 -08:00
use std::borrow::Cow;
2021-02-24 09:11:18 -08:00
use git_config::parser::{parse_from_str, Event, ParsedSectionHeader};
2021-02-18 21:12:59 -08:00
2021-02-23 16:47:24 -08:00
pub fn section_header_event(
2021-02-19 16:18:02 -08:00
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> Event<'_> {
2021-02-23 16:47:24 -08:00
Event::SectionHeader(section_header(name, subsection))
}
pub fn section_header(
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> ParsedSectionHeader<'_> {
2021-03-03 17:43:00 -08:00
let name = Cow::Borrowed(name);
2021-02-23 16:47:24 -08:00
if let Some((separator, subsection_name)) = subsection.into() {
ParsedSectionHeader {
name,
2021-03-03 17:43:00 -08:00
separator: Some(Cow::Borrowed(separator)),
subsection_name: Some(Cow::Borrowed(subsection_name)),
2021-02-23 16:47:24 -08:00
}
} else {
ParsedSectionHeader {
name,
separator: None,
subsection_name: None,
}
}
2021-02-18 21:12:59 -08:00
}
2021-02-23 16:47:24 -08:00
2021-02-18 21:12:59 -08:00
fn name(name: &'static str) -> Event<'static> {
2021-03-03 17:43:00 -08:00
Event::Key(Cow::Borrowed(name))
2021-02-18 21:12:59 -08:00
}
fn value(value: &'static str) -> Event<'static> {
2021-02-27 19:18:44 -08:00
Event::Value(Cow::Borrowed(value.as_bytes()))
2021-02-18 21:12:59 -08:00
}
2021-02-19 16:18:02 -08:00
fn newline() -> Event<'static> {
2021-02-23 16:47:24 -08:00
newline_custom("\n")
}
fn newline_custom(value: &'static str) -> Event<'static> {
2021-03-03 17:43:00 -08:00
Event::Newline(Cow::Borrowed(value))
2021-02-19 16:18:02 -08:00
}
fn whitespace(value: &'static str) -> Event<'static> {
2021-03-03 17:43:00 -08:00
Event::Whitespace(Cow::Borrowed(value))
2021-02-19 16:18:02 -08:00
}
2021-02-23 08:30:48 -08:00
fn separator() -> Event<'static> {
Event::KeyValueSeparator
}
2021-02-18 21:12:59 -08:00
#[test]
2021-02-19 16:18:02 -08:00
#[rustfmt::skip]
2021-02-18 21:12:59 -08:00
fn personal_config() {
let config = r#"[user]
email = code@eddie.sh
2021-02-23 08:30:48 -08:00
name = Foo Bar
2021-02-18 21:12:59 -08:00
[core]
autocrlf = input
[push]
default = simple
[commit]
gpgsign = true
[gpg]
program = gpg
[url "ssh://git@github.com/"]
insteadOf = "github://"
[url "ssh://git@git.eddie.sh/edward/"]
insteadOf = "gitea://"
[pull]
ff = only
[init]
defaultBranch = master"#;
assert_eq!(
parse_from_str(config)
.unwrap()
.into_vec(),
2021-02-18 21:12:59 -08:00
vec![
2021-02-23 16:47:24 -08:00
section_header_event("user", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("email"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("code@eddie.sh"),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("name"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
value("Foo Bar"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("core", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("autocrlf"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("input"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("push", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("default"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("simple"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("commit", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("gpgsign"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("true"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("gpg", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("program"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("gpg"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("url", (" ", "ssh://git@github.com/")),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("insteadOf"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-19 09:08:42 -08:00
value("\"github://\""),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("url", (" ", "ssh://git@git.eddie.sh/edward/")),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("insteadOf"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-19 09:08:42 -08:00
value("\"gitea://\""),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("pull", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("ff"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("only"),
2021-02-19 16:18:02 -08:00
newline(),
2021-02-23 16:47:24 -08:00
section_header_event("init", None),
2021-02-19 16:18:02 -08:00
newline(),
whitespace(" "),
2021-02-18 21:12:59 -08:00
name("defaultBranch"),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-23 08:30:48 -08:00
separator(),
2021-02-19 16:18:02 -08:00
whitespace(" "),
2021-02-18 21:12:59 -08:00
value("master"),
]
);
}
#[test]
fn parse_empty() {
assert_eq!(parse_from_str("").unwrap().into_vec(), vec![]);
}
2021-02-21 18:16:08 -08:00
#[test]
fn parse_whitespace() {
assert_eq!(
parse_from_str("\n \n \n").unwrap().into_vec(),
vec![
newline(),
whitespace(" "),
newline(),
whitespace(" "),
newline(),
]
)
}
#[test]
fn newline_events_are_merged() {
assert_eq!(
parse_from_str("\n\n\n\n\n").unwrap().into_vec(),
2021-02-23 16:47:24 -08:00
vec![newline_custom("\n\n\n\n\n")]
2021-02-21 18:16:08 -08:00
);
}
2021-02-24 20:19:45 -08:00
#[test]
fn error() {
let input = "[core] a=b\n 4a=3";
println!("{}", parse_from_str(input).unwrap_err());
2021-02-25 08:11:26 -08:00
let input = "[core] a=b\n =3";
println!("{}", parse_from_str(input).unwrap_err());
let input = "[core";
println!("{}", parse_from_str(input).unwrap_err());
2021-02-24 20:19:45 -08:00
}