Skip to content

Commit

Permalink
Error when overriding private variables
Browse files Browse the repository at this point in the history
  • Loading branch information
adsnaider committed Aug 4, 2024
1 parent 7124399 commit 01b254d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
15 changes: 7 additions & 8 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ impl<'src, 'run> Evaluator<'src, 'run> {

for (name, value) in overrides {
if let Some(assignment) = module.assignments.get(name) {
scope.bind(
assignment.export,
assignment.is_private(),
assignment.name,
value.clone(),
);
if assignment.is_public() {
scope.bind(assignment.export, false, assignment.name, value.clone());
} else {
unknown_overrides.push(name.clone());
}
} else {
unknown_overrides.push(name.clone());
}
Expand Down Expand Up @@ -70,7 +69,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
let value = self.evaluate_expression(&assignment.value)?;
self.scope.bind(
assignment.export,
assignment.is_private(),
assignment.private,
assignment.name,
value,
);
Expand Down Expand Up @@ -331,7 +330,7 @@ impl<'src, 'run> Evaluator<'src, 'run> {
};
evaluator
.scope
.bind(parameter.export, false, parameter.name, value);
.bind(parameter.export, true, parameter.name, value);
}

Ok((evaluator.scope, positional))
Expand Down
11 changes: 8 additions & 3 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Subcommand {
List { path } => Self::list(config, justfile, path)?,
Show { path } => Self::show(config, justfile, path)?,
Summary => Self::summary(config, justfile),
Variables => Self::variables(justfile),
Variables => Self::public_variables(justfile),
Changelog | Completions { .. } | Edit | Init | Man | Run { .. } => unreachable!(),
}

Expand Down Expand Up @@ -713,8 +713,13 @@ impl Subcommand {
}
}

fn variables(justfile: &Justfile) {
for (i, (_, assignment)) in justfile.assignments.iter().enumerate() {
fn public_variables(justfile: &Justfile) {
for (i, (_, assignment)) in justfile
.assignments
.iter()
.filter(|(_, binding)| binding.is_public())
.enumerate()
{
if i > 0 {
print!(" ");
}
Expand Down
26 changes: 26 additions & 0 deletions tests/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@ test! {
",
status: EXIT_FAILURE,
}

test! {
name: evaluate_private,
justfile: "
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
",
args: ("--evaluate"),
stdout: "_baz := \"three\"\nbar := \"two\"\nfoo := \"one\"\n",
status: EXIT_SUCCESS,
}

test! {
name: evaluate_single_private,
justfile: "
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
",
args: ("--evaluate", "foo"),
stdout: "one",
status: EXIT_SUCCESS,
}
67 changes: 67 additions & 0 deletions tests/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,70 @@ fn private_attribute_for_alias() {
)
.run();
}

#[test]
fn private_attribute_for_assignment() {
Test::new()
.justfile(
"
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
",
)
.args(["--variables"])
.stdout("bar\n")
.run();
}

test! {
name: no_private_overrides,
justfile: "
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
default:
@echo hello
",
args: ("foo=two"),
stdout: "",
stderr: "error: Variable `foo` overridden on the command line but not present in justfile\n",
status: EXIT_FAILURE,
}

test! {
name: no_private_implicit_overrides,
justfile: "
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
default:
@echo hello
",
args: ("_baz=two"),
stdout: "",
stderr: "error: Variable `_baz` overridden on the command line but not present in justfile\n",
status: EXIT_FAILURE,
}

test! {
name: allowed_public_overrides,
justfile: "
[private]
foo := 'one'
bar := 'two'
_baz := 'three'
default:
@echo hello
",
args: ("bar=two"),
stdout: "hello\n",
stderr: "",
status: EXIT_SUCCESS,
}

0 comments on commit 01b254d

Please sign in to comment.