2023-11-21 11:28:59 -08:00
|
|
|
use {super::*, pretty_assertions::assert_eq};
|
2019-12-07 04:03:03 -08:00
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
pub(crate) fn compile(src: &str) -> Justfile {
|
|
|
|
Compiler::test_compile(src).expect("expected successful compilation")
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
pub(crate) fn config(args: &[&str]) -> Config {
|
|
|
|
let mut args = Vec::from(args);
|
|
|
|
args.insert(0, "just");
|
|
|
|
|
|
|
|
let app = Config::app();
|
|
|
|
|
2024-05-14 20:29:40 -07:00
|
|
|
let matches = app.try_get_matches_from(args).unwrap();
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
|
|
|
|
Config::from_matches(&matches).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-12-25 06:12:06 -08:00
|
|
|
pub(crate) fn search(config: &Config) -> Search {
|
|
|
|
let working_directory = config.invocation_directory.clone();
|
2021-07-31 12:25:49 -07:00
|
|
|
let justfile = working_directory.join("justfile");
|
2019-12-25 06:12:06 -08:00
|
|
|
|
|
|
|
Search {
|
|
|
|
justfile,
|
2021-05-07 00:14:38 -07:00
|
|
|
working_directory,
|
2019-12-25 06:12:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:26:59 -07:00
|
|
|
pub(crate) fn tempdir() -> tempfile::TempDir {
|
|
|
|
tempfile::Builder::new()
|
|
|
|
.prefix("just-test-tempdir")
|
|
|
|
.tempdir()
|
|
|
|
.expect("failed to create temporary directory")
|
|
|
|
}
|
2019-07-06 20:55:46 -07:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
macro_rules! analysis_error {
|
2017-11-18 01:44:59 -08:00
|
|
|
(
|
2019-11-07 10:55:15 -08:00
|
|
|
name: $name:ident,
|
|
|
|
input: $input:expr,
|
|
|
|
offset: $offset:expr,
|
|
|
|
line: $line:expr,
|
|
|
|
column: $column:expr,
|
|
|
|
width: $width:expr,
|
|
|
|
kind: $kind:expr,
|
|
|
|
) => {
|
2017-11-18 01:44:59 -08:00
|
|
|
#[test]
|
|
|
|
fn $name() {
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
$crate::testing::analysis_error($input, $offset, $line, $column, $width, $kind);
|
2017-11-18 01:44:59 -08:00
|
|
|
}
|
2018-12-08 14:29:41 -08:00
|
|
|
};
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
pub(crate) fn analysis_error(
|
2019-11-07 10:55:15 -08:00
|
|
|
src: &str,
|
|
|
|
offset: usize,
|
|
|
|
line: usize,
|
|
|
|
column: usize,
|
2019-11-13 19:32:50 -08:00
|
|
|
length: usize,
|
2021-07-26 01:26:06 -07:00
|
|
|
kind: CompileErrorKind,
|
2019-11-07 10:55:15 -08:00
|
|
|
) {
|
2023-11-21 20:17:38 -08:00
|
|
|
let tokens = Lexer::test_lex(src).expect("Lexing failed in parse test...");
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2024-01-11 19:00:38 -08:00
|
|
|
let ast = Parser::parse(
|
2024-05-18 23:38:57 -07:00
|
|
|
0,
|
2024-01-11 19:00:38 -08:00
|
|
|
&PathBuf::new(),
|
2024-05-25 18:10:06 -07:00
|
|
|
&[],
|
2024-01-11 19:00:38 -08:00
|
|
|
&Namepath::default(),
|
|
|
|
0,
|
|
|
|
&tokens,
|
|
|
|
&PathBuf::new(),
|
|
|
|
)
|
|
|
|
.expect("Parsing failed in analysis test...");
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2023-11-21 20:17:38 -08:00
|
|
|
let root = PathBuf::from("justfile");
|
2023-11-21 11:28:59 -08:00
|
|
|
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
|
|
|
|
asts.insert(root.clone(), ast);
|
|
|
|
|
2023-11-22 10:27:49 -08:00
|
|
|
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
|
|
|
|
paths.insert("justfile".into(), "justfile".into());
|
|
|
|
|
2024-06-28 21:13:11 -07:00
|
|
|
match Analyzer::analyze(&asts, None, &[], None, &paths, &root) {
|
2019-11-13 19:32:50 -08:00
|
|
|
Ok(_) => panic!("Analysis unexpectedly succeeded"),
|
|
|
|
Err(have) => {
|
2021-07-26 01:26:06 -07:00
|
|
|
let want = CompileError {
|
2019-11-13 19:32:50 -08:00
|
|
|
token: Token {
|
|
|
|
kind: have.token.kind,
|
|
|
|
src,
|
|
|
|
offset,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
length,
|
2023-11-21 20:17:38 -08:00
|
|
|
path: "justfile".as_ref(),
|
2019-11-13 19:32:50 -08:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
kind: kind.into(),
|
2019-11-13 19:32:50 -08:00
|
|
|
};
|
|
|
|
assert_eq!(have, want);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
macro_rules! run_error {
|
|
|
|
{
|
|
|
|
name: $name:ident,
|
|
|
|
src: $src:expr,
|
|
|
|
args: $args:expr,
|
|
|
|
error: $error:pat,
|
|
|
|
check: $check:block $(,)?
|
|
|
|
} => {
|
|
|
|
#[test]
|
|
|
|
fn $name() {
|
2019-12-25 06:12:06 -08:00
|
|
|
let config = $crate::testing::config(&$args);
|
|
|
|
let search = $crate::testing::search(&config);
|
2019-11-07 10:55:15 -08:00
|
|
|
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
if let Subcommand::Run{ overrides, arguments } = &config.subcommand {
|
2023-11-21 11:28:59 -08:00
|
|
|
match $crate::testing::compile(&$crate::unindent::unindent($src))
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
.run(
|
2019-12-25 06:12:06 -08:00
|
|
|
&config,
|
|
|
|
&search,
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
&overrides,
|
|
|
|
&arguments,
|
|
|
|
).expect_err("Expected runtime error") {
|
|
|
|
$error => $check
|
|
|
|
other => {
|
2023-10-16 20:07:09 -07:00
|
|
|
panic!("Unexpected run error: {other:?}");
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
} else {
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
panic!("Unexpected subcommand: {:?}", config.subcommand);
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
};
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
2020-10-26 18:16:42 -07:00
|
|
|
|
|
|
|
macro_rules! assert_matches {
|
2024-06-13 19:41:45 -07:00
|
|
|
($expression:expr, $( $pattern:pat_param )|+ $( if $guard:expr )? $(,)?) => {
|
2020-10-26 18:16:42 -07:00
|
|
|
match $expression {
|
|
|
|
$( $pattern )|+ $( if $guard )? => {}
|
|
|
|
left => panic!(
|
|
|
|
"assertion failed: (left ~= right)\n left: `{:?}`\n right: `{}`",
|
|
|
|
left,
|
|
|
|
stringify!($($pattern)|+ $(if $guard)?)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|