feat: add rust-script

This commit is contained in:
2022-08-06 01:14:04 +08:00
parent 70f202e982
commit a65b8f0aae
67 changed files with 5086 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
use std::env;
pub fn main() {
// Test that CARGO_TARGET_DIR is not set by rust-script to avoid
// interfering with cargo calls done by the script.
// See https://github.com/fornwall/rust-script/issues/27
let env_variable = env::var("CARGO_TARGET_DIR");
println!("--output--");
println!(
"{:?}",
matches!(env_variable, Err(env::VarError::NotPresent))
);
}

View File

@@ -0,0 +1 @@
hello, including script

View File

@@ -0,0 +1,10 @@
/// ```cargo
/// [dependencies]
/// boolinator = "=0.1.0"
/// ```
use boolinator::Boolinator;
pub fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,10 @@
//! ```cargo
//! [dependencies]
//! boolinator = "=0.1.0"
//! ```
use boolinator::Boolinator;
pub fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,3 @@
use std::fs::File;
File::open("__rust-script-this-file-does-not-exist.txt")?;

View File

@@ -0,0 +1,4 @@
println!("--output--");
if let Some(arg) = std::env::args().skip(1).next() {
println!("Argument: {}", arg);
}

View File

@@ -0,0 +1,6 @@
fn main() {
println!("--output--");
for (i, arg) in std::env::args().enumerate() {
println!("{:>4}: {:?}", format!("[{}]", i), arg);
}
}

View File

@@ -0,0 +1,14 @@
//! This is merged into a default manifest in order to form the full package manifest:
//!
//! ```cargo
//! [dependencies]
//! boolinator = "=0.1.0"
//! tokio = { version = "1", features = ["full"] }
//! ```
use boolinator::Boolinator;
#[tokio::main]
async fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,12 @@
use std::env;
fn main() {
println!("--output--");
let path = env::var("RUST_SCRIPT_PATH").expect("CSSP wasn't set");
assert!(path.ends_with("script-cs-env.rs"));
assert_eq!(env::var("RUST_SCRIPT_SAFE_NAME"), Ok("script-cs-env".into()));
assert_eq!(env::var("RUST_SCRIPT_PKG_NAME"), Ok("script-cs-env".into()));
let base_path = env::var("RUST_SCRIPT_BASE_PATH").expect("CSBP wasn't set");
assert!(base_path.ends_with("data"));
println!("Ok");
}

View File

@@ -0,0 +1,6 @@
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,16 @@
/*!
```cargo
[features]
dont-panic = []
```
*/
#[cfg(feature="dont-panic")]
fn main() {
println!("--output--");
println!("Keep calm and borrow check.");
}
#[cfg(not(feature="dont-panic"))]
fn main() {
panic!("Do I really exist from an external, non-subjective point of view?");
}

View File

@@ -0,0 +1,11 @@
//! This is merged into a default manifest in order to form the full package manifest:
//!
//! ```cargo
//! [dependencies]
//! boolinator = "=0.1.0"
//! ```
use boolinator::Boolinator;
fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env rust-script
/// This is merged into a default manifest in order to form the full package manifest:
///
/// ```cargo
/// [dependencies]
/// boolinator = "=0.1.0"
/// ```
use boolinator::Boolinator;
println!("--output--");
println!("{:?}", true.as_some(1));

View File

@@ -0,0 +1,13 @@
/*!
This is merged into a default manifest in order to form the full package manifest:
```cargo
[dependencies]
boolinator = "=0.1.0"
```
*/
use boolinator::Boolinator;
fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,4 @@
fn main() {
println!("--output--");
println!("Ok");
}

View File

@@ -0,0 +1,12 @@
use std::env;
mod script_module {
include!(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/script-module.rs"));
}
fn main() {
println!("--output--");
let s = include_str!(concat!(env!("RUST_SCRIPT_BASE_PATH"), "/file-to-be-included.txt"));
assert_eq!(script_module::A_VALUE, 1);
println!("{}", s);
}

View File

@@ -0,0 +1,12 @@
fn main() {
println!("--output--");
println!("Hello, World!");
}
/**
```cargo
[dependencies]
i-cant-decide-whether-you-should = ["live", "die"]
```
*/
fn dummy() {}

View File

@@ -0,0 +1 @@
pub const A_VALUE: i32 = 1;

View File

@@ -0,0 +1,4 @@
fn main() {
println!("--output--");
println!("Hello, World!");
}

View File

@@ -0,0 +1,9 @@
// cargo-deps: boolinator="=0.1.0"
// You can also leave off the version number, in which case, it's assumed
// to be "*". Also, the `cargo-deps` comment *must* be a single-line
// comment, and it *must* be the first thing in the file, after the
// shebang.
use boolinator::Boolinator;
println!("--output--");
println!("{:?}", true.as_some(1));

View File

@@ -0,0 +1,10 @@
// cargo-deps: boolinator="=0.1.0"
// You can also leave off the version number, in which case, it's assumed
// to be "*". Also, the `cargo-deps` comment *must* be a single-line
// comment, and it *must* be the first thing in the file, after the
// shebang.
use boolinator::Boolinator;
fn main() {
println!("--output--");
println!("{:?}", true.as_some(1));
}

View File

@@ -0,0 +1,10 @@
/*!
```cargo
[dependencies]
slow-build = { version = "0.1.0", path = "slow-build" }
```
*/
fn main() {
println!("--output--");
println!("Ok");
}

View File

@@ -0,0 +1,2 @@
#[test]
fn test() {}

View File

@@ -0,0 +1,6 @@
#![feature(lang_items)]
fn main() {
println!("--output--");
println!("`#![feature]` *may* be used!");
}

View File

@@ -0,0 +1 @@
/Cargo.lock

View File

@@ -0,0 +1,10 @@
[package]
name = "slow-build"
version = "0.1.0"
authors = ["Daniel Keep <daniel.keep@gmail.com>"]
build = "slow-build-build.rs"
[lib]
name = "slow_build"
path = "slow-build-lib.rs"

View File

@@ -0,0 +1,5 @@
fn main() {
println!("Sleeping for 2 seconds...");
std::thread::sleep(std::time::Duration::from_millis(2000));
println!("Done.");
}

View File

@@ -0,0 +1,9 @@
// cargo-deps: boolinator="0.1.0"
#{prelude}
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("{:?}", Boolinator::as_option({#{script}}));
}

View File

@@ -0,0 +1,9 @@
// cargo-deps: boolinator="0.1.0"
#{prelude}
extern crate boolinator;
use boolinator::Boolinator;
fn main() {
println!("{:?}", Boolinator::as_option({#{script}}));
}

View File

@@ -0,0 +1,11 @@
#{prelude}
fn main() {
match {#{script}} {
script_result => {
let text = script_result.to_string();
let text = text.to_uppercase();
println!("{}", text);
}
}
}

View File

@@ -0,0 +1,6 @@
// cargo-deps: chrono
extern crate chrono;
fn main() {
println!("--output--");
println!("Hello");
}

View File

@@ -0,0 +1,4 @@
fn main() {
println!("--output--");
println!("hello, world");
}

View File

@@ -0,0 +1,19 @@
/*!
# Why is this here?
Because *both* Cargo and Rust both know better than I do and won't let me tell them to stop running the tests in parallel. This is a problem because they do not, in fact, know better than me: Cargo doesn't do *any* locking, which causes random failures as two tests try to update the registry simultaneously (quite *why* Cargo needs to update the registry so fucking often I have no damn idea).
*All* integration tests have to be glommed into a single runner so that we can use locks to prevent Cargo from falling over and breaking both its legs as soon as a gentle breeze comes along. I *would* do this "properly" using file locks, except that's apparently impossible in Rust without writing the whole stack yourself directly on native OS calls, and I just can't be arsed to go to *that* much effort just to get some bloody tests to work.
*/
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate scan_rules;
#[macro_use]
mod util;
mod tests {
mod expr;
mod others;
mod script;
}

View File

@@ -0,0 +1,4 @@
hello
2
3
4

View File

@@ -0,0 +1,7 @@
#!/bin/sh
set -e -u
rust-script -e 'println!("hello");'
rust-script -e '1+1'
rust-script -e '1+2'
rust-script -e '1+3'

View File

@@ -0,0 +1,6 @@
First:
1: line1
2: line2
Second:
1: line1
2: line2

View File

@@ -0,0 +1,10 @@
#!/bin/sh
set -e -u
echo "First:"
echo "line1\nline2" | rust-script --loop \
"let mut n=0; move |l| {n+=1; println!(\"{:>6}: {}\",n,l.trim_right())}"
echo "Second:"
echo "line1\nline2" | rust-script --count --loop \
"|l,n| println!(\"{:>6}: {}\", n, l.trim_right())"

View File

@@ -0,0 +1,40 @@
#!/bin/bash
set -e -u
ANY_ERROR=0
# Make sure newly built binary is first in PATH:
cargo build &> /dev/null || {
echo "ERROR: Compilation failed"
exit 1
}
export PATH=$PWD/target/debug/:$PATH
cd tests/scripts
for TEST_SCRIPT in *.script; do
EXPECTED_STDOUT=${TEST_SCRIPT/.script/.expected}
ACTUAL_STDOUT=${TEST_SCRIPT/.script/.actual-stdout}
ACTUAL_STDERR=${TEST_SCRIPT/.script/.actual-stderr}
echo -n "Running $TEST_SCRIPT ... "
./$TEST_SCRIPT > $ACTUAL_STDOUT 2> $ACTUAL_STDERR || {
ANY_ERROR=1
echo "Failed to run!"
}
if cmp -s "$EXPECTED_STDOUT" "$ACTUAL_STDOUT"; then
echo "Ok"
else
ANY_ERROR=1
echo "Failed!"
echo "######################## Expected:"
cat $EXPECTED_STDOUT
echo "######################## Actual:"
cat $ACTUAL_STDOUT
echo "######################## Error output:"
cat $ACTUAL_STDERR
echo "########################"
fi
done
exit $ANY_ERROR

112
external/rust-script/tests/tests/expr.rs vendored Normal file
View File

@@ -0,0 +1,112 @@
#[test]
fn test_expr_0() {
let out = rust_script!("-e", with_output_marker!("0")).unwrap();
scan!(out.stdout_output();
("0") => ()
)
.unwrap()
}
#[test]
fn test_expr_comma() {
let out = rust_script!("-e", with_output_marker!("[1, 2, 3]")).unwrap();
scan!(out.stdout_output();
("[1, 2, 3]") => ()
)
.unwrap()
}
#[test]
fn test_expr_dnc() {
let out = rust_script!("-e", "swing begin").unwrap();
assert!(!out.success());
}
#[test]
fn test_expr_temporary() {
let out = rust_script!("-e", "[1].iter().max()").unwrap();
assert!(out.success());
}
#[test]
fn test_expr_dep() {
let out = rust_script!(
"-d",
"boolinator=0.1.0",
"-e",
with_output_marker!(
prelude "use boolinator::Boolinator;";
"true.as_some(1)"
)
)
.unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap();
}
#[test]
fn test_expr_panic() {
let out = rust_script!("-e", with_output_marker!("panic!()")).unwrap();
assert!(!out.success());
}
#[test]
fn test_expr_qmark() {
let code = with_output_marker!("\"42\".parse::<i32>()?.wrapping_add(1)");
let out = rust_script!("-e", code).unwrap();
scan!(out.stdout_output();
("43") => ()
)
.unwrap();
}
#[test]
fn test_expr_template() {
let template_dir = "tests/data/templates";
let out = rust_script!(
#[env(RUST_SCRIPT_DEBUG_TEMPLATE_PATH=template_dir)]
"-t",
"shout",
"-e",
with_output_marker!(r#""no way? no way!""#)
)
.unwrap();
scan!(out.stdout_output();
("NO WAY? NO WAY!") => ()
)
.unwrap();
}
#[test]
fn test_expr_template_with_deps() {
let template_dir = "tests/data/templates";
let out = rust_script!(
#[env(RUST_SCRIPT_DEBUG_TEMPLATE_PATH=template_dir)]
"-t",
"boolinate",
"-e",
with_output_marker!(r#"true"#)
)
.unwrap();
scan!(out.stdout_output();
("Some(())") => ()
)
.unwrap();
}
#[test]
fn test_expr_template_override_expr() {
let template_dir = "tests/data/templates/override";
let out = rust_script!(
#[env(RUST_SCRIPT_DEBUG_TEMPLATE_PATH=template_dir)]
"-e",
with_output_marker!(r#"true"#)
)
.unwrap();
scan!(out.stdout_output();
("Some(())") => ()
)
.unwrap();
}

View File

@@ -0,0 +1,15 @@
#[test]
fn test_version() {
let out = rust_script!("--version").unwrap();
assert!(out.success());
scan!(&out.stdout;
("rust-script", &::std::env::var("CARGO_PKG_VERSION").unwrap(), .._) => ()
)
.unwrap();
}
#[test]
fn test_clear_cache() {
let out = rust_script!("--clear-cache").unwrap();
assert!(out.success());
}

View File

@@ -0,0 +1,235 @@
#[test]
fn test_script_explicit() {
let out = rust_script!("-d", "boolinator", "tests/data/script-explicit.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_features() {
let out = rust_script!("--features", "dont-panic", "tests/data/script-features.rs").unwrap();
scan!(out.stdout_output();
("Keep calm and borrow check.") => ()
)
.unwrap();
let out = rust_script!("tests/data/script-features.rs").unwrap();
assert!(!out.success());
}
#[test]
fn test_script_full_block() {
let out = rust_script!("tests/data/script-full-block.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_full_line() {
let out = rust_script!("tests/data/script-full-line.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_full_line_without_main() {
let out = rust_script!("tests/data/script-full-line-without-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_invalid_doc_comment() {
let out = rust_script!("tests/data/script-invalid-doc-comment.rs").unwrap();
scan!(out.stdout_output();
("Hello, World!") => ()
)
.unwrap()
}
#[test]
fn test_script_no_deps() {
let out = rust_script!("tests/data/script-no-deps.rs").unwrap();
scan!(out.stdout_output();
("Hello, World!") => ()
)
.unwrap()
}
#[test]
fn test_script_short() {
let out = rust_script!("tests/data/script-short.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_short_without_main() {
let out = rust_script!("tests/data/script-short-without-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_script_test() {
let out = rust_script!("--test", "tests/data/script-test.rs").unwrap();
assert!(out.success());
}
#[test]
fn test_script_hyphens() {
use scan_rules::scanner::QuotedString;
let out = rust_script!("--", "tests/data/script-args.rs", "-NotAnArg").unwrap();
scan!(out.stdout_output();
("[0]:", let _: QuotedString, "[1]:", let arg: QuotedString) => {
assert_eq!(arg, "-NotAnArg");
}
)
.unwrap()
}
#[test]
fn test_script_hyphens_without_separator() {
use scan_rules::scanner::QuotedString;
let out = rust_script!("tests/data/script-args.rs", "-NotAnArg").unwrap();
scan!(out.stdout_output();
("[0]:", let _: QuotedString, "[1]:", let arg: QuotedString) => {
assert_eq!(arg, "-NotAnArg");
}
)
.unwrap()
}
#[test]
fn test_script_has_weird_chars() {
let out = rust_script!("tests/data/script-has.weird§chars!.rs").unwrap();
assert!(out.success());
}
#[test]
fn test_script_cs_env() {
let out = rust_script!("tests/data/script-cs-env.rs").unwrap();
scan!(out.stdout_output();
("Ok") => ()
)
.unwrap()
}
#[test]
fn test_script_including_relative() {
let out = rust_script!("tests/data/script-including-relative.rs").unwrap();
scan!(out.stdout_output();
("hello, including script") => ()
)
.unwrap()
}
#[test]
fn script_with_same_name_as_dependency() {
let out = rust_script!("tests/data/time.rs").unwrap();
scan!(out.stdout_output();
("Hello") => ()
)
.unwrap()
}
#[test]
fn script_without_main_question_mark() {
let out = rust_script!("tests/data/question-mark").unwrap();
assert!(out
.stderr
.starts_with("Error: Os { code: 2, kind: NotFound, message:"));
}
#[test]
fn test_script_async_main() {
let out = rust_script!("tests/data/script-async-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_pub_fn_main() {
let out = rust_script!("tests/data/pub-fn-main.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_cargo_target_dir_env() {
let out = rust_script!("tests/data/cargo-target-dir-env.rs").unwrap();
scan!(out.stdout_output();
("true") => ()
)
.unwrap()
}
#[test]
fn test_outer_line_doc() {
let out = rust_script!("tests/data/outer-line-doc.rs").unwrap();
scan!(out.stdout_output();
("Some(1)") => ()
)
.unwrap()
}
#[test]
fn test_whitespace_before_main() {
let out = rust_script!("tests/data/whitespace-before-main.rs").unwrap();
scan!(out.stdout_output();
("hello, world") => ()
)
.unwrap()
}
#[test]
fn test_stable_toolchain() {
let out = rust_script!(
"--toolchain-version",
"stable",
"tests/data/script-unstable-feature.rs"
)
.unwrap();
assert!(out.stderr.contains("`#![feature]` may not be used"));
assert!(!out.success());
}
#[test]
fn test_nightly_toolchain() {
let out = rust_script!(
"--toolchain-version",
"nightly",
"tests/data/script-unstable-feature.rs"
)
.unwrap();
scan!(out.stdout_output();
("`#![feature]` *may* be used!") => ()
)
.unwrap();
assert!(out.success());
}
#[test]
fn test_same_flags() {
let out = rust_script!("tests/data/same-flags.rs", "--help").unwrap();
scan!(out.stdout_output();
("Argument: --help") => ()
)
.unwrap()
}

121
external/rust-script/tests/util/mod.rs vendored Normal file
View File

@@ -0,0 +1,121 @@
use std::sync::Mutex;
macro_rules! rust_script {
(
#[env($($env_k:ident=$env_v:expr),* $(,)*)]
$($args:expr),* $(,)*
) => {
{
extern crate tempfile;
use std::process::Command;
let cargo_lock = crate::util::CARGO_MUTEX.lock().expect("Could not acquire Cargo mutex");
let cmd_str;
let out = {
let target_dir = ::std::env::var("CARGO_TARGET_DIR")
.unwrap_or_else(|_| String::from("target"));
let mut cmd = Command::new(format!("{}/debug/rust-script", target_dir));
$(
cmd.arg($args);
)*
cmd.env_remove("CARGO_TARGET_DIR");
$(cmd.env(stringify!($env_k), $env_v);)*
cmd_str = format!("{:?}", cmd);
cmd.output()
.map(crate::util::Output::from)
};
if let Ok(out) = out.as_ref() {
println!("rust-script cmd: {}", cmd_str);
println!("rust-script stdout:");
println!("-----");
println!("{}", out.stdout);
println!("-----");
println!("rust-script stderr:");
println!("-----");
println!("{}", out.stderr);
println!("-----");
}
drop(cargo_lock);
out
}
};
($($args:expr),* $(,)*) => {
rust_script!(#[env()] $($args),*)
};
}
macro_rules! with_output_marker {
(prelude $p:expr; $e:expr) => {
format!(concat!($p, "{}", $e), crate::util::OUTPUT_MARKER_CODE)
};
($e:expr) => {
format!(concat!("{}", $e), crate::util::OUTPUT_MARKER_CODE)
};
}
lazy_static! {
#[doc(hidden)]
pub static ref CARGO_MUTEX: Mutex<()> = Mutex::new(());
}
pub const OUTPUT_MARKER: &str = "--output--";
pub const OUTPUT_MARKER_CODE: &str = "println!(\"--output--\");";
pub struct Output {
pub status: ::std::process::ExitStatus,
pub stdout: String,
pub stderr: String,
}
impl Output {
pub fn stdout_output(&self) -> &str {
assert!(self.success());
for marker in self.stdout.matches(OUTPUT_MARKER) {
let i = subslice_offset(&self.stdout, marker).expect("couldn't find marker in output");
let before_cp = self.stdout[..i].chars().rev().next().unwrap_or('\n');
if !(before_cp == '\r' || before_cp == '\n') {
continue;
}
let after = &self.stdout[i + OUTPUT_MARKER.len()..];
let after_cp = after.chars().next().expect("couldn't find cp after marker");
if !(after_cp == '\r' || after_cp == '\n') {
continue;
}
return after;
}
panic!("could not find `{}` in script output", OUTPUT_MARKER);
}
pub fn success(&self) -> bool {
self.status.success()
}
}
impl From<::std::process::Output> for Output {
fn from(v: ::std::process::Output) -> Self {
Self {
status: v.status,
stdout: String::from_utf8(v.stdout).unwrap(),
stderr: String::from_utf8(v.stderr).unwrap(),
}
}
}
fn subslice_offset(outer: &str, inner: &str) -> Option<usize> {
let outer_beg = outer.as_ptr() as usize;
let inner = inner.as_ptr() as usize;
if inner < outer_beg || inner > outer_beg.wrapping_add(outer.len()) {
None
} else {
Some(inner.wrapping_sub(outer_beg))
}
}