2022-12-27 20:16:18 -08:00
|
|
|
use {super::*, crate::compiler::Compiler, pretty_assertions::assert_eq};
|
2019-12-07 04:03:03 -08:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) fn compile(text: &str) -> Justfile {
|
|
|
|
match Compiler::compile(text) {
|
2017-11-16 23:30:08 -08:00
|
|
|
Ok(justfile) => justfile,
|
2022-12-15 16:53:21 -08:00
|
|
|
Err(error) => panic!("Expected successful compilation but got error:\n {error}"),
|
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();
|
|
|
|
|
|
|
|
let matches = app.get_matches_from_safe(args).unwrap();
|
|
|
|
|
|
|
|
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
|
|
|
) {
|
|
|
|
let tokens = Lexer::lex(src).expect("Lexing failed in parse test...");
|
|
|
|
|
2021-07-23 20:26:27 -07:00
|
|
|
let ast = Parser::parse(&tokens).expect("Parsing failed in analysis test...");
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2021-07-23 20:26:27 -07:00
|
|
|
match Analyzer::analyze(ast) {
|
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,
|
|
|
|
},
|
2022-09-11 01:41:24 -07:00
|
|
|
kind: Box::new(kind),
|
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 {
|
2021-04-05 21:28:37 -07:00
|
|
|
match $crate::compiler::Compiler::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
|
|
|
.expect("Expected successful compilation")
|
|
|
|
.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 => {
|
|
|
|
panic!("Unexpected run error: {:?}", other);
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-08-08 20:17:38 -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)?)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|