just/src/justfile.rs

1089 lines
21 KiB
Rust
Raw Normal View History

2022-12-27 20:16:18 -08:00
use {super::*, serde::Serialize};
2021-11-17 00:07:48 -08:00
2023-12-27 20:27:15 -08:00
#[derive(Debug)]
struct Invocation<'src: 'run, 'run> {
arguments: Vec<&'run str>,
module_source: &'run Path,
2023-12-27 20:27:15 -08:00
recipe: &'run Recipe<'src>,
scope: &'run Scope<'src, 'run>,
settings: &'run Settings<'src>,
2023-12-27 20:27:15 -08:00
}
2021-11-17 00:07:48 -08:00
#[derive(Debug, PartialEq, Serialize)]
pub(crate) struct Justfile<'src> {
pub(crate) aliases: Table<'src, Alias<'src>>,
2021-11-17 00:07:48 -08:00
pub(crate) assignments: Table<'src, Assignment<'src>>,
#[serde(rename = "first", serialize_with = "keyed::serialize_option")]
pub(crate) default: Option<Rc<Recipe<'src>>>,
#[serde(skip)]
pub(crate) loaded: Vec<PathBuf>,
pub(crate) modules: Table<'src, Justfile<'src>>,
#[serde(skip)]
pub(crate) name: Option<Name<'src>>,
2021-11-17 00:07:48 -08:00
pub(crate) recipes: Table<'src, Rc<Recipe<'src>>>,
pub(crate) settings: Settings<'src>,
#[serde(skip)]
pub(crate) source: PathBuf,
pub(crate) unexports: HashSet<String>,
pub(crate) warnings: Vec<Warning>,
2017-11-16 23:30:08 -08:00
}
impl<'src> Justfile<'src> {
pub(crate) fn suggest_recipe(&self, input: &str) -> Option<Suggestion<'src>> {
let mut suggestions = self
.recipes
.keys()
.map(|name| {
(
edit_distance(name, input),
Suggestion { name, target: None },
)
})
.chain(self.aliases.iter().map(|(name, alias)| {
(
edit_distance(name, input),
Suggestion {
name,
target: Some(alias.target.name.lexeme()),
},
)
}))
.filter(|(distance, _suggestion)| distance < &3)
.collect::<Vec<(usize, Suggestion)>>();
suggestions.sort_by_key(|(distance, _suggestion)| *distance);
suggestions
.into_iter()
.map(|(_distance, suggestion)| suggestion)
.next()
2017-11-16 23:30:08 -08:00
}
pub(crate) fn suggest_variable(&self, input: &str) -> Option<Suggestion<'src>> {
let mut suggestions = self
.assignments
.keys()
.map(|name| {
(
edit_distance(name, input),
Suggestion { name, target: None },
)
})
.filter(|(distance, _suggestion)| distance < &3)
.collect::<Vec<(usize, Suggestion)>>();
suggestions.sort_by_key(|(distance, _suggestion)| *distance);
suggestions
.into_iter()
.map(|(_distance, suggestion)| suggestion)
.next()
}
pub(crate) fn run(
&self,
config: &Config,
search: &Search,
overrides: &BTreeMap<String, String>,
arguments: &[String],
2023-12-29 12:16:31 -08:00
) -> RunResult<'src> {
let unknown_overrides = overrides
.keys()
.filter(|name| !self.assignments.contains_key(name.as_str()))
.cloned()
.collect::<Vec<String>>();
2017-11-16 23:30:08 -08:00
if !unknown_overrides.is_empty() {
return Err(Error::UnknownOverrides {
overrides: unknown_overrides,
});
2017-11-16 23:30:08 -08:00
}
let dotenv = if config.load_dotenv {
2022-01-30 12:16:10 -08:00
load_dotenv(config, &self.settings, &search.working_directory)?
} else {
BTreeMap::new()
};
2018-03-05 13:21:35 -08:00
2024-05-18 16:12:11 -07:00
let root = Scope::root();
2024-06-08 07:42:16 -07:00
let scope = Evaluator::evaluate_assignments(config, &dotenv, self, overrides, &root, search)?;
2017-11-16 23:30:08 -08:00
2021-05-09 20:35:35 -07:00
match &config.subcommand {
Subcommand::Command {
binary, arguments, ..
} => {
let mut command = if config.shell_command {
2022-01-30 12:16:10 -08:00
let mut command = self.settings.shell_command(config);
2021-05-09 20:35:35 -07:00
command.arg(binary);
command
} else {
2021-05-09 20:35:35 -07:00
Command::new(binary)
};
command.args(arguments);
command.current_dir(&search.working_directory);
let scope = scope.child();
command.export(&self.settings, &dotenv, &scope, &self.unexports);
2021-05-09 20:35:35 -07:00
let status = InterruptHandler::guard(|| command.status()).map_err(|io_error| {
Error::CommandInvoke {
2021-05-09 20:35:35 -07:00
binary: binary.clone(),
arguments: arguments.clone(),
io_error,
}
})?;
if !status.success() {
return Err(Error::CommandStatus {
binary: binary.clone(),
arguments: arguments.clone(),
status,
});
2021-05-09 20:35:35 -07:00
};
return Ok(());
}
2021-05-09 20:35:35 -07:00
Subcommand::Evaluate { variable, .. } => {
if let Some(variable) = variable {
if let Some(value) = scope.value(variable) {
2022-12-15 16:53:21 -08:00
print!("{value}");
2021-05-09 20:35:35 -07:00
} else {
return Err(Error::EvalUnknownVariable {
2022-01-30 12:16:10 -08:00
suggestion: self.suggest_variable(variable),
variable: variable.clone(),
2021-05-09 20:35:35 -07:00
});
}
} else {
2024-06-08 07:42:16 -07:00
let width = scope.names().fold(0, |max, name| name.len().max(max));
2021-05-09 20:35:35 -07:00
for binding in scope.bindings() {
println!(
"{0:1$} := \"{2}\"",
binding.name.lexeme(),
width,
binding.value
);
}
}
2021-05-09 20:35:35 -07:00
return Ok(());
}
_ => {}
2017-11-16 23:30:08 -08:00
}
let arguments = arguments.iter().map(String::as_str).collect::<Vec<&str>>();
let groups = ArgumentParser::parse_arguments(self, &arguments)?;
let arena: Arena<Scope> = Arena::new();
let mut invocations = Vec::<Invocation>::new();
let mut scopes = BTreeMap::new();
for group in &groups {
invocations.push(self.invocation(
2023-12-27 20:27:15 -08:00
&arena,
&group.arguments,
2023-12-27 20:27:15 -08:00
config,
&dotenv,
&scope,
&group.path,
0,
&mut scopes,
search,
)?);
2017-11-16 23:30:08 -08:00
}
let mut ran = Ran::default();
2023-12-27 20:27:15 -08:00
for invocation in invocations {
2024-06-08 07:42:16 -07:00
let context = ExecutionContext {
2023-12-27 20:27:15 -08:00
config,
dotenv: &dotenv,
module_source: invocation.module_source,
2023-12-27 20:27:15 -08:00
scope: invocation.scope,
search,
settings: invocation.settings,
unexports: &self.unexports,
2023-12-27 20:27:15 -08:00
};
Self::run_recipe(
&invocation
.arguments
.iter()
.copied()
.map(str::to_string)
.collect::<Vec<String>>(),
&context,
2023-12-27 20:27:15 -08:00
&mut ran,
invocation.recipe,
2024-06-08 18:17:55 -07:00
false,
2023-12-27 20:27:15 -08:00
)?;
2017-11-16 23:30:08 -08:00
}
Ok(())
}
pub(crate) fn get_alias(&self, name: &str) -> Option<&Alias<'src>> {
self.aliases.get(name)
}
pub(crate) fn get_recipe(&self, name: &str) -> Option<&Recipe<'src>> {
self
.recipes
.get(name)
.map(Rc::as_ref)
.or_else(|| self.aliases.get(name).map(|alias| alias.target.as_ref()))
}
2023-12-27 20:27:15 -08:00
fn invocation<'run>(
&'run self,
arena: &'run Arena<Scope<'src, 'run>>,
arguments: &[&'run str],
2023-12-27 20:27:15 -08:00
config: &'run Config,
dotenv: &'run BTreeMap<String, String>,
parent: &'run Scope<'src, 'run>,
path: &'run [String],
position: usize,
scopes: &mut BTreeMap<&'run [String], &'run Scope<'src, 'run>>,
search: &'run Search,
) -> RunResult<'src, Invocation<'src, 'run>> {
if position + 1 == path.len() {
let recipe = self.get_recipe(&path[position]).unwrap();
Ok(Invocation {
recipe,
module_source: &self.source,
arguments: arguments.into(),
settings: &self.settings,
scope: parent,
})
} else {
let module = self.modules.get(&path[position]).unwrap();
2023-12-27 20:27:15 -08:00
let scope = if let Some(scope) = scopes.get(&path[..position]) {
2023-12-27 20:27:15 -08:00
scope
} else {
2024-06-08 07:42:16 -07:00
let scope = Evaluator::evaluate_assignments(
config,
dotenv,
module,
&BTreeMap::new(),
parent,
search,
)?;
2023-12-27 20:27:15 -08:00
let scope = arena.alloc(scope);
scopes.insert(path, scope);
2023-12-27 20:27:15 -08:00
scopes.get(path).unwrap()
};
module.invocation(
arena,
arguments,
config,
dotenv,
scope,
path,
position + 1,
scopes,
search,
)
2023-12-27 20:27:15 -08:00
}
}
pub(crate) fn name(&self) -> &'src str {
self.name.map(|name| name.lexeme()).unwrap_or_default()
}
fn run_recipe(
arguments: &[String],
2024-06-08 07:42:16 -07:00
context: &ExecutionContext<'src, '_>,
ran: &mut Ran<'src>,
recipe: &Recipe<'src>,
2024-06-08 18:17:55 -07:00
is_dependency: bool,
2023-12-29 12:16:31 -08:00
) -> RunResult<'src> {
if ran.has_run(&recipe.namepath, arguments) {
return Ok(());
}
2023-11-16 15:51:34 -08:00
if !context.config.yes && !recipe.confirm()? {
return Err(Error::NotConfirmed {
recipe: recipe.name(),
});
}
2024-06-08 07:42:16 -07:00
let (outer, positional) =
2024-06-08 18:17:55 -07:00
Evaluator::evaluate_parameters(context, is_dependency, arguments, &recipe.parameters)?;
let scope = outer.child();
2024-06-08 18:17:55 -07:00
let mut evaluator = Evaluator::new(context, true, &scope);
if !context.config.no_dependencies {
for Dependency { recipe, arguments } in recipe.dependencies.iter().take(recipe.priors) {
let arguments = arguments
.iter()
.map(|argument| evaluator.evaluate_expression(argument))
.collect::<RunResult<Vec<String>>>()?;
2024-06-08 18:17:55 -07:00
Self::run_recipe(&arguments, context, ran, recipe, true)?;
}
2017-11-16 23:30:08 -08:00
}
2024-06-08 18:17:55 -07:00
recipe.run(context, &scope, &positional, is_dependency)?;
if !context.config.no_dependencies {
let mut ran = Ran::default();
for Dependency { recipe, arguments } in recipe.dependencies.iter().skip(recipe.priors) {
let mut evaluated = Vec::new();
for argument in arguments {
evaluated.push(evaluator.evaluate_expression(argument)?);
}
2024-06-08 18:17:55 -07:00
Self::run_recipe(&evaluated, context, &mut ran, recipe, true)?;
}
}
ran.ran(&recipe.namepath, arguments.to_vec());
2017-11-16 23:30:08 -08:00
Ok(())
}
pub(crate) fn modules(&self, config: &Config) -> Vec<&Justfile> {
let mut modules = self.modules.values().collect::<Vec<&Justfile>>();
if config.unsorted {
modules.sort_by_key(|module| {
module
.name
.map(|name| name.token.offset)
.unwrap_or_default()
});
}
modules
}
pub(crate) fn public_recipes(&self, config: &Config) -> Vec<&Recipe<'src, Dependency>> {
let mut recipes = self
.recipes
.values()
.map(AsRef::as_ref)
.filter(|recipe| recipe.is_public())
.collect::<Vec<&Recipe<Dependency>>>();
if config.unsorted {
recipes.sort_by_key(|recipe| (&recipe.import_offsets, recipe.name.offset));
}
recipes
}
2024-05-25 00:32:25 -07:00
pub(crate) fn public_groups(&self) -> BTreeSet<String> {
self
.recipes
.values()
.map(AsRef::as_ref)
.filter(|recipe| recipe.is_public())
.flat_map(Recipe::groups)
.collect()
}
2017-11-16 23:30:08 -08:00
}
impl<'src> ColorDisplay for Justfile<'src> {
fn fmt(&self, f: &mut Formatter, color: Color) -> Result<(), fmt::Error> {
let mut items = self.recipes.len() + self.assignments.len() + self.aliases.len();
for (name, assignment) in &self.assignments {
if assignment.export {
2017-11-16 23:30:08 -08:00
write!(f, "export ")?;
}
2022-12-15 16:53:21 -08:00
write!(f, "{name} := {}", assignment.value)?;
2017-11-16 23:30:08 -08:00
items -= 1;
if items != 0 {
write!(f, "\n\n")?;
}
}
for alias in self.aliases.values() {
2022-12-15 16:53:21 -08:00
write!(f, "{alias}")?;
items -= 1;
if items != 0 {
write!(f, "\n\n")?;
}
}
2017-11-16 23:30:08 -08:00
for recipe in self.recipes.values() {
write!(f, "{}", recipe.color_display(color))?;
2017-11-16 23:30:08 -08:00
items -= 1;
if items != 0 {
write!(f, "\n\n")?;
}
}
Ok(())
}
}
impl<'src> Keyed<'src> for Justfile<'src> {
fn key(&self) -> &'src str {
self.name()
}
}
2017-11-16 23:30:08 -08:00
#[cfg(test)]
mod tests {
2017-11-16 23:30:08 -08:00
use super::*;
use testing::compile;
use Error::*;
run_error! {
name: unknown_recipe_no_suggestion,
src: "a:\nb:\nc:",
args: ["a", "xyz", "y", "z"],
error: UnknownRecipe {
recipe,
suggestion,
},
check: {
assert_eq!(recipe, "xyz");
assert_eq!(suggestion, None);
2017-11-16 23:30:08 -08:00
}
}
run_error! {
name: unknown_recipe_with_suggestion,
src: "a:\nb:\nc:",
args: ["a", "x", "y", "z"],
error: UnknownRecipe {
recipe,
suggestion,
},
check: {
assert_eq!(recipe, "x");
assert_eq!(suggestion, Some(Suggestion {
name: "a",
target: None,
}));
}
}
run_error! {
name: unknown_recipe_show_alias_suggestion,
src: "
foo:
echo foo
alias z := foo
",
args: ["zz"],
error: UnknownRecipe {
recipe,
suggestion,
},
check: {
assert_eq!(recipe, "zz");
assert_eq!(suggestion, Some(Suggestion {
name: "z",
target: Some("foo"),
}
));
}
}
run_error! {
name: code_error,
src: "
fail:
@exit 100
",
args: ["fail"],
error: Code {
recipe,
line_number,
code,
print_message,
},
check: {
assert_eq!(recipe, "fail");
assert_eq!(code, 100);
assert_eq!(line_number, Some(2));
assert!(print_message);
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: run_args,
src: r#"
a return code:
@x() { {{return}} {{code + "0"}}; }; x
"#,
args: ["a", "return", "15"],
error: Code {
recipe,
line_number,
code,
print_message,
},
check: {
assert_eq!(recipe, "a");
assert_eq!(code, 150);
assert_eq!(line_number, Some(2));
assert!(print_message);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: missing_some_arguments,
src: "a b c d:",
args: ["a", "b", "c"],
error: ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
},
check: {
let param_names = parameters
.iter()
.map(|p| p.name.lexeme())
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 2);
assert_eq!(min, 3);
assert_eq!(max, 3);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: missing_some_arguments_variadic,
src: "a b c +d:",
args: ["a", "B", "C"],
error: ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
},
check: {
let param_names = parameters
.iter()
.map(|p| p.name.lexeme())
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 2);
assert_eq!(min, 3);
assert_eq!(max, usize::MAX - 1);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: missing_all_arguments,
src: "a b c d:\n echo {{b}}{{c}}{{d}}",
args: ["a"],
error: ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
},
check: {
let param_names = parameters
.iter()
.map(|p| p.name.lexeme())
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 0);
assert_eq!(min, 3);
assert_eq!(max, 3);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: missing_some_defaults,
src: "a b c d='hello':",
args: ["a", "b"],
error: ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
},
check: {
let param_names = parameters
.iter()
.map(|p| p.name.lexeme())
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 1);
assert_eq!(min, 2);
assert_eq!(max, 3);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: missing_all_defaults,
src: "a b c='r' d='h':",
args: ["a"],
error: ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
},
check: {
let param_names = parameters
.iter()
.map(|p| p.name.lexeme())
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 0);
assert_eq!(min, 1);
assert_eq!(max, 3);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: unknown_overrides,
src: "
a:
echo {{`f() { return 100; }; f`}}
",
args: ["foo=bar", "baz=bob", "a"],
error: UnknownOverrides { overrides },
check: {
assert_eq!(overrides, &["baz", "foo"]);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
run_error! {
name: export_failure,
src: r#"
export foo := "a"
baz := "c"
export bar := "b"
export abc := foo + bar + baz
wut:
echo $foo $bar $baz
"#,
args: ["--quiet", "wut"],
error: Code {
recipe,
line_number,
print_message,
..
},
check: {
assert_eq!(recipe, "wut");
assert_eq!(line_number, Some(7));
assert!(print_message);
2017-11-17 17:28:06 -08:00
}
2017-11-16 23:30:08 -08:00
}
2023-11-16 13:51:57 -08:00
fn case(input: &str, expected: &str) {
let justfile = compile(input);
let actual = format!("{}", justfile.color_display(Color::never()));
assert_eq!(actual, expected);
println!("Re-parsing...");
let reparsed = compile(&actual);
let redumped = format!("{}", reparsed.color_display(Color::never()));
assert_eq!(redumped, actual);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_empty() {
case(
"
# hello
",
2023-11-16 13:51:57 -08:00
"",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_string_default() {
case(
r#"
foo a="b\t":
"#,
2023-11-16 13:51:57 -08:00
r#"foo a="b\t":"#,
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_multiple() {
case(
r"
a:
b:
2023-11-16 13:51:57 -08:00
", r"a:
2023-11-16 13:51:57 -08:00
b:",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_variadic() {
case(
r"
foo +a:
2023-11-16 13:51:57 -08:00
",
r"foo +a:",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_variadic_string_default() {
case(
r#"
foo +a="Hello":
"#,
2023-11-16 13:51:57 -08:00
r#"foo +a="Hello":"#,
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_raw_string_default() {
case(
r"
foo a='b\t':
2023-10-08 19:34:05 -07:00
",
2023-11-16 13:51:57 -08:00
r"foo a='b\t':",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_export() {
case(
r#"
export a := "hello"
"#,
2023-11-16 13:51:57 -08:00
r#"export a := "hello""#,
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_alias_after_target() {
case(
r"
foo:
echo a
alias f := foo
2023-11-16 13:51:57 -08:00
",
r"alias f := foo
foo:
2023-11-16 13:51:57 -08:00
echo a",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_alias_before_target() {
case(
r"
alias f := foo
foo:
echo a
2023-11-16 13:51:57 -08:00
",
r"alias f := foo
foo:
2023-11-16 13:51:57 -08:00
echo a",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_alias_with_comment() {
case(
r"
alias f := foo #comment
foo:
echo a
2023-11-16 13:51:57 -08:00
",
r"alias f := foo
foo:
2023-11-16 13:51:57 -08:00
echo a",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_complex() {
case(
"
x:
y:
z:
foo := \"xx\"
bar := foo
goodbye := \"y\"
hello a b c : x y z #hello
#! blah
#blarg
{{ foo + bar}}abc{{ goodbye\t + \"x\" }}xyz
1
2
3
",
2023-11-16 13:51:57 -08:00
"bar := foo
foo := \"xx\"
goodbye := \"y\"
hello a b c: x y z
#! blah
#blarg
2021-06-08 01:01:27 -07:00
{{ foo + bar }}abc{{ goodbye + \"x\" }}xyz
1
2
3
x:
y:
2023-11-16 13:51:57 -08:00
z:",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_shebang() {
case(
"
practicum := 'hello'
install:
\t#!/bin/sh
\tif [[ -f {{practicum}} ]]; then
\t\treturn
\tfi
",
2023-11-16 13:51:57 -08:00
"practicum := 'hello'
install:
#!/bin/sh
2021-06-08 01:01:27 -07:00
if [[ -f {{ practicum }} ]]; then
\treturn
fi",
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_simple_shebang() {
case("a:\n #!\n print(1)", "a:\n #!\n print(1)");
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_assignments() {
case(
r#"a := "0"
c := a + b + a + b
b := "1"
"#,
2023-11-16 13:51:57 -08:00
r#"a := "0"
b := "1"
c := a + b + a + b"#,
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_assignment_backticks() {
case(
"a := `echo hello`
c := a + b + a + b
b := `echo goodbye`",
2023-11-16 13:51:57 -08:00
"a := `echo hello`
b := `echo goodbye`
c := a + b + a + b",
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parse_interpolation_backticks() {
case(
r#"a:
echo {{ `echo hello` + "blarg" }} {{ `echo bob` }}"#,
2023-11-16 13:51:57 -08:00
r#"a:
2021-06-08 01:01:27 -07:00
echo {{ `echo hello` + "blarg" }} {{ `echo bob` }}"#,
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn eof_test() {
case("x:\ny:\nz:\na b c: x y z", "a b c: x y z\n\nx:\n\ny:\n\nz:");
}
2023-11-16 13:51:57 -08:00
#[test]
fn string_quote_escape() {
case(r#"a := "hello\"""#, r#"a := "hello\"""#);
}
2023-11-16 13:51:57 -08:00
#[test]
fn string_escapes() {
case(r#"a := "\n\t\r\"\\""#, r#"a := "\n\t\r\"\\""#);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameters() {
case(
"a b c:
{{b}} {{c}}",
2023-11-16 13:51:57 -08:00
"a b c:
2021-06-08 01:01:27 -07:00
{{ b }} {{ c }}",
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn unary_functions() {
case(
"
x := arch()
a:
2023-08-02 16:52:21 -07:00
{{os()}} {{os_family()}} {{num_cpus()}}",
2023-11-16 13:51:57 -08:00
"x := arch()
a:
2023-08-02 16:52:21 -07:00
{{ os() }} {{ os_family() }} {{ num_cpus() }}",
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn env_functions() {
case(
r#"
x := env_var('foo',)
a:
{{env_var_or_default('foo' + 'bar', 'baz',)}} {{env_var(env_var("baz"))}}"#,
2023-11-16 13:51:57 -08:00
r#"x := env_var('foo')
a:
2021-06-08 01:01:27 -07:00
{{ env_var_or_default('foo' + 'bar', 'baz') }} {{ env_var(env_var("baz")) }}"#,
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_string() {
case(
r#"
f x="abc":
"#,
2023-11-16 13:51:57 -08:00
r#"f x="abc":"#,
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_raw_string() {
case(
r"
f x='abc':
2023-11-16 13:51:57 -08:00
",
r"f x='abc':",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_backtick() {
case(
r"
f x=`echo hello`:
2023-11-16 13:51:57 -08:00
",
r"f x=`echo hello`:",
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_concatenation_string() {
case(
r#"
f x=(`echo hello` + "foo"):
"#,
2023-11-16 13:51:57 -08:00
r#"f x=(`echo hello` + "foo"):"#,
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_concatenation_variable() {
case(
r#"
x := "10"
f y=(`echo hello` + x) +z="foo":
"#,
2023-11-16 13:51:57 -08:00
r#"x := "10"
f y=(`echo hello` + x) +z="foo":"#,
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn parameter_default_multiple() {
case(
r#"
x := "10"
f y=(`echo hello` + x) +z=("foo" + "bar"):
"#,
2023-11-16 13:51:57 -08:00
r#"x := "10"
f y=(`echo hello` + x) +z=("foo" + "bar"):"#,
2023-11-16 13:51:57 -08:00
);
}
2023-11-16 13:51:57 -08:00
#[test]
fn concatenation_in_group() {
case("x := ('0' + '1')", "x := ('0' + '1')");
}
2023-11-16 13:51:57 -08:00
#[test]
fn string_in_group() {
case("x := ('0' )", "x := ('0')");
}
#[rustfmt::skip]
2023-11-16 13:51:57 -08:00
#[test]
fn escaped_dos_newlines() {
case("@spam:\r
\t{ \\\r
\t\tfiglet test; \\\r
\t\tcargo build --color always 2>&1; \\\r
\t\tcargo test --color always -- --color always 2>&1; \\\r
\t} | less\r
",
"@spam:
{ \\
\tfiglet test; \\
\tcargo build --color always 2>&1; \\
\tcargo test --color always -- --color always 2>&1; \\
2023-11-16 13:51:57 -08:00
} | less");
}
2017-11-16 23:30:08 -08:00
}