use proptest::prelude::*;
use proptest::test_runner::RngSeed;
use super::*;
const DEFAULT_SEED: u64 = 0x0001_f600;
const NOISE: &[&str] = &[
"a",
"Z",
"0",
" ",
"\t",
"\u{e9}",
"e\u{301}",
"\u{3042}",
"\u{65e5}",
"\u{1f600}",
"\u{1d49c}",
"\u{10400}",
"\u{a0}",
"\u{3000}",
"\u{feff}",
"\u{2028}",
"\u{2029}",
"\u{1680}",
"\u{202f}",
"\u{200d}",
"\"",
"[",
"]",
",",
"=",
":",
"-",
];
const KEYWORDS: &[&str] = &[
"RUN ",
"CMD ",
"ENTRYPOINT ",
"run ",
"Cmd ",
"R",
"RU",
"RUN",
"C",
"CM",
"CMD",
"ENTRY",
"ENTRYPOIN",
"ENTRYPOINT",
"",
];
const INDENT: &[&str] = &[" ", "\t", "\u{a0}", "\u{3000}", "\u{2028}", "\u{1680}"];
const NAME_CHARS: &[&str] = &["a", "b", "z", "\u{e9}", "\u{65e5}", "\u{1d49c}", "\u{3042}"];
fn config() -> ProptestConfig {
let mut config = ProptestConfig {
failure_persistence: None,
..ProptestConfig::default()
};
if matches!(config.rng_seed, RngSeed::Random) {
config.rng_seed = RngSeed::Fixed(DEFAULT_SEED);
}
config
}
fn pick(set: &'static [&'static str]) -> impl Strategy<Value = &'static str> {
prop::sample::select(set)
}
fn joined(set: &'static [&'static str], len: std::ops::Range<usize>) -> BoxedStrategy<String> {
prop::collection::vec(pick(set), len)
.prop_map(|parts| parts.concat())
.boxed()
}
fn noise_line() -> BoxedStrategy<String> {
(joined(INDENT, 0..3), pick(KEYWORDS), joined(NOISE, 0..12))
.prop_map(|(indent, keyword, rest)| format!("{indent}{keyword}{rest}"))
.boxed()
}
fn planted_file() -> BoxedStrategy<String> {
joined(NAME_CHARS, 1..5)
.prop_map(|name| format!("scripts/{name}.ts"))
.boxed()
}
#[derive(Debug, Clone)]
struct Line {
text: String,
planted: Option<String>,
}
fn dockerfile_line() -> BoxedStrategy<Line> {
prop_oneof![
3 => noise_line().prop_map(|text| Line { text, planted: None }),
1 => (joined(INDENT, 0..3), prop::sample::select(&["RUN", "CMD", "ENTRYPOINT"][..]), planted_file())
.prop_map(|(indent, keyword, file)| Line {
text: format!("{indent}{keyword} node {file}"),
planted: Some(file),
}),
1 => (prop::sample::select(&["CMD", "ENTRYPOINT"][..]), planted_file())
.prop_map(|(keyword, file)| Line {
text: format!("{keyword} [\"node\", \"{file}\"]"),
planted: Some(file),
}),
]
.boxed()
}
fn procfile_line() -> BoxedStrategy<Line> {
prop_oneof![
3 => noise_line().prop_map(|text| Line { text, planted: None }),
1 => (joined(NAME_CHARS, 1..4), joined(INDENT, 0..3), planted_file())
.prop_map(|(process, indent, file)| Line {
text: format!("{indent}{process}: node {file}"),
planted: Some(file),
}),
]
.boxed()
}
fn fly_toml_line() -> BoxedStrategy<Line> {
prop_oneof![
3 => noise_line().prop_map(|text| Line { text, planted: None }),
1 => (prop::sample::select(&["release_command", "cmd"][..]), joined(INDENT, 0..3), planted_file())
.prop_map(|(key, indent, file)| Line {
text: format!("{indent}{key} = \"node {file}\""),
planted: Some(file),
}),
]
.boxed()
}
fn expected_refs(lines: &[Line]) -> Vec<&str> {
lines
.iter()
.filter_map(|line| line.planted.as_deref())
.collect()
}
fn content(lines: &[Line]) -> String {
lines
.iter()
.map(|line| line.text.as_str())
.collect::<Vec<_>>()
.join("\n")
}
fn check(lines: &[Line], refs: &[String]) -> Result<(), TestCaseError> {
for expected in expected_refs(lines) {
prop_assert!(
refs.iter().any(|found| found == expected),
"the parser lost the planted reference {expected:?}; found {refs:?}"
);
}
Ok(())
}
fn dockerfile_refs(content: &str) -> Vec<String> {
extract_dockerfile_file_refs(content, CommandRefContext::NONE)
}
fn procfile_refs(content: &str) -> Vec<String> {
extract_procfile_file_refs(content, CommandRefContext::NONE)
}
fn fly_toml_refs(content: &str) -> Vec<String> {
extract_fly_toml_file_refs(content, CommandRefContext::NONE)
}
proptest! {
#![proptest_config(config())]
#[test]
fn dockerfile_parser_keeps_planted_refs(lines in prop::collection::vec(dockerfile_line(), 1..8)) {
check(&lines, &dockerfile_refs(&content(&lines)))?;
}
#[test]
fn procfile_parser_keeps_planted_refs(lines in prop::collection::vec(procfile_line(), 1..8)) {
check(&lines, &procfile_refs(&content(&lines)))?;
}
#[test]
fn fly_toml_parser_keeps_planted_refs(lines in prop::collection::vec(fly_toml_line(), 1..8)) {
check(&lines, &fly_toml_refs(&content(&lines)))?;
}
}
#[test]
fn dockerfile_keyword_cut_inside_a_character() {
for line in [
"RU\u{3042}",
"CM\u{e9}",
"ENTRYPOIN\u{1f600}",
"\u{3042}\u{3044}",
] {
assert!(dockerfile_refs(line).is_empty(), "{line:?}");
}
}