diff --git a/scripts/run_docker_wasm_compare.bash b/scripts/run_docker_wasm_compare.bash new file mode 100755 index 00000000..500b25c1 --- /dev/null +++ b/scripts/run_docker_wasm_compare.bash @@ -0,0 +1,111 @@ +#!/usr/bin/env bash +# +set -euo pipefail +IFS=$'\n\t' +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +: ${SHELL:="NO"} # or YES to launch a shell instead of running the test +: ${TRACE:="NO"} # or YES to send traces to jaeger +: ${BACKTRACE:="NO"} # or YES to print a rust backtrace when panicking +: ${NO_COLOR:=""} # Set to anything to disable color output +: ${PROFILE:="debug"} + +REALPATH=$(command -v uu-realpath || command -v realpath) +MAKE=$(command -v gmake || command -v make) + +############## Setup ######################### + +function die { + local status_code="$1" + shift + (>&2 echo "${@}") + exit "$status_code" +} + +function log { + (>&2 echo "${@}") +} + +############## Program ######################### + +function main { + build_container + launch_container "${@}" +} + +function build_container { + $MAKE -C "$DIR/../docker/organic_test" +} + +function launch_container { + local additional_flags=() + local features=(wasm_test) + + if [ "$NO_COLOR" != "" ]; then + additional_flags+=(--env "NO_COLOR=$NO_COLOR") + fi + + if [ "$TRACE" = "YES" ]; then + # We use the host network so it can talk to jaeger hosted at 127.0.0.1 + additional_flags+=(--network=host --env RUST_LOG=debug) + features+=(tracing) + fi + + if [ "$SHELL" != "YES" ]; then + additional_flags+=(--read-only) + else + additional_flags+=(-t) + fi + + if [ "$BACKTRACE" = "YES" ]; then + additional_flags+=(--env RUST_BACKTRACE=full) + fi + + if [ "$SHELL" = "YES" ]; then + exec docker run "${additional_flags[@]}" --init --rm -i --mount type=tmpfs,destination=/tmp -v "/:/input:ro" -v "$($REALPATH "$DIR/../"):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source --entrypoint "" organic-test /bin/sh + fi + + local features_joined + features_joined=$(IFS=","; echo "${features[*]}") + + local build_flags=() + if [ "$PROFILE" = "dev" ] || [ "$PROFILE" = "debug" ]; then + PROFILE="debug" + else + build_flags+=(--profile "$PROFILE") + fi + + + if [ $# -gt 0 ]; then + # If we passed in args, we need to forward them along + for path in "${@}"; do + local full_path + full_path=$($REALPATH "$path") + init_script=$(cat < Result<(), Box> { } Ok(()) } else { - todo!() - // for arg in args { - // if run_compare_on_file(arg).await? { - // } else { - // Err("Diff results do not match.")?; - // } - // } - // Ok(()) + for arg in args { + if wasm_run_compare_on_file(arg).await? { + } else { + Err("Diff results do not match.")?; + } + } + Ok(()) } } diff --git a/src/wasm_test/macros.rs b/src/wasm_test/macros.rs index 1edfafc1..cf2bf124 100644 --- a/src/wasm_test/macros.rs +++ b/src/wasm_test/macros.rs @@ -32,11 +32,11 @@ macro_rules! wasm_compare { { // Compare children. - result.extend(wasm_compare_list( - $source, - emacs_list_iter, - $wasm.children.iter(), - )?)?; + // result.extend(wasm_compare_list( + // $source, + // emacs_list_iter, + // $wasm.children.iter(), + // )?)?; } { diff --git a/src/wasm_test/mod.rs b/src/wasm_test/mod.rs index 3a5b67da..e2d68a94 100644 --- a/src/wasm_test/mod.rs +++ b/src/wasm_test/mod.rs @@ -5,3 +5,5 @@ mod runner; pub use runner::wasm_run_anonymous_compare; pub use runner::wasm_run_anonymous_compare_with_settings; +pub use runner::wasm_run_compare_on_file; +pub use runner::wasm_run_compare_on_file_with_settings; diff --git a/src/wasm_test/runner.rs b/src/wasm_test/runner.rs index 05c8d2a8..c14f143a 100644 --- a/src/wasm_test/runner.rs +++ b/src/wasm_test/runner.rs @@ -1,8 +1,13 @@ +use std::path::Path; + use super::compare::wasm_compare_document; use crate::compare::sexp; use crate::context::GlobalSettings; +use crate::parser::parse_file_with_settings; use crate::parser::parse_with_settings; +use crate::settings::LocalFileAccessInterface; use crate::util::emacs_parse_anonymous_org_document; +use crate::util::emacs_parse_file_org_document; use crate::util::foreground_color; use crate::util::print_versions; use crate::util::reset_color; @@ -59,3 +64,68 @@ pub async fn wasm_run_anonymous_compare_with_settings<'g, 's, P: AsRef>( Ok(true) } + +//wasm_run_compare_on_file +pub async fn wasm_run_compare_on_file>( + org_path: P, +) -> Result> { + wasm_run_compare_on_file_with_settings(org_path, &GlobalSettings::default(), false).await +} + +pub async fn wasm_run_compare_on_file_with_settings<'g, 's, P: AsRef>( + org_path: P, + global_settings: &GlobalSettings<'g, 's>, + silent: bool, +) -> Result> { + let org_path = org_path.as_ref(); + if !silent { + print_versions().await?; + } + let parent_directory = org_path + .parent() + .ok_or("Should be contained inside a directory.")?; + let org_contents = std::fs::read_to_string(org_path)?; + // TODO: This is a work-around to pretend that dos line endings do not exist. It would be better to handle the difference in line endings. + let org_contents = org_contents.replace("\r\n", "\n"); + let org_contents = org_contents.as_str(); + let file_access_interface = LocalFileAccessInterface { + working_directory: Some(parent_directory.to_path_buf()), + }; + let global_settings = { + let mut global_settings = global_settings.clone(); + global_settings.file_access = &file_access_interface; + global_settings + }; + let rust_parsed = parse_file_with_settings(org_contents, &global_settings, Some(org_path))?; + let to_wasm_context = ToWasmContext::new(org_contents); + let wasm_parsed = rust_parsed + .to_wasm(to_wasm_context) + .map_err(|_e| "Failed to convert to wasm.")?; + let org_sexp = emacs_parse_file_org_document(org_path, &global_settings).await?; + let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).map_err(|e| e.to_string())?; + + if !silent { + println!("{}\n\n\n", org_contents); + println!("{}", org_sexp); + println!("{:#?}", rust_parsed); + println!("{}", serde_json::to_string(&wasm_parsed)?); + } + + // We do the diffing after printing out both parsed forms in case the diffing panics + let diff_result = wasm_compare_document(org_contents, &parsed_sexp, wasm_parsed)?; + if !silent { + diff_result.print(org_contents)?; + } + + if diff_result.is_bad() { + return Ok(false); + } else if !silent { + println!( + "{color}Entire document passes.{reset}", + color = foreground_color(0, 255, 0), + reset = reset_color(), + ); + } + + Ok(true) +}