about summary refs log tree commit diff
path: root/tvix/cli/src/main.rs
diff options
context:
space:
mode:
authorAspen Smith <root@gws.fyi>2024-07-06T13·00-0400
committerclbot <clbot@tvl.fyi>2024-07-07T14·19+0000
commit0ad986169d06922945d035aab8d82266e798bffc (patch)
tree683e4633d8fee9636aa14bf5c329281e0b3733a9 /tvix/cli/src/main.rs
parent3a79f937951d34c7293bb093e4c21ddc3c2d88fc (diff)
test(tvix/cli): Make the REPL testable r/8353
Juggle around the internals of the tvix-cli crate so that we expose the
Repl as a public type with a `send` method, that sends a string to the
repl and *captures all output* so that it can be subsequently asserted
on in tests. Then, demonstrate that this works with a single (for now)
REPL test using expect-test to assert on the output of a single command
sent to the REPL.

As the REPL gets more complicated, this will allow us to make tests that
cover that complex behavior.

Change-Id: I88175bd72d8760c79faade95ebb1d956f08a7b83
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11958
Autosubmit: aspen <root@gws.fyi>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/cli/src/main.rs')
-rw-r--r--tvix/cli/src/main.rs268
1 files changed, 8 insertions, 260 deletions
diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs
index 2f7dc58a23aa..0bd3be37eeb2 100644
--- a/tvix/cli/src/main.rs
+++ b/tvix/cli/src/main.rs
@@ -1,23 +1,11 @@
-mod assignment;
-mod repl;
-
 use clap::Parser;
-use repl::Repl;
-use smol_str::SmolStr;
-use std::collections::HashMap;
 use std::rc::Rc;
 use std::{fs, path::PathBuf};
-use tracing::{instrument, Level, Span};
-use tracing_indicatif::span_ext::IndicatifSpanExt;
-use tvix_build::buildservice;
-use tvix_eval::builtins::impure_builtins;
-use tvix_eval::observer::{DisassemblingObserver, TracingObserver};
-use tvix_eval::{ErrorKind, EvalIO, Value};
-use tvix_glue::builtins::add_fetcher_builtins;
-use tvix_glue::builtins::add_import_builtins;
-use tvix_glue::tvix_io::TvixIO;
+use tvix_cli::args::Args;
+use tvix_cli::repl::Repl;
+use tvix_cli::{init_io_handle, interpret, AllowIncomplete};
+use tvix_eval::observer::DisassemblingObserver;
 use tvix_glue::tvix_store_io::TvixStoreIO;
-use tvix_glue::{builtins::add_derivation_builtins, configure_nix_path};
 
 #[cfg(not(target_env = "msvc"))]
 use tikv_jemallocator::Jemalloc;
@@ -26,240 +14,6 @@ use tikv_jemallocator::Jemalloc;
 #[global_allocator]
 static GLOBAL: Jemalloc = Jemalloc;
 
-#[derive(Parser, Clone)]
-struct Args {
-    /// A global log level to use when printing logs.
-    /// It's also possible to set `RUST_LOG` according to
-    /// `tracing_subscriber::filter::EnvFilter`, which will always have
-    /// priority.
-    #[arg(long, default_value_t=Level::INFO)]
-    log_level: Level,
-
-    /// Path to a script to evaluate
-    script: Option<PathBuf>,
-
-    #[clap(long, short = 'E')]
-    expr: Option<String>,
-
-    /// Dump the raw AST to stdout before interpreting
-    #[clap(long, env = "TVIX_DISPLAY_AST")]
-    display_ast: bool,
-
-    /// Dump the bytecode to stdout before evaluating
-    #[clap(long, env = "TVIX_DUMP_BYTECODE")]
-    dump_bytecode: bool,
-
-    /// Trace the runtime of the VM
-    #[clap(long, env = "TVIX_TRACE_RUNTIME")]
-    trace_runtime: bool,
-
-    /// Capture the time (relative to the start time of evaluation) of all events traced with
-    /// `--trace-runtime`
-    #[clap(long, env = "TVIX_TRACE_RUNTIME_TIMING", requires("trace_runtime"))]
-    trace_runtime_timing: bool,
-
-    /// Only compile, but do not execute code. This will make Tvix act
-    /// sort of like a linter.
-    #[clap(long)]
-    compile_only: bool,
-
-    /// Don't print warnings.
-    #[clap(long)]
-    no_warnings: bool,
-
-    /// A colon-separated list of directories to use to resolve `<...>`-style paths
-    #[clap(long, short = 'I', env = "NIX_PATH")]
-    nix_search_path: Option<String>,
-
-    /// Print "raw" (unquoted) output.
-    #[clap(long)]
-    raw: bool,
-
-    /// Strictly evaluate values, traversing them and forcing e.g.
-    /// elements of lists and attribute sets before printing the
-    /// return value.
-    #[clap(long)]
-    strict: bool,
-
-    #[arg(long, env, default_value = "memory://")]
-    blob_service_addr: String,
-
-    #[arg(long, env, default_value = "memory://")]
-    directory_service_addr: String,
-
-    #[arg(long, env, default_value = "memory://")]
-    path_info_service_addr: String,
-
-    #[arg(long, env, default_value = "dummy://")]
-    build_service_addr: String,
-}
-
-fn init_io_handle(tokio_runtime: &tokio::runtime::Runtime, args: &Args) -> Rc<TvixStoreIO> {
-    let (blob_service, directory_service, path_info_service, nar_calculation_service) =
-        tokio_runtime
-            .block_on({
-                let blob_service_addr = args.blob_service_addr.clone();
-                let directory_service_addr = args.directory_service_addr.clone();
-                let path_info_service_addr = args.path_info_service_addr.clone();
-                async move {
-                    tvix_store::utils::construct_services(
-                        blob_service_addr,
-                        directory_service_addr,
-                        path_info_service_addr,
-                    )
-                    .await
-                }
-            })
-            .expect("unable to setup {blob|directory|pathinfo}service before interpreter setup");
-
-    let build_service = tokio_runtime
-        .block_on({
-            let blob_service = blob_service.clone();
-            let directory_service = directory_service.clone();
-            async move {
-                buildservice::from_addr(
-                    &args.build_service_addr,
-                    blob_service.clone(),
-                    directory_service.clone(),
-                )
-                .await
-            }
-        })
-        .expect("unable to setup buildservice before interpreter setup");
-
-    Rc::new(TvixStoreIO::new(
-        blob_service.clone(),
-        directory_service.clone(),
-        path_info_service.into(),
-        nar_calculation_service.into(),
-        build_service.into(),
-        tokio_runtime.handle().clone(),
-    ))
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
-enum AllowIncomplete {
-    Allow,
-    #[default]
-    RequireComplete,
-}
-
-impl AllowIncomplete {
-    fn allow(&self) -> bool {
-        matches!(self, Self::Allow)
-    }
-}
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-struct IncompleteInput;
-
-/// Interprets the given code snippet, printing out warnings and errors and returning the result
-fn evaluate(
-    tvix_store_io: Rc<TvixStoreIO>,
-    code: &str,
-    path: Option<PathBuf>,
-    args: &Args,
-    allow_incomplete: AllowIncomplete,
-    env: Option<&HashMap<SmolStr, Value>>,
-) -> Result<Option<Value>, IncompleteInput> {
-    let span = Span::current();
-    span.pb_start();
-    span.pb_set_style(&tvix_tracing::PB_SPINNER_STYLE);
-    span.pb_set_message("Setting up evaluator…");
-
-    let mut eval_builder = tvix_eval::Evaluation::builder(Box::new(TvixIO::new(
-        tvix_store_io.clone() as Rc<dyn EvalIO>,
-    )) as Box<dyn EvalIO>)
-    .enable_import()
-    .with_strict(args.strict)
-    .add_builtins(impure_builtins())
-    .env(env);
-
-    eval_builder = add_derivation_builtins(eval_builder, Rc::clone(&tvix_store_io));
-    eval_builder = add_fetcher_builtins(eval_builder, Rc::clone(&tvix_store_io));
-    eval_builder = add_import_builtins(eval_builder, tvix_store_io);
-    eval_builder = configure_nix_path(eval_builder, &args.nix_search_path);
-
-    let source_map = eval_builder.source_map().clone();
-    let result = {
-        let mut compiler_observer =
-            DisassemblingObserver::new(source_map.clone(), std::io::stderr());
-        if args.dump_bytecode {
-            eval_builder.set_compiler_observer(Some(&mut compiler_observer));
-        }
-
-        let mut runtime_observer = TracingObserver::new(std::io::stderr());
-        if args.trace_runtime {
-            if args.trace_runtime_timing {
-                runtime_observer.enable_timing()
-            }
-            eval_builder.set_runtime_observer(Some(&mut runtime_observer));
-        }
-
-        span.pb_set_message("Evaluating…");
-
-        let eval = eval_builder.build();
-        eval.evaluate(code, path)
-    };
-
-    if allow_incomplete.allow()
-        && result.errors.iter().any(|err| {
-            matches!(
-                &err.kind,
-                ErrorKind::ParseErrors(pes)
-                    if pes.iter().any(|pe| matches!(pe, rnix::parser::ParseError::UnexpectedEOF))
-            )
-        })
-    {
-        return Err(IncompleteInput);
-    }
-
-    if args.display_ast {
-        if let Some(ref expr) = result.expr {
-            eprintln!("AST: {}", tvix_eval::pretty_print_expr(expr));
-        }
-    }
-
-    for error in &result.errors {
-        error.fancy_format_stderr();
-    }
-
-    if !args.no_warnings {
-        for warning in &result.warnings {
-            warning.fancy_format_stderr(&source_map);
-        }
-    }
-
-    Ok(result.value)
-}
-
-/// Interprets the given code snippet, printing out warnings, errors
-/// and the result itself. The return value indicates whether
-/// evaluation succeeded.
-#[instrument(skip_all, fields(indicatif.pb_show=1))]
-fn interpret(
-    tvix_store_io: Rc<TvixStoreIO>,
-    code: &str,
-    path: Option<PathBuf>,
-    args: &Args,
-    explain: bool,
-    allow_incomplete: AllowIncomplete,
-    env: Option<&HashMap<SmolStr, Value>>,
-) -> Result<bool, IncompleteInput> {
-    let result = evaluate(tvix_store_io, code, path, args, allow_incomplete, env)?;
-
-    if let Some(value) = result.as_ref() {
-        if explain {
-            println!("=> {}", value.explain());
-        } else {
-            println_result(value, args.raw);
-        }
-    }
-
-    // inform the caller about any errors
-    Ok(result.is_some())
-}
-
 /// Interpret the given code snippet, but only run the Tvix compiler
 /// on it and return errors and warnings.
 fn lint(code: &str, path: Option<PathBuf>, args: &Args) -> bool {
@@ -323,12 +77,13 @@ fn main() {
             None, // TODO(aspen): Pass in --arg/--argstr here
         )
         .unwrap()
+        .finalize()
         {
             std::process::exit(1);
         }
     } else {
-        let mut repl = Repl::new();
-        repl.run(io_handle, &args)
+        let mut repl = Repl::new(io_handle, &args);
+        repl.run()
     }
 }
 
@@ -351,17 +106,10 @@ fn run_file(io_handle: Rc<TvixStoreIO>, mut path: PathBuf, args: &Args) {
             None,
         )
         .unwrap()
+        .finalize()
     };
 
     if !success {
         std::process::exit(1);
     }
 }
-
-fn println_result(result: &Value, raw: bool) {
-    if raw {
-        println!("{}", result.to_contextful_str().unwrap())
-    } else {
-        println!("=> {} :: {}", result, result.type_of())
-    }
-}